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['$wb'] || './${ TARGET_JS_NAME } '
45
34
#else
46
35
Module['$wb']
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,14 @@ 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
+ Module [ '$ww' ] = m [ '$ww' ] ;
101
+ #if RUNTIME_DEBUG
102
+ dbg ( "wasmWorkerInitializeRuntime $ww:" , Module [ '$ww' ] ) ;
103
+ #endif
98
104
99
105
#if ! MINIMAL_RUNTIME && isSymbolNeeded ( '$noExitRuntime' )
100
106
// Wasm workers basically never exit their runtime
@@ -149,20 +155,9 @@ addToLibrary({
149
155
#endif
150
156
} ,
151
157
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
158
_emscripten_create_wasm_worker__deps : [
161
159
'$_wasmWorkers' , '$_wasmWorkersID' ,
162
160
'$_wasmWorkerAppendToQueue' , '$_wasmWorkerRunPostMessage' ,
163
- #if WASM_WORKERS == 2
164
- '$_wasmWorkerBlobUrl' ,
165
- #endif
166
161
#if ASSERTIONS
167
162
'emscripten_has_threading_support' ,
168
163
#endif
@@ -174,7 +169,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
174
169
&& !ENVIRONMENT_IS_AUDIO_WORKLET
175
170
#endif
176
171
) {
177
- _wasmWorkers[0] = this ;
172
+ _wasmWorkers[0] = globalThis ;
178
173
addEventListener("message", _wasmWorkerAppendToQueue);
179
174
}` ,
180
175
_emscripten_create_wasm_worker : ( stackLowestAddress , stackSize ) => {
@@ -191,28 +186,21 @@ if (ENVIRONMENT_IS_WASM_WORKER
191
186
var p = trustedTypes . createPolicy (
192
187
'emscripten#workerPolicy1' , { createScriptURL : ( ignored ) => { { { wasmWorkerJs } } } }
193
188
) ;
194
- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) ) ;
189
+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( p . createScriptURL ( 'ignored' ) , { { { wasmWorkerOptions } } } ) ;
195
190
} else
196
191
#endif
197
- worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } ) ;
192
+ worker = _wasmWorkers [ _wasmWorkersID ] = new Worker ( { { { wasmWorkerJs } } } , { { { wasmWorkerOptions } } } ) ;
198
193
// Craft the Module object for the Wasm Worker scope:
199
194
worker . postMessage ( {
200
195
// Signal with a non-zero value that this Worker will be a Wasm Worker,
201
196
// and not the main browser thread.
202
197
'$ww' : _wasmWorkersID ,
203
198
#if MINIMAL_RUNTIME
204
199
'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
200
#else
212
201
'wasm' : wasmModule ,
213
- 'js' : Module [ 'mainScriptUrlOrBlob' ] || _scriptName ,
214
- 'wasmMemory' : wasmMemory ,
215
202
#endif
203
+ 'mem' : wasmMemory ,
216
204
'sb' : stackLowestAddress , // sb = stack bottom (lowest stack address, SP points at this when stack is full)
217
205
'sz' : stackSize , // sz = stack size
218
206
} ) ;
0 commit comments