dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.26k stars 1.58k forks source link

Ability to utilize existing Javascript code #625

Closed DartBot closed 9 years ago

DartBot commented 12 years ago

This issue was originally filed by Tojiro@gmail.com


Dart does not currently have any way to interact with existing Javascript libraries, and this may be detrimental to adoption. While long term the goal is obviously to have more Dart-native libraries available, while Dart picks up steam it would be invaluable to the first few generations of applications.

There are many useful and non-trivial javascript libraries available that could be useful in Dart but difficult to re-implement. Some examples include:

 - Math libraries like Sylvester or glMatrix  - Physics libraries like Box2D  - UI libraries like SproutCore  - Graphics frameworks like Three.js or processing.js  - API wrappers for protocols like OAuth, Twitter, Facebook, etc.

These are all things that can be ported over time, but only with a good deal of effort and maintenance. As such developers that wish to use the features they provide are more likely to avoid Dart.

Even if javascript code cannot be used directly, the ability to create interface wrappers (like JNI for Java/C interaction) would allow Dart developers much greater flexibility from the start without waiting for a large library ecosystem to build up around the language.

DartBot commented 12 years ago

This comment was originally written by drfibonacci@google.com


Removed Type-Defect label. Added Type-Enhancement, Area-Library, Triaged labels.

dgrove commented 12 years ago

cc @sigmundch.

DartBot commented 12 years ago

This comment was originally written by @chrisbu


Some kind of equivalent to GWT's JSNI would be useful. A particular use case that I have had in the past is interacting with a java applet from GWT via JSNI.

DartBot commented 12 years ago

This comment was originally written by huntc...@gmail.com


Another perspective here is that library authors of new and existing libraries may wish to only target JavaScript given its wide availability and their limited programmer resources.

DartBot commented 12 years ago

This comment was originally written by ericTree...@gmail.com


I would add to the list a small method_missing library implemented by Nakajima. (https://github.com/nakajima/method-missing-js). That library allows for the construction of an HTML/XML "builder" that uses function calls to generate HTML, instead of having to output strings. (Upside: No worries about closing braces or simple syntax errors. Even better: Ability to generate a builder dynamically and eval it to create the result.)

That sort of thing won't be possible in Dart. And to expand on the argument, here is eloquent case by Florian Bosch at https://groups.google.com/a/dartlang.org/group/misc/browse_thread/thread/64f25f37bb690797/56f18cd62c04688d?lnk=gst&q=Calling+old+javascript+code#­56f18cd62c04688d:


There are two cases: 1) Calling from Javascript to Dart 2) Calling from Dart to Javascript

Not being able to do both is a HUGE issue. People are not going to abandon all their own JS they have written over the years and all their frameworks like ExtJS, jQuery, Dojo, Three.js, Glow, Prototype/ Script.acu.lous, Pyjamas, GWT, midori, MochiKit, qooxdoo, Rialto, Rico, YUI, FUEL, Ally, Ample, DHTMLX, iX, underscore, backbone, jiglib, box2djs, Processing.js, socket.io, spark, sproutcore, SoundManager, Sylvester, Zreshk and thousands more.

People (will) understand that calling out to JS will hurt performance. But that is far more favorable for most then throwing away all they know.

DartBot commented 12 years ago

This comment was originally written by bakerstreet...@gmail.com


At a minimum you should be able to "export" functions from Dart to javascript so that they are accessible via javascript.

One method might be using Attributes like [Export]/[Import] to mark functions which are either imported from Javascript (which in Darts case would be left as null for the compiler to pick up) or exported to Javascript so that Javascript can access them.

I am not sure that this option is the best solution since it kind of defeats the idea of replacing what GWT does with comments into the spec itself but it is what Visual Studio MEF extensions Do to allow communication between the Extension and IDE. Except this time you would just be enabling communication between JS and Dart without sacrificing the Isolation that Dart provides.

sethladd commented 12 years ago

cc @vsmenon.

sethladd commented 12 years ago

Removed Area-Library label. Added Area-DOM label.

vsmenon commented 12 years ago

Set owner to @vsmenon. Added this to the M1 milestone.

DartBot commented 12 years ago

This comment was originally written by mattsh@google.com

DartBot commented 12 years ago

This comment was originally written by version....@gmail.com


+1 What about use google maps (or any other huge and useful js lib) in dart application?

kasperl commented 12 years ago

We've made some progress on the low-level building blocks for supporting JS interop better. See http://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/tests/html/js_interop_2_test.dart for a test case that runs using dart2js and in Dartium.

DartBot commented 12 years ago

This comment was originally written by @chrisbu


http://blog.dartwatch.com/2012/06/dart-javascript-interoperability-coming.html

This blog post extracts some of the code from a recent code review, showing JS and googlemaps integration with Dart.

DartBot commented 12 years ago

This comment was originally written by version...@gmail.com


It's not what we would like. I want this:

native void someOverlayFunc /-{    // js code here }-/;

Like JSNI methods in GWT. And, imho, it necessary for progress of language

iposva-google commented 12 years ago

Removed Area-DOM label. Added Area-HTML label.

vsmenon commented 12 years ago

Removed Priority-Medium label. Added Priority-High, Started labels.

vsmenon commented 12 years ago

We're starting to land a Dart-JavaScript interop library here:

https://github.com/dart-lang/js-interop/

It can be pulled into Dart projects via pub. Preliminary dartdocs are here with pointers to some samples:

http://dart-lang.github.com/js-interop/docs/js.html

vsmenon commented 12 years ago

The initial version has been uploaded to pub. I'm closing this bug. Please file separate bugs for specific issues.

See the github page for details, docs, samples, and usage instructions:

 https://github.com/dart-lang/js-interop/


cc @kwalrath. cc @sethladd. Added Fixed label.