From c14c1a95b060ca9112bd293ea199f6422aeb1837 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 11 Oct 2024 10:20:48 -0700 Subject: [PATCH 1/2] Error on wasm worker creation when SAB is not available This should have been part of #22710 --- src/library_wasm_worker.js | 7 ++++++- test/code_size/hello_wasm_worker_wasm.js | 1 + test/code_size/hello_wasm_worker_wasm.json | 8 ++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/library_wasm_worker.js b/src/library_wasm_worker.js index ec50bdb1f8df0..c73cfd05e934e 100644 --- a/src/library_wasm_worker.js +++ b/src/library_wasm_worker.js @@ -137,7 +137,6 @@ addToLibrary({ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_eval_expressions $_wasmWorkerBlobUrl: "URL.createObjectURL(new Blob(['onmessage=function(d){onmessage=null;d=d.data;{{{ captureModuleArg() }}}{{{ instantiateWasm() }}}importScripts(d.js);{{{ instantiateModule() }}}d.wasm=d.mem=d.js=0;}'],{type:'application/javascript'}))", #endif - _emscripten_create_wasm_worker__deps: [ '$_wasmWorkers', '$_wasmWorkersID', '$_wasmWorkerAppendToQueue', '$_wasmWorkerRunPostMessage', @@ -156,6 +155,12 @@ if (ENVIRONMENT_IS_WASM_WORKER addEventListener("message", _wasmWorkerAppendToQueue); }`, _emscripten_create_wasm_worker: (stackLowestAddress, stackSize) => { + if (typeof SharedArrayBuffer == 'undefined') { +#if ASSERTIONS + dbg('create_wasm_worker: environment does not support SharedArrayBuffer, wasm workers are not available'); +#endif + return 0; + } let worker = _wasmWorkers[_wasmWorkersID] = new Worker( #if WASM_WORKERS == 2 // WASM_WORKERS=2 mode embeds .ww.js file contents into the main .js file diff --git a/test/code_size/hello_wasm_worker_wasm.js b/test/code_size/hello_wasm_worker_wasm.js index 4e7446cc3dfa6..4cfde0b3130ef 100644 --- a/test/code_size/hello_wasm_worker_wasm.js +++ b/test/code_size/hello_wasm_worker_wasm.js @@ -15,6 +15,7 @@ c && (m[0] = this, addEventListener("message", l)); WebAssembly.instantiate(b.wasm, { a: { b: (a, d) => { + if ("undefined" == typeof SharedArrayBuffer) return 0; let r = m[n] = new Worker(b.$wb); r.postMessage({ $ww: n, diff --git a/test/code_size/hello_wasm_worker_wasm.json b/test/code_size/hello_wasm_worker_wasm.json index 8e9c264d88594..bde3b5b202084 100644 --- a/test/code_size/hello_wasm_worker_wasm.json +++ b/test/code_size/hello_wasm_worker_wasm.json @@ -1,12 +1,12 @@ { "a.html": 618, "a.html.gz": 384, - "a.js": 665, - "a.js.gz": 455, + "a.js": 715, + "a.js.gz": 484, "a.ww.js": 115, "a.ww.js.gz": 127, "a.wasm": 1850, "a.wasm.gz": 1050, - "total": 3248, - "total_gz": 2016 + "total": 3298, + "total_gz": 2045 } From e097cb694b9b2d01e3746a1ae2497cec5430e1b8 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 11 Oct 2024 13:06:01 -0700 Subject: [PATCH 2/2] feedback --- src/library_wasm_worker.js | 9 ++++++--- test/code_size/hello_wasm_worker_wasm.js | 1 - test/code_size/hello_wasm_worker_wasm.json | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/library_wasm_worker.js b/src/library_wasm_worker.js index c73cfd05e934e..1bda126e3a211 100644 --- a/src/library_wasm_worker.js +++ b/src/library_wasm_worker.js @@ -142,6 +142,9 @@ addToLibrary({ '$_wasmWorkerAppendToQueue', '$_wasmWorkerRunPostMessage', #if WASM_WORKERS == 2 '$_wasmWorkerBlobUrl', +#endif +#if ASSERTIONS + 'emscripten_has_threading_support', #endif ], _emscripten_create_wasm_worker__postset: ` @@ -155,12 +158,12 @@ if (ENVIRONMENT_IS_WASM_WORKER addEventListener("message", _wasmWorkerAppendToQueue); }`, _emscripten_create_wasm_worker: (stackLowestAddress, stackSize) => { - if (typeof SharedArrayBuffer == 'undefined') { #if ASSERTIONS - dbg('create_wasm_worker: environment does not support SharedArrayBuffer, wasm workers are not available'); -#endif + if (!_emscripten_has_threading_support()) { + err('create_wasm_worker: environment does not support SharedArrayBuffer, wasm workers are not available'); return 0; } +#endif let worker = _wasmWorkers[_wasmWorkersID] = new Worker( #if WASM_WORKERS == 2 // WASM_WORKERS=2 mode embeds .ww.js file contents into the main .js file diff --git a/test/code_size/hello_wasm_worker_wasm.js b/test/code_size/hello_wasm_worker_wasm.js index 4cfde0b3130ef..4e7446cc3dfa6 100644 --- a/test/code_size/hello_wasm_worker_wasm.js +++ b/test/code_size/hello_wasm_worker_wasm.js @@ -15,7 +15,6 @@ c && (m[0] = this, addEventListener("message", l)); WebAssembly.instantiate(b.wasm, { a: { b: (a, d) => { - if ("undefined" == typeof SharedArrayBuffer) return 0; let r = m[n] = new Worker(b.$wb); r.postMessage({ $ww: n, diff --git a/test/code_size/hello_wasm_worker_wasm.json b/test/code_size/hello_wasm_worker_wasm.json index bde3b5b202084..8e9c264d88594 100644 --- a/test/code_size/hello_wasm_worker_wasm.json +++ b/test/code_size/hello_wasm_worker_wasm.json @@ -1,12 +1,12 @@ { "a.html": 618, "a.html.gz": 384, - "a.js": 715, - "a.js.gz": 484, + "a.js": 665, + "a.js.gz": 455, "a.ww.js": 115, "a.ww.js.gz": 127, "a.wasm": 1850, "a.wasm.gz": 1050, - "total": 3298, - "total_gz": 2045 + "total": 3248, + "total_gz": 2016 }