sdk icon indicating copy to clipboard operation
sdk copied to clipboard

[js_interop/dart2wasm] Canonicalize and move `<constant-or-literal>.toJS` at compile-time to mjs file

Open mkustermann opened this issue 1 year ago • 0 comments

We should evaluate whether it makes sense for dart2wasm recognize <constant-or-literal>.toJS expressions and canonicalize & move those objects at compile-time to JS (by emitting them into the ...mjs file and importing those external references) instead of lazily at runtime.

It may be very common to have code such as

@JS()
external JSObject foo(JSString value);

main() {
  foo('foo'.toJS);
}

Currently one may be tempted to cache such manually at the top-level via

@JS()
external JSObject foo(JSString value);

final jsFooArg = 'foo'.toJS;
main() {
  foo(jsFooArg);
}

but this compiles to worse code in dart2js (see https://github.com/dart-lang/sdk/issues/56045)

/cc @srujzs

mkustermann avatar Jun 19 '24 09:06 mkustermann