/**1* @license2* Copyright 2025 The Emscripten Authors3* SPDX-License-Identifier: MIT4*/56// This file is used as the initial script loaded into pthread workers when7// running in WASM_ESM_INTEGRATION mode.8// Tyhe point of this file is to delay the loading of the main program module9// until the wasm memory has been received via postMessage.1011#if RUNTIME_DEBUG12console.log("Running pthread_esm_startup");13#endif1415#if ENVIRONMENT_MAY_BE_NODE16// Create as web-worker-like an environment as we can.17var worker_threads = await import('worker_threads');18global.Worker = worker_threads.Worker;19var parentPort = worker_threads['parentPort'];20parentPort.on('message', (msg) => global.onmessage?.({ data: msg }));21Object.assign(globalThis, {22self: global,23postMessage: (msg) => parentPort['postMessage'](msg),24});25#endif2627self.onmessage = async (msg) => {28#if RUNTIME_DEBUG29console.log('pthread_esm_startup', msg.data.cmd);30#endif31if (msg.data.cmd == 'load') {32// Until we initialize the runtime, queue up any further incoming messages33// that can arrive while the async import (await import below) is happening.34// For examples the `run` message often arrives right away before the import35// is complete.36let messageQueue = [msg];37self.onmessage = (e) => messageQueue.push(e);3839// Now that we have the wasmMemory we can import the main program40globalThis.wasmMemory = msg.data.wasmMemory;41const prog = await import('./{{{ TARGET_JS_NAME }}}');4243// Now that the import is completed the main program will have installed44// its own `onmessage` handler and replaced our handler.45// Now we can dispatch any queued messages to this new handler.46for (let msg of messageQueue) {47await self.onmessage(msg);48}4950await prog.default()51}52};535455