How does the dart javascript compiler (dart2js) wo

2019-06-21 20:18发布

问题:

Dart runs in its own Dart VM, but you can compile it to modern optimized JavaScript. But how does that work? Are there any articles or papers online which explain that process?

I am wondering if that is an easy straight forward matching element to element, only time consuming to develop, process or are there some elements/aspects of Dart which could not be compiled to JavaScript.

http://www.dartlang.org

EDIT:thx for your 2 answers. The point is, that i told a collegue of mine about dart and that dart can be compiled to JavaScript. He accepted that it could be compiled to JavaScript but it would result in mumbojumba code and will only work for simple stuff. In the end he said: '''ok, explain me. how is this compiling going to work on nontrivial code?'''

That's why i am asking. I was hoping that there is some material online i could link him.

回答1:

The Dart Editor will now compile your Dart code to JavaScript with dart2js.

As for the second part of your question, the dart:io libraries are only for the server/command-line, so they can't be compiled with dart2js.

I realize now I think you are asking "how did the engineers design and implement the dart2js compiler", not "how do you run dart2js"

Here's a blog post on the announcement of dart2js: http://news.dartlang.org/2012/05/new-dart-to-javascript-compiler-ready.html

Kasper Lund, one of the engineers, adds "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."

The source code is at http://code.google.com/p/dart/source/browse/#svn%2Fbranches%2Fbleeding_edge%2Fdart%2Flib%2Fcompiler%2Fimplementation



回答2:

Are there any articles or papers online which explain that process?

The front-end is your classic hand-rolled lexer and recursive descent parser. The back end is, I believe doing a bunch of type inference (since Dart's type annotations are too loose to let you do reliable static type analysis from them, and the runtime semantics don't let you take the type annotations seriously). If you look for papers on "concrete type inference", I believe you'll find some of the literature you're looking for. Also, any reference on compiling to SSA form (and doing code gen from that form, I assume) should help.



回答3:

Kasper lund on the intermediate representation and the implemented optimizations. https://www.youtube.com/watch?v=GwBb_nqQLuc