Understanding dart2js patching

69 views
Skip to first unread message

sara...@gmail.com

unread,
Jun 18, 2014, 10:37:01 PM6/18/14
to compil...@dartlang.org
Hi,
I was wondering if someone could help in understanding how dart2js replaces or patches function calls with javascript calls.
For eg., the print function call gets replaced with printString.
I would like to know the code path that achieves this patching.
I'm attempting to see if i can use dart2js to re-target other languages
Thanks,
Sarat

Johnni Winther

unread,
Jun 19, 2014, 3:56:50 AM6/19/14
to sara...@gmail.com, Dart Compiler Developers
Currently two mechanism are at play: patching and foreign helpers.

Patching is our mechanism for supplying implementation to external methods. 

For instance the 'print' method in dart:core calls an external method called 'printToConsole' defined in an internal library called dart:_internal.

Internal libraries cannot be import directly but are used internally to share implementation of the dart: libraries. The implementation of the external method 'printToConsole' is defined through patching. Both the VM and dart2js have special means of providing such patch implementation. For dart2js we have a so-called patch library for each dart: library. 

For the dart:_internal library this is defined in sdk/lib/_internal/lib/internal_patch.dart. Here printToConsole is defined as

void printToConsole(String line) {
  printString(line);
}

where is printString is defined in another JavaScript-specific internal library called dart:_js_helper. 

The implementation of printString is implemented through calls to a magic method called JS, defined in yet another JavaScript-specific internal library called dart:_foreign_helper. The JS method is specific in that it is handled specially by our JavaScript generating backend. Instead of having an implementation written in Dart, the implementation is generated directly into JavaScript using the arguments.

Both the patching system and the foreign helpers are currently hard-wired into dart2js but we are working on making the features purely backend dependent meaning that for instance our Dart-generating backend does not rely on their existence and for your use-case maybe even support backends to other languages that can provide their own patch implementations.


To unsubscribe from this group and stop receiving emails from it, send an email to compiler-dev...@dartlang.org.

Sarat Adiraj

unread,
Jun 19, 2014, 3:54:30 PM6/19/14
to Johnni Winther, Dart Compiler Developers
Johnni,
Thanks for the explanation. It's given me a good idea on what the dart2js compiler is doing.
I agree that a factory pattern for loading the foreign helpers, patching system and printer system based on a target flag passed to the dart2js compiler will be ideal.
dart2js should ideally be called dart2Any ;-)

Cheers,
Sarat
Reply all
Reply to author
Forward
0 new messages