-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[esm-integration] Add pthread support #24555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @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. | ||
// Tyhe 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 | ||
// Create as web-worker-like an environment as we can. | ||
var worker_threads = await import('worker_threads'); | ||
global.Worker = worker_threads.Worker; | ||
var parentPort = worker_threads['parentPort']; | ||
parentPort.on('message', (msg) => global.onmessage?.({ data: msg })); | ||
Object.assign(globalThis, { | ||
self: global, | ||
postMessage: (msg) => parentPort['postMessage'](msg), | ||
}); | ||
#endif | ||
|
||
self.onmessage = async (msg) => { | ||
#if RUNTIME_DEBUG | ||
console.log('pthread_esm_startup', msg.data.cmd); | ||
#endif | ||
if (msg.data.cmd == 'load') { | ||
// 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. | ||
let 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 }}}'); | ||
|
||
// 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 (let msg of messageQueue) { | ||
await self.onmessage(msg); | ||
} | ||
|
||
await prog.default() | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.