Wednesday, May 9, 2012

New Dart to JavaScript compiler ready for testing

Posted by Kasper Lund

We're getting close to replacing our old friend "frog"—the current
Dart-to-JavaScript compiler—with a new version called "dart2js". We've
built the new version with an emphasis on correctness and
compatibility with the Dart VM. This means that you can now take
advantage of some of the niceties of closures and code like this:

  main() {
    var closures = [];
    for (var i = 0; i < 8; i++) closures.add(() => i);
    for (var closure in closures) print(closure());
  }


will no longer print 88888888 when compiled to JavaScript, but
01234567 just like on the VM.

The new compiler is available in the latest continuous builds of the
Dart Editor and SDK. If you click on [Tools > Generate JavaScript] in
the editor menu, you'll see that we're automatically compiling your
code with both frog and dart2js allowing you to easily compare the
output:

 Generating JavaScript using frog...
 Wrote ...Sunflower.dart.js [103.0kb written in 1.9 seconds]
 Generating JavaScript using dart2js...
 Wrote ...Sunflower.dart.js_ [126.0kb written in 2.6 seconds]

We're actively working on making the output smaller and faster, so
expect improvements over the next weeks. If you want to try to use the
dart2js output in your web app, you need to change the <script> tag in
the HTML file to include the data-compiler="dart2js" attribute like
this:

 <script type="application/dart" src="..." data-compiler="dart2js"></script>

Writing a new compiler isn't completely trivial, and we're not done yet.
There are a few features remaining before we are at parity with the VM,
such as "support continue in switch statement" and
"support closures in initializer lists". 
As always we appreciate any feedback you
might be able to provide us with in the form of bug reports or e-mails
sent to the mailing list (http://www.dartlang.org/support/). If you
find that your project doesn't work when compiled with dart2js, we
really want to work with you to fix that.

For the technically interested, I can tell you that the new compiler
uses an internal representation in SSA form (static single assignment)
and that the compiler is implemented entirely in Dart.
Performance-wise it isn't highly tuned yet, but we feel like we have a
good infrastructure in place for interesting and effective
optimizations. We will keep you posted on our progress!

Cheers,
Kasper on behalf of the dart2js team