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