Dart-Compiled Dart Compiler Compiles Dart Code in Browser

by Zef Hemel on May 13, 2013 |

Google has relaunched, the online playground that allows users try out Dart without having to download and install the Dart SDK. Unlike its predecessor, the new version compiles Dart code to JavaScript in the browser and, as a result, also works offline. It also supports dart:html, Dart's library for HTML DOM manipulation. However, its implementation is perhaps the most impressive aspect of screenshot

Peter von der Ahé announced the release on Google+:

We have revived Compared to the old version, the new version features in-browser compilation, support for dart:html, and works offline.

If you have an iOS device, you can install Try Dart! as an app on your home screen.

While there are playground environments available for most languages -- for instance, try ruby, or which offers REPLs (Read Eval Print Loops) for many languages, including Python, JavaScript, Lua and many others -- most of these playground sites send the user's input to the server, execute the code there and send the result back to the client. is different in that it both compiles and executes the code in the browser.

So, how does it work? How did the Dart team manage to host the Dart to JavaScript compiler (called dart2js) in the browser? A look at the source code repository of the project reveals the trick.

The dart2js compiler is itself written in Dart. Usually, it runs using the DartVM included with the SDK. However, to implement, the Dart team invoked the dart2js compiler on itself, thereby producing a compiler that translates Dart code to JavaScript in a single 457KB JavaScript file. This JavaScript version of the compiler is loaded into the browser and invoked every time the Dart code in the editor changes. The resulting JavaScript code is then evaluated and the result displayed.

While running the dart2js compiler in the browser is not a typical example of the type of web applications that one would build using Dart, bootstrapping the dart2js compiler in this way is an impressive technical feat. comes with a number of prepackaged example applications that can easily be run and tweaked to get a better understanding of how the language works. The examples range in complexity from a simple hello world application to the Delta Blue benchmark

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread by Amjad Masad

Great article. However, small correction, doesn't have a backend, all languages are executed on the client. We cross-compiled Python, Ruby, and Lua's interpreters to JavaScript using emscripten.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss
General Feedback
Marketing and all content copyright © 2006-2016 C4Media Inc. hosted at Contegix, the best ISP we've ever worked with.
Privacy policy

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.