| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| #if ASSERTIONS |
| |
| |
| var workerID = 0; |
| #endif |
|
|
| #if MAIN_MODULE |
| |
| |
| var sharedModules = {}; |
| #endif |
|
|
| var startWorker; |
|
|
| if (ENVIRONMENT_IS_PTHREAD) { |
| |
| var initializedJS = false; |
|
|
| #if LOAD_SOURCE_MAP |
| |
| |
| |
| |
| |
| function resetPrototype(constructor, attrs) { |
| var object = Object.create(constructor.prototype); |
| return Object.assign(object, attrs); |
| } |
| #endif |
|
|
| |
| |
| self.onunhandledrejection = (e) => { throw e.reason || e; }; |
|
|
| {{{ asyncIf(ASYNCIFY == 2) }}}function handleMessage(e) { |
| try { |
| var msgData = e['data']; |
| |
| var cmd = msgData.cmd; |
| if (cmd === 'load') { |
| #if ASSERTIONS |
| workerID = msgData.workerID; |
| #endif |
| #if PTHREADS_DEBUG |
| dbg('worker: loading module') |
| #endif |
|
|
| |
| let messageQueue = []; |
| self.onmessage = (e) => messageQueue.push(e); |
|
|
| |
| startWorker = () => { |
| |
| postMessage({ cmd: 'loaded' }); |
| |
| for (let msg of messageQueue) { |
| handleMessage(msg); |
| } |
| |
| self.onmessage = handleMessage; |
| }; |
|
|
| #if MAIN_MODULE |
| dynamicLibraries = msgData.dynamicLibraries; |
| sharedModules = msgData.sharedModules; |
| #if RUNTIME_DEBUG |
| dbg(`worker: received ${Object.keys(msgData.sharedModules).length} shared modules: ${Object.keys(msgData.sharedModules)}`); |
| #endif |
| #endif |
|
|
| |
| |
| for (const handler of msgData.handlers) { |
| |
| |
| |
| if (!Module[handler] || Module[handler].proxy) { |
| #if RUNTIME_DEBUG |
| dbg(`worker: installer proxying handler: ${handler}`); |
| #endif |
| Module[handler] = (...args) => { |
| #if RUNTIME_DEBUG |
| dbg(`worker: calling handler on main thread: ${handler}`); |
| #endif |
| postMessage({ cmd: 'callHandler', handler, args: args }); |
| } |
| |
| if (handler == 'print') out = Module[handler]; |
| if (handler == 'printErr') err = Module[handler]; |
| } |
| #if RUNTIME_DEBUG |
| else dbg(`worker: using thread-local handler: ${handler}`); |
| #endif |
| } |
|
|
| #if !WASM_ESM_INTEGRATION |
| wasmMemory = msgData.wasmMemory; |
| updateMemoryViews(); |
| #endif |
|
|
| #if LOAD_SOURCE_MAP |
| wasmSourceMap = resetPrototype(WasmSourceMap, msgData.wasmSourceMap); |
| #endif |
|
|
| #if !WASM_ESM_INTEGRATION |
| #if MINIMAL_RUNTIME |
| |
| Module['wasm'] = msgData.wasmModule; |
| loadModule(); |
| #else |
| wasmModule = msgData.wasmModule; |
| #if MODULARIZE == 'instance' |
| init(); |
| #else |
| createWasm(); |
| run(); |
| #endif |
| #endif |
| #endif |
| } else if (cmd === 'run') { |
| #if ASSERTIONS |
| assert(msgData.pthread_ptr); |
| #endif |
| |
| |
| |
| establishStackSpace(msgData.pthread_ptr); |
|
|
| |
| __emscripten_thread_init(msgData.pthread_ptr, 0, 0, 1, 0, 0); |
|
|
| #if OFFSCREENCANVAS_SUPPORT |
| PThread.receiveOffscreenCanvases(msgData); |
| #endif |
| PThread.threadInitTLS(); |
|
|
| |
| |
| __emscripten_thread_mailbox_await(msgData.pthread_ptr); |
|
|
| if (!initializedJS) { |
| #if EMBIND |
| #if PTHREADS_DEBUG |
| dbg(`worker: Pthread 0x${_pthread_self().toString(16)} initializing embind.`); |
| #endif |
| |
| |
| __embind_initialize_bindings(); |
| #endif |
| initializedJS = true; |
| } |
|
|
| try { |
| {{{ awaitIf(ASYNCIFY == 2) }}}invokeEntryPoint(msgData.start_routine, msgData.arg); |
| } catch(ex) { |
| if (ex != 'unwind') { |
| |
| |
| |
| throw ex; |
| } |
| #if RUNTIME_DEBUG |
| dbg(`worker: Pthread 0x${_pthread_self().toString(16)} completed its main entry point with an 'unwind', keeping the worker alive for asynchronous operation.`); |
| #endif |
| } |
| } else if (msgData.target === 'setimmediate') { |
| |
| } else if (cmd === 'checkMailbox') { |
| if (initializedJS) { |
| checkMailbox(); |
| } |
| } else if (cmd) { |
| |
| |
| |
| err(`worker: received unknown command ${cmd}`); |
| err(msgData); |
| } |
| } catch(ex) { |
| #if ASSERTIONS |
| err(`worker: onmessage() captured an uncaught exception: ${ex}`); |
| if (ex?.stack) err(ex.stack); |
| #endif |
| __emscripten_thread_crashed(); |
| throw ex; |
| } |
| }; |
|
|
| self.onmessage = handleMessage; |
|
|
| } |
|
|