Skip to content

Commit 31f9fb3

Browse files
authored
Fix ENVIRONMENT_IS_WORKER detection on Deno (#22778)
`importScripts()` is always `undefined` on Deno.
1 parent c194f15 commit 31f9fb3

File tree

5 files changed

+7
-15
lines changed

5 files changed

+7
-15
lines changed

src/library_pthread_stub.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var LibraryPThreadStub = {
1818

1919
emscripten_is_main_browser_thread: () =>
2020
#if MINIMAL_RUNTIME
21-
typeof importScripts == 'undefined'
21+
typeof WorkerGlobalScope == 'undefined'
2222
#else
2323
!ENVIRONMENT_IS_WORKER
2424
#endif

src/runtime_pthread.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ if (ENVIRONMENT_IS_PTHREAD) {
3131

3232
Object.assign(globalThis, {
3333
self: global,
34-
// Dummy importScripts. The presence of this global is used
35-
// to detect that we are running on a Worker.
36-
// TODO(sbc): Find another way?
37-
importScripts: () => {
38-
#if ASSERTIONS
39-
assert(false, 'dummy importScripts called');
40-
#endif
41-
},
4234
postMessage: (msg) => parentPort.postMessage(msg),
4335
});
4436
}

src/shell.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var ENVIRONMENT_IS_AUDIO_WORKLET = typeof AudioWorkletGlobalScope !== 'undefined
8484
var ENVIRONMENT_IS_WEB = {{{ ENVIRONMENT === 'web' }}};
8585
#if PTHREADS && ENVIRONMENT_MAY_BE_NODE
8686
// node+pthreads always supports workers; detect which we are at runtime
87-
var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function';
87+
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
8888
#else
8989
var ENVIRONMENT_IS_WORKER = {{{ ENVIRONMENT === 'worker' }}};
9090
#endif
@@ -93,7 +93,7 @@ var ENVIRONMENT_IS_SHELL = {{{ ENVIRONMENT === 'shell' }}};
9393
#else // ENVIRONMENT
9494
// Attempt to auto-detect the environment
9595
var ENVIRONMENT_IS_WEB = typeof window == 'object';
96-
var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function';
96+
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
9797
// N.b. Electron.js environment is simultaneously a NODE-environment, but
9898
// also a web environment.
9999
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) {
298298
if (ENVIRONMENT_IS_SHELL) {
299299

300300
#if ENVIRONMENT && ASSERTIONS
301-
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?)');
301+
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?)');
302302
#endif
303303

304304
#if ENVIRONMENT_MAY_BE_SHELL
@@ -398,7 +398,7 @@ if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
398398
}
399399

400400
#if ENVIRONMENT && ASSERTIONS
401-
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?)');
401+
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?)');
402402
#endif
403403

404404
#if PTHREADS && ENVIRONMENT_MAY_BE_NODE

src/shell_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function ready() {
137137
#if PTHREADS
138138
// MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments
139139
// coincide.
140-
var ENVIRONMENT_IS_WORKER = typeof importScripts == 'function';
140+
var ENVIRONMENT_IS_WORKER = typeof WorkerGlobalScope != 'undefined';
141141
var ENVIRONMENT_IS_PTHREAD = ENVIRONMENT_IS_WORKER && self.name?.startsWith('em-pthread');
142142

143143
#if !MODULARIZE

test/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15016,7 +15016,7 @@ def test_embind_no_duplicate_symbols(self):
1501615016
def test_no_pthread(self):
1501715017
self.do_runf('hello_world.c', emcc_args=['-pthread', '-no-pthread'])
1501815018
self.assertExists('hello_world.js')
15019-
self.assertNotContained('Worker', read_file('hello_world.js'))
15019+
self.assertNotContained('new Worker(', read_file('hello_world.js'))
1502015020

1502115021
def test_sysroot_includes_first(self):
1502215022
self.do_other_test('test_stdint_limits.c', emcc_args=['-std=c11', '-iwithsysroot/include'])

0 commit comments

Comments
 (0)