| /** | |
| * @license | |
| * Copyright 2025 The Emscripten Authors | |
| * SPDX-License-Identifier: MIT | |
| */ | |
| // This file is used as the initial script loaded into pthread workers when | |
| // running in WASM_ESM_INTEGRATION mode. | |
| // The point of this file is to delay the loading of the main program module | |
| // until the wasm memory has been received via postMessage. | |
| #if RUNTIME_DEBUG | |
| console.log('Running pthread_esm_startup'); | |
| #endif | |
| #if ENVIRONMENT_MAY_BE_NODE | |
| if ({{{ nodeDetectionCode() }}}) { | |
| // Create as web-worker-like an environment as we can. | |
| globalThis.self = globalThis; | |
| const {Worker, parentPort} = await import('node:worker_threads'); | |
| globalThis.Worker = Worker; | |
| // Deno and Bun already have `postMessage` defined on the global scope and | |
| // deliver messages to `globalThis.onmessage`, so we must not duplicate that | |
| // behavior here if `postMessage` is already present. | |
| if (!globalThis.postMessage) { | |
| parentPort.on('message', (msg) => globalThis.onmessage?.({ data: msg })); | |
| globalThis.postMessage = (msg) => parentPort.postMessage(msg); | |
| } | |
| // Node.js Workers do not pass postMessage()s and uncaught exception events to the parent | |
| // thread necessarily in the same order where they were generated in sequential program order. | |
| // See https://github.com/nodejs/node/issues/59617 | |
| // To remedy this, capture all uncaughtExceptions in the Worker, and sequentialize those over | |
| // to the same postMessage pipe that other messages use. | |
| const CMD_UNCAUGHT_EXN = 8; | |
| process.on('uncaughtException', (err) => { | |
| postMessage({ cmd: CMD_UNCAUGHT_EXN, error: err }); | |
| // Also shut down the Worker to match the same semantics as if this uncaughtException | |
| // handler was not registered. | |
| // (n.b. this will not shut down the whole Node.js app process, but just the Worker) | |
| process.exit(1); | |
| }); | |
| } | |
| #endif | |
| self.onmessage = async (msg) => { | |
| #if RUNTIME_DEBUG | |
| console.log('pthread_esm_startup', msg.data.cmd); | |
| #endif | |
| // Until we initialize the runtime, queue up any further incoming messages | |
| // that can arrive while the async import (await import below) is happening. | |
| // For examples the `run` message often arrives right away before the import | |
| // is complete. | |
| #if ASSERTIONS | |
| if (!msg.data.wasmMemory) console.error('first message should include wasmMemory'); | |
| #endif | |
| const messageQueue = [msg]; | |
| self.onmessage = (e) => messageQueue.push(e); | |
| // Now that we have the wasmMemory we can import the main program | |
| globalThis.wasmMemory = msg.data.wasmMemory; | |
| const prog = await import('./{{{ TARGET_JS_NAME }}}'); | |
| #if !AUTO_INIT | |
| await prog.default() | |
| #endif | |
| // Now that the import is completed the main program will have installed | |
| // its own `onmessage` handler and replaced our handler. | |
| // Now we can dispatch any queued messages to this new handler. | |
| for (const msg of messageQueue) { | |
| await self.onmessage(msg); | |
| } | |
| }; |