Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dart2js using self when it should (likely) use globalThis #59982

Open
kevmoo opened this issue Jan 24, 2025 · 4 comments
Open

dart2js using self when it should (likely) use globalThis #59982

kevmoo opened this issue Jan 24, 2025 · 4 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-js-interop Issues that impact all js interop

Comments

@kevmoo
Copy link
Member

kevmoo commented Jan 24, 2025

These docs: https://api.dart.dev/dart-js_interop/globalContext.html

Say globalContext can be treated like JS's globalThis.

But when compiling, self is used, which blows up in Node.

Replacing self with globalThis in the output fixes issues.

Seeing this both in the use of globalContext from js_interop and with the code generated with createJSInteropWrapper

diff --git a/out.js b/out.js
index 0c4c10f..fe9f8d3 100644
--- a/out.js
+++ b/out.js
@@ -2636,7 +2636,7 @@
     },
     main() {
       A.print("Hello World!");
-      type$.JSObject._as(self).exportValue = new A.main_closure(new A.Export()).call$0();
+      type$.JSObject._as(globalThis).exportValue = new A.main_closure(new A.Export()).call$0();
     },
     main_closure: function main_closure(t0) {
       this.thing = t0;
@@ -3146,7 +3146,7 @@
     call$0() {
       var result,
         t1 = type$.JSObject,
-        t2 = t1._as(t1._as(self).Object),
+        t2 = t1._as(t1._as(globalThis).Object),
         _jsExporter = t1._as(t2.create.apply(t2, [null]));
       t2 = this.thing.get$printMessage();
       if (typeof t2 == "function")
@kevmoo
Copy link
Member Author

kevmoo commented Jan 24, 2025

CC @srujzs and @rakudrama

@dart-github-bot
Copy link
Collaborator

Summary: dart2js uses self instead of globalThis in generated JS code, causing errors in Node.js environments. Replacing self with globalThis resolves the issue.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jan 24, 2025
@srujzs
Copy link
Contributor

srujzs commented Jan 24, 2025

There are subtle differences depending on the compiler, but in general, globalContext can be treated like JavaScript's globalThis.

Unfortunately, you came across the subtle differences. :D Historically, package:js used self and dart.global in dart2js and ddc. When some users migrated to dart:js_interop, they saw failures in older browsers that didn't support globalThis. So, to make the migration easier, the global context that's used in every compiler is slightly different.

@sigmundch
Copy link
Member

For context, we made the intentional change to use self here: https://dart-review.googlesource.com/c/sdk/+/319301

@srujzs srujzs added the web-js-interop Issues that impact all js interop label Jan 24, 2025
@mraleph mraleph removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

5 participants