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.