Skip to content

Commit 2b44e64

Browse files
authored
Fix ENVIRONMENT_IS_WORKER under WASM_WORKERS + node (#26562)
This fix came about because I was trying to remove the `@requires_pthread` decorator from the `WASM_WORKER` tests in test_core.py. This decorator should not be needed or used for wasm worker tests because it injects the `-pthread` compile flag. Without this fix the `ENVIRONMENT_IS_WORKER` is not set correctly under node which means that `emscripten_is_main_browser_thread` was reporting the wrong value when called from within a Wasm Worker. This bug was being hidden when the program was compiled with both pthreads and wasm workers enabled.
1 parent 2760f83 commit 2b44e64

3 files changed

Lines changed: 13 additions & 16 deletions

File tree

src/shell_minimal.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_NODE;
5353
#endif
5454
#endif // ASSERTIONS || PTHREADS
5555

56+
#if ENVIRONMENT_MAY_BE_WORKER || (PTHREADS || WASM_WORKERS)
57+
var ENVIRONMENT_IS_WORKER = !!globalThis.WorkerGlobalScope;
58+
#endif
59+
5660
#if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS)
5761
if (ENVIRONMENT_IS_NODE) {
5862
var worker_threads = require('node:worker_threads');
5963
global.Worker = worker_threads.Worker;
64+
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
6065
}
6166
#endif
6267

@@ -136,10 +141,6 @@ var readAsync, readBinary;
136141
#include "node_shell_read.js"
137142
#endif
138143

139-
#if ENVIRONMENT_MAY_BE_WORKER || PTHREADS
140-
var ENVIRONMENT_IS_WORKER = !!globalThis.WorkerGlobalScope;
141-
#endif
142-
143144
#if PTHREADS
144145
// MINIMAL_RUNTIME does not support --proxy-to-worker option, so Worker and Pthread environments
145146
// coincide.

test/codesize/hello_wasm_worker_wasm.expected.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var c = Module, d = "em-ww" == globalThis.name, e = !!globalThis.WorkerGlobalScope, f, g, C, r, D, n, E, w;
1+
var c = Module, d = !!globalThis.WorkerGlobalScope, e = "em-ww" == globalThis.name, f, g, C, r, D, n, E, w;
22

3-
d && (onmessage = a => {
3+
e && (onmessage = a => {
44
onmessage = null;
55
f = a = a.data;
66
g = a.o;
@@ -13,7 +13,7 @@ d && (onmessage = a => {
1313

1414
function h() {}
1515

16-
d || (g = c.mem || new WebAssembly.Memory({
16+
e || (g = c.mem || new WebAssembly.Memory({
1717
initial: 256,
1818
maximum: 256,
1919
shared: !0
@@ -26,7 +26,7 @@ var l = [], p = a => {
2626
}, q = a => {
2727
l.push(a);
2828
}, t = () => {
29-
r(0, !e, !d, e && 1);
29+
r(0, !d, !e, d && 1);
3030
}, u = {}, v = 1, x = (a, b) => {
3131
let m = u[v] = new Worker(c.js, {
3232
name: "em-ww"
@@ -47,7 +47,7 @@ var l = [], p = a => {
4747
});
4848
};
4949

50-
d && (u[0] = globalThis, addEventListener("message", q));
50+
e && (u[0] = globalThis, addEventListener("message", q));
5151

5252
function B() {
5353
console.log("Hello from wasm worker!");
@@ -72,9 +72,9 @@ function k() {
7272
r = b.k;
7373
D = b.l;
7474
n = b.j;
75-
d ? (D(f.v, f.s, f.u), removeEventListener("message", q), l = l.forEach(p), addEventListener("message", p)) : b.h();
76-
d || C();
75+
e ? (D(f.v, f.s, f.u), removeEventListener("message", q), l = l.forEach(p), addEventListener("message", p)) : b.h();
76+
e || C();
7777
}));
7878
}
7979

80-
d || k();
80+
e || k();

test/test_core.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9700,7 +9700,6 @@ def test_emscripten_async_load_script(self):
97009700
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'file1.txt', 'file2.txt', '--from-emcc', '--js-output=script2.js'])
97019701
self.do_runf('test_emscripten_async_load_script.c', cflags=['-sFORCE_FILESYSTEM'])
97029702

9703-
@requires_pthreads
97049703
@no_sanitize('sanitizers do not support WASM_WORKERS')
97059704
@also_with_minimal_runtime
97069705
@also_with_modularize
@@ -9711,19 +9710,16 @@ def test_wasm_worker_hello(self):
97119710
self.maybe_closure()
97129711
self.do_run_in_out_file_test('wasm_worker/hello_wasm_worker.c', cflags=['-sWASM_WORKERS'])
97139712

9714-
@requires_pthreads
97159713
@no_sanitize('sanitizers do not support WASM_WORKERS')
97169714
@no_esm_integration('WASM_ESM_INTEGRATION is not compatible with WASM_WORKERS')
97179715
def test_wasm_worker_malloc(self):
97189716
self.do_run_in_out_file_test('wasm_worker/malloc_wasm_worker.c', cflags=['-sWASM_WORKERS'])
97199717

9720-
@requires_pthreads
97219718
@no_sanitize('sanitizers do not support WASM_WORKERS')
97229719
@no_esm_integration('WASM_ESM_INTEGRATION is not compatible with WASM_WORKERS')
97239720
def test_wasm_worker_runtime_debug(self):
97249721
self.do_runf('wasm_worker/hello_wasm_worker.c', 'wasm worker starting ...', cflags=['-sWASM_WORKERS', '-sRUNTIME_DEBUG'])
97259722

9726-
@requires_pthreads
97279723
@no_sanitize('sanitizers do not support WASM_WORKERS')
97289724
@no_esm_integration('WASM_ESM_INTEGRATION is not compatible with WASM_WORKERS')
97299725
def test_wasm_worker_wait_async(self):

0 commit comments

Comments
 (0)