-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Simplify modular runtime initialization promise #27059
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,27 +114,26 @@ function stackCheckInit() { | |
| } | ||
| #endif | ||
|
|
||
| #if MAIN_READS_PARAMS | ||
| function run(args = programArgs) { | ||
| #else | ||
| function run() { | ||
| #endif | ||
| {{{ asyncIf(MODULARIZE) }}}function run({{{ MAIN_READS_PARAMS ? 'args = programArgs' : '' }}}) { | ||
|
|
||
| #if '$runDependencies' in addedLibraryItems | ||
| if (runDependencies > 0) { | ||
| #if RUNTIME_DEBUG | ||
| dbg('run() called, but dependencies remain, so not running'); | ||
| #endif | ||
| #if MODULARIZE | ||
| await new Promise((resolve) => { | ||
| dependenciesFulfilled = resolve; | ||
| }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this just be a single line |
||
| #else | ||
| dependenciesFulfilled = run; | ||
| return; | ||
| #endif | ||
| } | ||
| #endif | ||
|
|
||
| #if PTHREADS || WASM_WORKERS | ||
| if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) { | ||
| #if MODULARIZE | ||
| readyPromiseResolve?.(Module); | ||
| #endif | ||
| initRuntime(); | ||
| return; | ||
| } | ||
|
|
@@ -152,8 +151,14 @@ function run() { | |
| #if RUNTIME_DEBUG | ||
| dbg('run() called, but dependencies remain, so not running'); | ||
| #endif | ||
| #if MODULARIZE | ||
| await new Promise((resolve) => { | ||
| dependenciesFulfilled = resolve; | ||
| }); | ||
| #else | ||
| dependenciesFulfilled = run; | ||
| return; | ||
| #endif | ||
| } | ||
| #endif | ||
|
|
||
|
|
@@ -174,9 +179,6 @@ function run() { | |
| preMain(); | ||
| #endif | ||
|
|
||
| #if MODULARIZE | ||
| readyPromiseResolve?.(Module); | ||
| #endif | ||
| #if expectToReceiveOnModule('onRuntimeInitialized') | ||
| Module['onRuntimeInitialized']?.(); | ||
| #if ASSERTIONS | ||
|
|
@@ -203,10 +205,20 @@ function run() { | |
| #if expectToReceiveOnModule('setStatus') | ||
| if (Module['setStatus']) { | ||
| Module['setStatus']('Running...'); | ||
| #if MODULARIZE | ||
| await new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| setTimeout(() => Module['setStatus'](''), 1); | ||
| doRun(); | ||
| resolve(); | ||
| }, 1); | ||
| }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code looks kind of weird.. perhaps it should have a comment? |
||
| #else | ||
| setTimeout(() => { | ||
| setTimeout(() => Module['setStatus'](''), 1); | ||
| doRun(); | ||
| }, 1); | ||
| #endif | ||
| } else | ||
| #endif | ||
| { | ||
|
|
@@ -297,13 +309,12 @@ export default async function init(moduleArg = {}) { | |
| #endif | ||
| updateMemoryViews(); | ||
| #if DYNCALLS && '$dynCalls' in addedLibraryItems | ||
|
|
||
| assignDynCalls(); | ||
| #endif | ||
| #else | ||
| wasmExports = await createWasm(); | ||
| #endif | ||
| run(); | ||
| await run(); | ||
| } | ||
|
|
||
| #if ENVIRONMENT_MAY_BE_NODE | ||
|
|
@@ -315,7 +326,7 @@ if (ENVIRONMENT_IS_NODE | |
| ) | ||
| { | ||
| const url = await import('node:url'); | ||
| const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url; | ||
| const isMainModule = process.argv[1] && url.pathToFileURL(process.argv[1]).href === import.meta.url; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I this change related? |
||
| if (isMainModule) await init(); | ||
| } | ||
| #endif | ||
|
|
@@ -335,23 +346,17 @@ if ({{{ ENVIRONMENT_IS_MAIN_THREAD() }}}) { | |
| // Worker threads call this once they receive the module via postMessage | ||
| #endif | ||
|
|
||
| #if WASM_ASYNC_COMPILATION | ||
|
|
||
| #if MODULARIZE | ||
| // In modularize mode the generated code is within a factory function so we | ||
| // can use await here (since it's not top-level-await). | ||
| wasmExports = await createWasm(); | ||
| #else | ||
| #if !MODULARIZE && WASM_ASYNC_COMPILATION | ||
| // With async instantation wasmExports is assigned asynchronously when the | ||
| // instance is received. | ||
| createWasm(); | ||
| #endif | ||
|
|
||
| #else | ||
| wasmExports = createWasm(); | ||
| // In modularize mode the generated code is within a factory function so we | ||
| // can use await here (since it's not top-level-await). | ||
| wasmExports = {{{ awaitIf(MODULARIZE && WASM_ASYNC_COMPILATION) }}}createWasm(); | ||
| #endif | ||
|
|
||
| run(); | ||
| {{{ awaitIf(MODULARIZE) }}}run(); | ||
|
|
||
| #if WASM_WORKERS || PTHREADS | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,5 @@ | ||
| // In MODULARIZE mode we wrap the generated code in a factory function | ||
| // and return either the Module itself, or a promise of the module. | ||
| // | ||
| // We assign to the `moduleRtn` global here and configure closure to see | ||
| // this as an extern so it won't get minified. | ||
|
|
||
| if (runtimeInitialized) { | ||
| moduleRtn = Module; | ||
| } else { | ||
| // Set up the promise that indicates the Module is initialized | ||
| moduleRtn = new Promise((resolve, reject) => { | ||
| readyPromiseResolve = resolve; | ||
| readyPromiseReject = reject; | ||
| }); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is awesome. Since this file doesn't do anything but inject these assertions now I wonder if it should just be removed (perhaps as a followup)? |
||
|
|
||
| #if ASSERTIONS | ||
| // Assertion for attempting to access module properties on the incoming | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make this new code into its own named function? Something like
mangleUnsupportedSyntax? or some better name?I wonder what knock on effects it will have that we now do this in
processMacrosvspreprocess? IIRC there are times when we do one but not the other.