diff --git a/emcc.py b/emcc.py index 313d68b4dd0f..d3e8230cc76e 100755 --- a/emcc.py +++ b/emcc.py @@ -2846,14 +2846,19 @@ 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: + script_url = "import.meta.url" + else: + script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined" src = ''' var %(EXPORT_NAME)s = (function() { - var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined; + var _scriptDir = %(script_url)s; return (%(src)s); })(); ''' % { 'EXPORT_NAME': shared.Settings.EXPORT_NAME, - 'src': src + 'src': src, + 'script_url': script_url } else: # Create the MODULARIZE_INSTANCE instance diff --git a/emscripten.py b/emscripten.py index d415557bdb7e..e511850c7d09 100644 --- a/emscripten.py +++ b/emscripten.py @@ -1699,6 +1699,11 @@ def create_receiving(function_table_data, function_tables_defs, exported_impleme shared.Settings.MODULE_EXPORTS = module_exports = exported_implemented_functions + function_tables(function_table_data) + # Currently USE PTHREADS + EXPORT ES6 doesn't work with the previously generated assertions. + # ES6 modules disallow re-assigning read-only properties. + if shared.Settings.USE_PTHREADS and shared.Settings.EXPORT_ES6 and shared.Settings.ASSERTIONS and receiving: + receiving = "assert(!ENVIRONMENT_IS_PTHREAD, 'Error: USE_PTHREADS and EXPORT_ES6 requires ASSERTIONS=0');\n" + receiving + if not shared.Settings.SWAPPABLE_ASM_MODULE: if shared.Settings.DECLARE_ASM_MODULE_EXPORTS: imported_exports = [s for s in module_exports if s not in initializers] diff --git a/src/shell_pthreads.js b/src/shell_pthreads.js index 6b1aeaa17053..e4f9802aa9de 100644 --- a/src/shell_pthreads.js +++ b/src/shell_pthreads.js @@ -4,10 +4,9 @@ // 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true) #if USE_PTHREADS -if (typeof ENVIRONMENT_IS_PTHREAD === 'undefined') { - // ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread. - // N.B. this line needs to appear without 'var' keyword to avoid 'var hoisting' from occurring. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) - ENVIRONMENT_IS_PTHREAD = false; +// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread. +var ENVIRONMENT_IS_PTHREAD = Module.ENVIRONMENT_IS_PTHREAD || false; +if (!ENVIRONMENT_IS_PTHREAD) { var PthreadWorkerInit = {}; // Collects together variables that are needed at initialization time for the web workers that host pthreads. } #if MODULARIZE diff --git a/src/worker.js b/src/worker.js index 44afa4ccc734..9205872591a8 100644 --- a/src/worker.js +++ b/src/worker.js @@ -23,7 +23,6 @@ var buffer; // All pthreads share the same Emscripten HEAP as SharedArrayBuffer var DYNAMICTOP_PTR = 0; var DYNAMIC_BASE = 0; -var ENVIRONMENT_IS_PTHREAD = true; var PthreadWorkerInit = {}; // performance.now() is specced to return a wallclock time in msecs since that Web Worker/main thread launched. However for pthreads this can cause @@ -35,6 +34,10 @@ var __performance_now_clock_drift = 0; // Cannot use console.log or console.error in a web worker, since that would risk a browser deadlock! https://bugzilla.mozilla.org/show_bug.cgi?id=1049091 // Therefore implement custom logging facility for threads running in a worker, which queue the messages to main thread to print. var Module = {}; +#if EXPORT_ES6 +var PThread; +var HEAPU32; +#endif #if ASSERTIONS function assert(condition, text) { @@ -128,7 +131,19 @@ this.onmessage = function(e) { #endif {{{ makeAsmExportAndGlobalAssignTargetInPthread('PthreadWorkerInit') }}} = e.data.PthreadWorkerInit; + Module['ENVIRONMENT_IS_PTHREAD'] = true; +#if MODULARIZE && EXPORT_ES6 + import(e.data.urlOrBlob).then(function({{{ EXPORT_NAME }}}) { + Module = {{{ EXPORT_NAME }}}.default(Module); + PThread = Module['PThread']; + HEAPU32 = Module['HEAPU32']; +#if !ASMFS + if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams(); +#endif + postMessage({ cmd: 'loaded' }); + }); +#else if (typeof e.data.urlOrBlob === 'string') { importScripts(e.data.urlOrBlob); } else { @@ -136,7 +151,6 @@ this.onmessage = function(e) { importScripts(objectUrl); URL.revokeObjectURL(objectUrl); } - #if MODULARIZE #if !MODULARIZE_INSTANCE Module = {{{ EXPORT_NAME }}}(Module); @@ -149,6 +163,7 @@ this.onmessage = function(e) { if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams(); #endif postMessage({ cmd: 'loaded' }); +#endif } else if (e.data.cmd === 'objectTransfer') { PThread.receiveObjectTransfer(e.data); } else if (e.data.cmd === 'run') { // This worker was idle, and now should start executing its pthread entry point.