From 3a11414bf9eef5668b3dc93ab78a14ce54259b1e Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Wed, 23 Oct 2024 12:52:01 +0200 Subject: [PATCH 1/3] Fix `ENVIRONMENT_IS_WORKER` detection on Deno `importScripts()` is always `undefined` on Deno. --- src/library_pthread_stub.js | 2 +- src/runtime_pthread.js | 11 +++-------- src/shell.js | 8 ++++---- src/shell_minimal.js | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) 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 8c194c12156d4..0aa256187eea7 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 909222d4a18e0..2d0c32426d78e 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 @@ -93,7 +93,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'; @@ -298,7 +298,7 @@ if (ENVIRONMENT_IS_NODE) { 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 @@ -398,7 +398,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 97214e1eba361..8247ddd05cf23 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 !MODULARIZE From 277cbd795fdb901a5cc0a4e09eda14c15863cf5b Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Thu, 24 Oct 2024 11:06:23 +0200 Subject: [PATCH 2/3] Fix test expectation --- test/test_other.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_other.py b/test/test_other.py index 61e13c20c037b..34ea591c8d018 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -14919,7 +14919,7 @@ def test_embind_no_duplicate_symbols(self): def test_no_pthread(self): self.do_runf('hello_world.c', emcc_args=['-pthread', '-no-pthread']) self.assertExists('hello_world.js') - self.assertNotContained('Worker', read_file('hello_world.js')) + self.assertNotContained('new Worker(', read_file('hello_world.js')) def test_sysroot_includes_first(self): self.do_other_test('test_stdint_limits.c', emcc_args=['-std=c11', '-iwithsysroot/include']) From 4bab494ba88b664241bbff2b4427d5b24ed9e16a Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Fri, 25 Oct 2024 13:42:54 +0200 Subject: [PATCH 3/3] Remove `WorkerGlobalScope` polyfill for Node --- src/runtime_pthread.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/runtime_pthread.js b/src/runtime_pthread.js index 0aa256187eea7..f4a425dff7e35 100644 --- a/src/runtime_pthread.js +++ b/src/runtime_pthread.js @@ -31,9 +31,6 @@ if (ENVIRONMENT_IS_PTHREAD) { Object.assign(globalThis, { self: global, - // The presence of this global is used to detect - // that we are running on a Worker. - WorkerGlobalScope: global, postMessage: (msg) => parentPort.postMessage(msg), }); }