From f9e6cfe8d7151d52bc036020541c0b24f169e644 Mon Sep 17 00:00:00 2001 From: Tim Lander Date: Sat, 20 Jul 2019 01:35:04 +0800 Subject: [PATCH] [ES6] Only use import.meta when strictly necessary (#8940) 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 #8729. Since this appears to fix the issue for everyone who commented on #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. --- emcc.py | 4 +++- src/shell.js | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/emcc.py b/emcc.py index 66e58c65bfca..e55c0a24d829 100755 --- a/emcc.py +++ b/emcc.py @@ -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" diff --git a/src/shell.js b/src/shell.js index d7c17226ab3b..0e25079035d7 100644 --- a/src/shell.js +++ b/src/shell.js @@ -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 = '';