Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/wasm_worker.js
14363 views
1
var wwParams;
2
3
/**
4
* Called once the initial message has been received from the creating thread.
5
* The `props` object is property bag sent via postMessage to create the worker.
6
*
7
* This function is called both in normal wasm workers and in audio worklets.
8
*/
9
function startWasmWorker(props) {
10
#if RUNTIME_DEBUG
11
dbg('startWasmWorker', props);
12
#endif
13
wwParams = props;
14
wasmMemory = props.wasmMemory;
15
updateMemoryViews();
16
#if MINIMAL_RUNTIME
17
Module ||= {};
18
Module['wasm'] = props.wasm;
19
loadModule()
20
#else
21
wasmModule = props.wasm;
22
#if MODULARIZE == 'instance'
23
init();
24
#else
25
createWasm();
26
run();
27
#endif
28
#endif
29
// Drop now unneeded references to from the Module object in this Worker,
30
// these are not needed anymore.
31
props.wasm = props.wasmMemory = 0;
32
}
33
34
#if AUDIO_WORKLET
35
if (ENVIRONMENT_IS_WASM_WORKER && !ENVIRONMENT_IS_AUDIO_WORKLET) {
36
#else
37
if (ENVIRONMENT_IS_WASM_WORKER) {
38
#endif
39
#if RUNTIME_DEBUG
40
dbg('wasm worker starting ...');
41
#endif
42
43
#if ENVIRONMENT_MAY_BE_NODE
44
// Node.js support
45
if (ENVIRONMENT_IS_NODE) {
46
// Weak map of handle functions to their wrapper. Used to implement
47
// addEventListener/removeEventListener.
48
var wrappedHandlers = new WeakMap();
49
/** @suppress {checkTypes} */
50
globalThis.onmessage = null;
51
function wrapMsgHandler(h) {
52
var f = wrappedHandlers.get(h)
53
if (!f) {
54
f = (msg) => h({data: msg});
55
wrappedHandlers.set(h, f);
56
}
57
return f;
58
}
59
60
Object.assign(globalThis, {
61
addEventListener: (name, handler) => parentPort['on'](name, wrapMsgHandler(handler)),
62
removeEventListener: (name, handler) => parentPort['off'](name, wrapMsgHandler(handler)),
63
});
64
}
65
#endif // ENVIRONMENT_MAY_BE_NODE
66
67
onmessage = (d) => {
68
// The first message sent to the Worker is always the bootstrap message.
69
// Drop this message listener, it served its purpose of bootstrapping
70
// the Wasm Module load, and is no longer needed. Let user code register
71
// any desired message handlers from now on.
72
/** @suppress {checkTypes} */
73
onmessage = null;
74
#if RUNTIME_DEBUG
75
dbg('wasm worker initial onmessage');
76
#endif
77
startWasmWorker(d.data);
78
}
79
80
}
81
82