44 * SPDX-License-Identifier: MIT
55 */
66
7- #if WASM_WORKERS == 2
8- // Helpers for _wasmWorkerBlobUrl used in WASM_WORKERS == 2 mode
9- { { {
10- const captureModuleArg = ( ) => MODULARIZE ? '' : 'self.Module=d;' ;
11- const instantiateModule = ( ) => MODULARIZE ? `${ EXPORT_NAME } (d);` : '' ;
12- const instantiateWasm = ( ) => MINIMAL_RUNTIME ? '' : 'd[`instantiateWasm`]=(i,r)=>{var n=new WebAssembly.Instance(d[`wasm`],i);return r(n,d[`wasm`]);};' ;
13- } } }
14- #endif
15-
167#if WASM_WORKERS
178
189#if ! SHARED_MEMORY
3728{ { {
3829 const workerSupportsFutexWait = ( ) => AUDIO_WORKLET ? "typeof AudioWorkletGlobalScope === 'undefined'" : '1' ;
3930 const wasmWorkerJs = `
40- #if WASM_WORKERS == 2
41- _wasmWorkerBlobUrl
42- #elif MINIMAL_RUNTIME
31+ #if MINIMAL_RUNTIME
4332#if ENVIRONMENT_MAY_BE_NODE
44- Module['$wb '] || './${ WASM_WORKER_FILE } '
33+ Module['js '] || './${ TARGET_JS_NAME } '
4534#else
46- Module['$wb ']
35+ Module['js ']
4736#endif
4837#else
49- locateFile('${ WASM_WORKER_FILE } ')
38+ locateFile('${ TARGET_JS_NAME } ')
5039#endif
5140` ;
41+ const wasmWorkerOptions = `{
42+ #if ENVIRONMENT_MAY_BE_NODE
43+ // This is the way that we signal to the node worker that it is hosting
44+ // a wasm worker.
45+ 'workerData': 'em-ww',
46+ #endif
47+ #if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
48+ // This is the way that we signal to the Web Worker that it is hosting
49+ // a pthread.
50+ 'name': 'em-ww',
51+ #endif
52+ }` ;
5253} } }
5354
5455#endif // ~WASM_WORKERS
@@ -92,9 +93,13 @@ addToLibrary({
9293 $_wasmWorkerInitializeRuntime : ( ) => {
9394 let m = Module ;
9495#if ASSERTIONS
96+ assert ( m && m [ '$ww' ] ) ;
9597 assert ( m [ 'sb' ] % 16 == 0 ) ;
9698 assert ( m [ 'sz' ] % 16 == 0 ) ;
9799#endif
100+ #if RUNTIME_DEBUG
101+ dbg ( "wasmWorkerInitializeRuntime $ww:" , m [ '$ww' ] ) ;
102+ #endif
98103
99104#if ! MINIMAL_RUNTIME && isSymbolNeeded ( '$noExitRuntime' )
100105 // Wasm workers basically never exit their runtime
@@ -149,20 +154,9 @@ addToLibrary({
149154#endif
150155 } ,
151156
152- #if WASM_WORKERS == 2
153- // In WASM_WORKERS == 2 build mode, we create the Wasm Worker global scope
154- // script from a string bundled in the main application JS file. This
155- // simplifies the number of deployed JS files with the app, but has a downside
156- // that the generated build output will no longer be csp-eval compliant.
157- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_eval_expressions
158- $_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'}))" ,
159- #endif
160157 _emscripten_create_wasm_worker__deps : [
161158 '$_wasmWorkers' , '$_wasmWorkersID' ,
162159 '$_wasmWorkerAppendToQueue' , '$_wasmWorkerRunPostMessage' ,
163- #if WASM_WORKERS == 2
164- '$_wasmWorkerBlobUrl' ,
165- #endif
166160#if ASSERTIONS
167161 'emscripten_has_threading_support' ,
168162#endif
@@ -174,7 +168,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
174168 && !ENVIRONMENT_IS_AUDIO_WORKLET
175169#endif
176170 ) {
177- _wasmWorkers[0] = this ;
171+ _wasmWorkers[0] = globalThis ;
178172 addEventListener("message", _wasmWorkerAppendToQueue);
179173}` ,
180174 _emscripten_create_wasm_worker : ( stackLowestAddress , stackSize ) => {
@@ -191,28 +185,21 @@ if (ENVIRONMENT_IS_WASM_WORKER
191185 var p = trustedTypes . createPolicy (
192186 'emscripten#workerPolicy1' , { createScriptURL : ( ignored ) => { { { wasmWorkerJs } } } }
193187 ) ;
194- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) ) ;
188+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) , { { { wasmWorkerOptions } } } ) ;
195189 } else
196190#endif
197- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } ) ;
191+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } , { { { wasmWorkerOptions } } } ) ;
198192 // Craft the Module object for the Wasm Worker scope:
199193 worker . postMessage ( {
200194 // Signal with a non-zero value that this Worker will be a Wasm Worker,
201195 // and not the main browser thread.
202196 '$ww' : _wasmWorkersID ,
203197#if MINIMAL_RUNTIME
204198 'wasm' : Module [ 'wasm' ] ,
205- #if ENVIRONMENT_MAY_BE_NODE
206- 'js' : Module [ 'js' ] || './{{{ TARGET_JS_NAME }}}' ,
207- #else
208- 'js' : Module [ 'js' ] ,
209- #endif
210- 'mem' : wasmMemory ,
211199#else
212200 'wasm' : wasmModule ,
213- 'js' : Module [ 'mainScriptUrlOrBlob' ] || _scriptName ,
214- 'wasmMemory' : wasmMemory ,
215201#endif
202+ 'mem' : wasmMemory ,
216203 'sb' : stackLowestAddress , // sb = stack bottom (lowest stack address, SP points at this when stack is full)
217204 'sz' : stackSize , // sz = stack size
218205 } ) ;
0 commit comments