4
4
* SPDX-License-Identifier: MIT
5
5
*/
6
6
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
-
16
7
#if WASM_WORKERS
17
8
18
9
#if ! SHARED_MEMORY
34
25
{ { {
35
26
const workerSupportsFutexWait = ( ) => AUDIO_WORKLET ? "typeof AudioWorkletGlobalScope === 'undefined'" : '1' ;
36
27
const wasmWorkerJs = `
37
- #if WASM_WORKERS == 2
38
- _wasmWorkerBlobUrl
39
- #elif MINIMAL_RUNTIME
28
+ #if MINIMAL_RUNTIME
40
29
#if ENVIRONMENT_MAY_BE_NODE
41
- Module['$wb'] || './${ WASM_WORKER_FILE } '
30
+ Module['$wb'] || './${ TARGET_JS_NAME } '
42
31
#else
43
32
Module['$wb']
44
33
#endif
45
34
#else
46
- locateFile('${ WASM_WORKER_FILE } ')
35
+ locateFile('${ TARGET_JS_NAME } ')
47
36
#endif
48
37
` ;
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
+ }` ;
49
50
} } }
50
51
51
52
#endif // ~WASM_WORKERS
@@ -87,11 +88,16 @@ addToLibrary({
87
88
#endif
88
89
] ,
89
90
$_wasmWorkerInitializeRuntime : ( ) => {
90
- let m = Module ;
91
+ let m = wasmWorkerArgs ;
91
92
#if ASSERTIONS
93
+ assert ( m && m [ '$ww' ] ) ;
92
94
assert ( m [ 'sb' ] % 16 == 0 ) ;
93
95
assert ( m [ 'sz' ] % 16 == 0 ) ;
94
96
#endif
97
+ Module [ '$ww' ] = m [ '$ww' ] ;
98
+ #if RUNTIME_DEBUG
99
+ dbg ( "wasmWorkerInitializeRuntime $ww:" , Module [ '$ww' ] ) ;
100
+ #endif
95
101
96
102
#if ! MINIMAL_RUNTIME && isSymbolNeeded ( '$noExitRuntime' )
97
103
// Wasm workers basically never exit their runtime
@@ -146,20 +152,9 @@ addToLibrary({
146
152
#endif
147
153
} ,
148
154
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
157
155
_emscripten_create_wasm_worker__deps : [
158
156
'$_wasmWorkers' , '$_wasmWorkersID' ,
159
157
'$_wasmWorkerAppendToQueue' , '$_wasmWorkerRunPostMessage' ,
160
- #if WASM_WORKERS == 2
161
- '$_wasmWorkerBlobUrl' ,
162
- #endif
163
158
#if ASSERTIONS
164
159
'emscripten_has_threading_support' ,
165
160
#endif
@@ -171,7 +166,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
171
166
&& !ENVIRONMENT_IS_AUDIO_WORKLET
172
167
#endif
173
168
) {
174
- _wasmWorkers[0] = this ;
169
+ _wasmWorkers[0] = globalThis ;
175
170
addEventListener("message", _wasmWorkerAppendToQueue);
176
171
}` ,
177
172
_emscripten_create_wasm_worker : ( stackLowestAddress , stackSize ) => {
@@ -188,28 +183,21 @@ if (ENVIRONMENT_IS_WASM_WORKER
188
183
var p = trustedTypes . createPolicy (
189
184
'emscripten#workerPolicy1' , { createScriptURL : ( ignored ) => { { { wasmWorkerJs } } } }
190
185
) ;
191
- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) ) ;
186
+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) , { { { wasmWorkerOptions } } } ) ;
192
187
} else
193
188
#endif
194
- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } ) ;
189
+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } , { { { wasmWorkerOptions } } } ) ;
195
190
// Craft the Module object for the Wasm Worker scope:
196
191
worker . postMessage ( {
197
192
// Signal with a non-zero value that this Worker will be a Wasm Worker,
198
193
// and not the main browser thread.
199
194
'$ww' : _wasmWorkersID ,
200
195
#if MINIMAL_RUNTIME
201
196
'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 ,
208
197
#else
209
198
'wasm' : wasmModule ,
210
- 'js' : Module [ 'mainScriptUrlOrBlob' ] || _scriptName ,
211
- 'wasmMemory' : wasmMemory ,
212
199
#endif
200
+ 'mem' : wasmMemory ,
213
201
'sb' : stackLowestAddress , // sb = stack bottom (lowest stack address, SP points at this when stack is full)
214
202
'sz' : stackSize , // sz = stack size
215
203
} ) ;
0 commit comments