Skip to main content

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

Popular posts from this blog

Dart in 2016: The fastest growing programming language at Google, 2nd fastest growing in TIOBE Index

Dart was the fastest growing programming language at Google in 2016 with millions of lines of code written. It also made it to TIOBE Index Top 20 this month (see TIOBE's methodology ). It takes time to build something as ambitious as Dart and, in some ways, Dart is still in its infancy. But we're glad the hard work is starting to pay off. Many thanks to our amazing community! We're going to celebrate by ... releasing 1.22 next week (as per our usual 6 week release schedule).
Read more

AngularDart is going all Dart

Until now, the multiple language flavors of Angular 2 were written as TypeScript source, and then automatically compiled to both JavaScript and Dart. We're happy to announce that we’re splitting the Angular 2 codebase into two flavors – a Dart version and a TypeScript/JavaScript version – and creating a dedicated AngularDart team. This is amazing news for Dart developers because: The framework will feel more like idiomatic Dart. It will make use of Dart features that couldn't work with the TypeScript flavor. It will be faster. This is equally great news for our TypeScript and JavaScript developers, by the way. Cleaner API, performance gains, easier contributions. Read more on the Angular blog. Angular 2 for Dart is used by many teams at Google. Most famously by the AdWords team, but many other Google teams build large, mobile-friendly web apps. Some of the top requests from these teams were: make the API feel like Dart, provide a faster edit-refresh cycle, and
Read more

The new AdWords UI uses Dart — we asked why

Google just announced a re-designed AdWords experience. In case you’re not familiar with AdWords: businesses use it to advertise on google.com and partner websites. Advertising makes up majority of Google’s revenue, so when Google decides to completely redo the customer-facing front end to it, it’s a big deal. The Dart team is proud to say that this new front end is built with Dart and Angular 2. Whenever you asked us whether Google is ‘even using Dart for anything,’ this is what we had in mind but couldn’t say aloud. Until now. We asked Joshy Joseph , the primary technical lead on the project, some questions. Joshy is focusing on things like infrastructure, application latency and development velocity, so he’s the right person to ask about Dart.   Q: What exactly did we launch on Monday? It’s a complete redesign of the AdWords customer experience that is rolling out slowly as a test to a small initial set of advertisers. The most noticeable thing is probably the Material
Read more