diff --git a/src/library_pthread_stub.js b/src/library_pthread_stub.js index 48ba03a6edd16..44043cd9843b5 100644 --- a/src/library_pthread_stub.js +++ b/src/library_pthread_stub.js @@ -18,7 +18,7 @@ var LibraryPThreadStub = { emscripten_is_main_browser_thread: () => #if MINIMAL_RUNTIME - typeof importScripts == 'undefined' + typeof WorkerGlobalScope == 'undefined' #else !ENVIRONMENT_IS_WORKER #endif diff --git a/src/runtime_pthread.js b/src/runtime_pthread.js index 6444102a16abf..5b4b2757f48b1 100644 --- a/src/runtime_pthread.js +++ b/src/runtime_pthread.js @@ -31,14 +31,9 @@ if (ENVIRONMENT_IS_PTHREAD) { Object.assign(globalThis, { self: global, - // Dummy importScripts. The presence of this global is used - // to detect that we are running on a Worker. - // TODO(sbc): Find another way? - importScripts: () => { -#if ASSERTIONS - assert(false, 'dummy importScripts called'); -#endif - }, + // The presence of this global is used to detect + // that we are running on a Worker. + WorkerGlobalScope: global, postMessage: (msg) => parentPort.postMessage(msg), }); } diff --git a/src/shell.js b/src/shell.js index 03a014603bfd4..aef392cd64e18 100644 --- a/src/shell.js +++ b/src/shell.js @@ -84,7 +84,7 @@ var ENVIRONMENT_IS_AUDIO_WORKLET = typeof AudioWorkletGlobalScope !== 'undefined var ENVIRONMENT_IS_WEB = {{{ ENVIRONMENT === 'web' }}}; #if PTHREADS && ENVIRONMENT_MAY_BE_NODE // node+pthreads always supports workers; detect which we are at runtime -var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function'; +var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined'; #else var ENVIRONMENT_IS_WORKER = {{{ ENVIRONMENT === 'worker' }}}; #endif @@ -96,7 +96,7 @@ var ENVIRONMENT_IS_SHELL = {{{ ENVIRONMENT === 'shell' }}}; #else // ENVIRONMENT // Attempt to auto-detect the environment var ENVIRONMENT_IS_WEB = typeof window == 'object'; -var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function'; +var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined'; // N.b. Electron.js environment is simultaneously a NODE-environment, but // also a web environment. var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer'; @@ -337,7 +337,7 @@ if (ENVIRONMENT_IS_DENO) { // Deno if (ENVIRONMENT_IS_SHELL) { #if ENVIRONMENT && ASSERTIONS - if ((typeof process == 'object' && typeof require === 'function') || typeof window == 'object' || typeof importScripts == 'function') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); + if ((typeof process == 'object' && typeof require === 'function') || typeof window == 'object' || typeof WorkerGlobalScope != 'undefined') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); #endif #if ENVIRONMENT_MAY_BE_SHELL @@ -423,7 +423,7 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) { } #if ENVIRONMENT && ASSERTIONS - if (!(typeof window == 'object' || typeof importScripts == 'function')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); + if (!(typeof window == 'object' || typeof WorkerGlobalScope != 'undefined')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); #endif #if PTHREADS && ENVIRONMENT_MAY_BE_NODE diff --git a/src/shell_minimal.js b/src/shell_minimal.js index a24ef0f0d1fec..a14e5a69ec5e0 100644 --- a/src/shell_minimal.js +++ b/src/shell_minimal.js @@ -137,7 +137,7 @@ function ready() { #if PTHREADS // MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments // coincide. -var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function'; +var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined'; var ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && self.name?.startsWith('em-pthread'); #if ENVIRONMENT_MAY_BE_NODE