Skip to content

Add fallback for emval_get_global to support Web Workers #9723

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

Merged
merged 1 commit into from
Oct 30, 2019

Conversation

osman-turan
Copy link
Contributor

In generated WebAssembly modules, emval_get_global is used for accessing global variables. This function tries to get globalThis property with a fallback mechanism. Unfortunately, fallback mechanism does not use Web Workers environment global object (self). This commit fixes that problem.

@welcome
Copy link

welcome bot commented Oct 27, 2019

Thank you for submitting a pull request! If this is your first PR, make sure to add yourself to AUTHORS.

Copy link
Contributor

@jgravelle-google jgravelle-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, given that the surrounding code seems totally bonkers.

Do we actually need to assign $$$embind_global$$$ = self; if the condition is true, in any of these if cases? Doesn't testGlobal do that for us?
I'm completely okay with leaving that well enough alone, I just don't think I understand that part of the existing code.

@@ -222,6 +222,8 @@ var LibraryEmVal = {
$$$embind_global$$$ = global;
} else if (typeof window === 'object' && testGlobal(window)) {
$$$embind_global$$$ = window;
} else if (typeof self === 'object' && testGlobal(self)) {
$$$embind_global$$$ = self;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't have guessed that self was for workers, though that might just be me. Might be worth a comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I've just added it for the sake of clarity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to know how far back self works. caniuse isn't clear: https://caniuse.com/#feat=mdn-api_window_self

The safe thing might be to check ENVIRONMENT_IS_WEB vs _WORKER, at least in LEGACY_VM_SUPPORT mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kripken I've added ENVIRONMENT_IS_WORKER precondition as a safety measure.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self has existed basically forever (like top, parent, etc.). In fact, we don't even need a separate branch for window as self is more universal and works both in windows and in workers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sgtm to do it that way, thanks @RReverser

@osman-turan sorry for the back-and-forth! But sounds like it'll be better to do it like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kripken No problem. I've just changed that way with a comment. Please feel free to tell me if we need further improvement.

@osman-turan osman-turan force-pushed the incoming branch 2 times, most recently from 770a141 to f1a801d Compare October 28, 2019 22:41
@osman-turan
Copy link
Contributor Author

@jgravelle-google

Do we actually need to assign $$$embind_global$$$ = self; if the condition is true, in any of these if cases?

Yes, during testing in testGlobal method, it's already assigned. Due to potential negative impact in case I'm missing something, I decided keep my changes minimum to just support Web Workers environment. If the maintainers are OK, I can change it with this pull-request.

@kripken
Copy link
Member

kripken commented Oct 30, 2019

Looks good, thanks @osman-turan!

The CI test errors are because older incoming was broken, sorry about that. Please merge in latest incoming to here, and tests should pass.

In generated WebAssembly modules, `emval_get_global` is used for accessing global variables. This function tries to get `globalThis` property with a fallback mechanism. Unfortunately, fallback mechanism does not use Web Workers environment global object (`self`). This commit fixes that problem.
@osman-turan
Copy link
Contributor Author

@kripken No problem. It can be expected in such a huge code base. I've updated my pull-request with a clean state.

@kripken
Copy link
Member

kripken commented Oct 30, 2019

Perfect, thanks again @osman-turan!

@kripken kripken merged commit 2d2442c into emscripten-core:incoming Oct 30, 2019
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
…core#9723)

In generated WebAssembly modules, `emval_get_global` is
used for accessing global variables. This function tries to get
`globalThis` property with a fallback mechanism. Unfortunately,
fallback mechanism does not use Web Workers environment
global object (`self`). This commit fixes that problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants