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
3425{ { {
3526 const workerSupportsFutexWait = ( ) => AUDIO_WORKLET ? "typeof AudioWorkletGlobalScope === 'undefined'" : '1' ;
3627 const wasmWorkerJs = `
37- #if WASM_WORKERS == 2
38- _wasmWorkerBlobUrl
39- #elif MINIMAL_RUNTIME
28+ #if MINIMAL_RUNTIME
4029#if ENVIRONMENT_MAY_BE_NODE
41- Module['$wb'] || './${ WASM_WORKER_FILE } '
30+ Module['$wb'] || './${ TARGET_JS_NAME } '
4231#else
4332 Module['$wb']
4433#endif
4534#else
46- locateFile('${ WASM_WORKER_FILE } ')
35+ locateFile('${ TARGET_JS_NAME } ')
4736#endif
4837` ;
38+ const wasmWorkerOptions = `{
39+ #if ENVIRONMENT_MAY_BE_NODE
40+ // This is the way that we signal to the node worker that it is hosting
41+ // a wasm worker.
42+ 'workerData': 'em-ww',
43+ #endif
44+ #if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
45+ // This is the way that we signal to the Web Worker that it is hosting
46+ // a pthread.
47+ 'name': 'em-ww',
48+ #endif
49+ }` ;
4950} } }
5051
5152#endif // ~WASM_WORKERS
@@ -87,11 +88,16 @@ addToLibrary({
8788#endif
8889 ] ,
8990 $_wasmWorkerInitializeRuntime : ( ) => {
90- let m = Module ;
91+ let m = wasmWorkerArgs ;
9192#if ASSERTIONS
93+ assert ( m && m [ '$ww' ] ) ;
9294 assert ( m [ 'sb' ] % 16 == 0 ) ;
9395 assert ( m [ 'sz' ] % 16 == 0 ) ;
9496#endif
97+ Module [ '$ww' ] = m [ '$ww' ] ;
98+ #if RUNTIME_DEBUG
99+ dbg ( "wasmWorkerInitializeRuntime $ww:" , Module [ '$ww' ] ) ;
100+ #endif
95101
96102#if ! MINIMAL_RUNTIME && isSymbolNeeded ( '$noExitRuntime' )
97103 // Wasm workers basically never exit their runtime
@@ -146,20 +152,9 @@ addToLibrary({
146152#endif
147153 } ,
148154
149- #if WASM_WORKERS == 2
150- // In WASM_WORKERS == 2 build mode, we create the Wasm Worker global scope
151- // script from a string bundled in the main application JS file. This
152- // simplifies the number of deployed JS files with the app, but has a downside
153- // that the generated build output will no longer be csp-eval compliant.
154- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_eval_expressions
155- $_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'}))" ,
156- #endif
157155 _emscripten_create_wasm_worker__deps : [
158156 '$_wasmWorkers' , '$_wasmWorkersID' ,
159157 '$_wasmWorkerAppendToQueue' , '$_wasmWorkerRunPostMessage' ,
160- #if WASM_WORKERS == 2
161- '$_wasmWorkerBlobUrl' ,
162- #endif
163158#if ASSERTIONS
164159 'emscripten_has_threading_support' ,
165160#endif
@@ -171,7 +166,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
171166 && !ENVIRONMENT_IS_AUDIO_WORKLET
172167#endif
173168 ) {
174- _wasmWorkers[0] = this ;
169+ _wasmWorkers[0] = globalThis ;
175170 addEventListener("message", _wasmWorkerAppendToQueue);
176171}` ,
177172 _emscripten_create_wasm_worker : ( stackLowestAddress , stackSize ) => {
@@ -188,28 +183,21 @@ if (ENVIRONMENT_IS_WASM_WORKER
188183 var p = trustedTypes . createPolicy (
189184 'emscripten#workerPolicy1' , { createScriptURL : ( ignored ) => { { { wasmWorkerJs } } } }
190185 ) ;
191- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) ) ;
186+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) , { { { wasmWorkerOptions } } } ) ;
192187 } else
193188#endif
194- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } ) ;
189+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } , { { { wasmWorkerOptions } } } ) ;
195190 // Craft the Module object for the Wasm Worker scope:
196191 worker . postMessage ( {
197192 // Signal with a non-zero value that this Worker will be a Wasm Worker,
198193 // and not the main browser thread.
199194 '$ww' : _wasmWorkersID ,
200195#if MINIMAL_RUNTIME
201196 'wasm' : Module [ 'wasm' ] ,
202- #if ENVIRONMENT_MAY_BE_NODE
203- 'js' : Module [ 'js' ] || './{{{ TARGET_JS_NAME }}}' ,
204- #else
205- 'js' : Module [ 'js' ] ,
206- #endif
207- 'mem' : wasmMemory ,
208197#else
209198 'wasm' : wasmModule ,
210- 'js' : Module [ 'mainScriptUrlOrBlob' ] || _scriptName ,
211- 'wasmMemory' : wasmMemory ,
212199#endif
200+ 'mem' : wasmMemory ,
213201 'sb' : stackLowestAddress , // sb = stack bottom (lowest stack address, SP points at this when stack is full)
214202 'sz' : stackSize , // sz = stack size
215203 } ) ;
0 commit comments