Skip to content

Commit

Permalink
[ES6] Only use import.meta when strictly necessary (emscripten-core#8940
Browse files Browse the repository at this point in the history
)

A webworker ES6 module doesn't have access to 'document.currentScript'. 'import.meta' was the only way I could see to achieve that functionality.

Fixes emscripten-core#8729.

Since this appears to fix the issue for everyone who commented on emscripten-core#8729, and no one proposed any better ideas, this is my suggestion.

It would be nice to use import.meta everywhere, and browsers seem to have good support, however the tooling appears to be lagging a bit behind.
  • Loading branch information
VirtualTim authored and belraquib committed Dec 23, 2020
1 parent 62cb1a0 commit d7793ef
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2991,7 +2991,9 @@ def modularize():
# after document.currentScript is gone, so we save it.
# (when MODULARIZE_INSTANCE, an instance is created
# immediately anyhow, like in non-modularize mode)
if shared.Settings.EXPORT_ES6:
# In EXPORT_ES6 + USE_PTHREADS the 'thread' is actually an ES6 module webworker running in strict mode,
# so doesn't have access to 'document'. In this case use 'import.meta' instead.
if shared.Settings.EXPORT_ES6 and shared.Settings.USE_PTHREADS:
script_url = "import.meta.url"
else:
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
Expand Down
4 changes: 4 additions & 0 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ if (Module['ENVIRONMENT']) {
#if USE_PTHREADS && (!MODULARIZE || MODULARIZE_INSTANCE)
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
// before the page load. In non-MODULARIZE modes generate it here.
#if EXPORT_ES6
var _scriptDir = import.meta.url;
#else
var _scriptDir = (typeof document !== 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
#endif
#endif

// `/` should be present at the end if `scriptDirectory` is not empty
var scriptDirectory = '';
Expand Down

0 comments on commit d7793ef

Please sign in to comment.