Skip to content

Commit cad8aea

Browse files
authored
Fix emscripten_current_thread_is_wasm_worker return value. (#23484)
This function return `ENVIRONMENT_IS_WASM_WORKER` which was being initialized to `Module['$ww']`, i.e. the worker id. Because `emscripten_current_thread_is_wasm_worker`, is defined to return the `bool` type its only valid values are 0 and 1. Returning values larger than 1 will result in undefined behaviour. Fixes: #23479
1 parent 4f8880f commit cad8aea

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

src/shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ if (ENVIRONMENT_IS_NODE) {
122122
#endif // ENVIRONMENT_MAY_BE_NODE
123123

124124
#if WASM_WORKERS
125-
var ENVIRONMENT_IS_WASM_WORKER = Module['$ww'];
125+
var ENVIRONMENT_IS_WASM_WORKER = !!Module['$ww'];
126126
#endif
127127

128128
// --pre-jses are emitted after the Module integration code, so that they can

src/shell_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var ENVIRONMENT_IS_WEB = !ENVIRONMENT_IS_NODE;
7070
#endif // ASSERTIONS || PTHREADS
7171

7272
#if WASM_WORKERS
73-
var ENVIRONMENT_IS_WASM_WORKER = Module['$ww'];
73+
var ENVIRONMENT_IS_WASM_WORKER = !!Module['$ww'];
7474
#endif
7575

7676
#if ASSERTIONS && ENVIRONMENT_MAY_BE_NODE && ENVIRONMENT_MAY_BE_SHELL

test/code_size/hello_wasm_worker_wasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var b = Module, c = b.$ww, e = b.mem || new WebAssembly.Memory({
1+
var b = Module, c = !!b.$ww, e = b.mem || new WebAssembly.Memory({
22
initial: 256,
33
maximum: 256,
44
shared: !0
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"a.html": 618,
33
"a.html.gz": 384,
4-
"a.js": 665,
5-
"a.js.gz": 455,
4+
"a.js": 667,
5+
"a.js.gz": 457,
66
"a.ww.js": 115,
77
"a.ww.js.gz": 127,
88
"a.wasm": 1881,
99
"a.wasm.gz": 1068,
10-
"total": 3279,
11-
"total_gz": 2034
10+
"total": 3281,
11+
"total_gz": 2036
1212
}

test/wasm_worker/wasm_worker_self_id.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void test_success() {
1717
}
1818

1919
void worker1_main() {
20+
assert(emscripten_current_thread_is_wasm_worker());
2021
assert(emscripten_wasm_worker_self_id() != 0);
2122
assert(emscripten_wasm_worker_self_id() == worker1);
2223
if (emscripten_wasm_worker_self_id() == worker1) {
@@ -25,6 +26,7 @@ void worker1_main() {
2526
}
2627

2728
void worker2_main() {
29+
assert(emscripten_current_thread_is_wasm_worker());
2830
assert(emscripten_wasm_worker_self_id() != 0);
2931
assert(emscripten_wasm_worker_self_id() == worker2);
3032
if (emscripten_wasm_worker_self_id() == worker2) {
@@ -36,6 +38,7 @@ char stack1[1024];
3638
char stack2[1024];
3739

3840
int main() {
41+
assert(!emscripten_current_thread_is_wasm_worker());
3942
assert(emscripten_wasm_worker_self_id() == 0);
4043
worker1 = emscripten_create_wasm_worker(stack1, sizeof(stack1));
4144
worker2 = emscripten_create_wasm_worker(stack2, sizeof(stack2));

0 commit comments

Comments
 (0)