From 544dbe2c24c66ea4c56074d4bc455f3296111f88 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Sat, 7 Dec 2024 20:32:19 -0800 Subject: [PATCH] Unify readyPromiseResolve calls --- src/library.js | 5 +++- src/postamble.js | 15 ++--------- src/preamble.js | 25 +++++++++++++------ test/other/test_unoptimized_code_size.js.size | 2 +- ...t_unoptimized_code_size_no_asserts.js.size | 2 +- .../test_unoptimized_code_size_strict.js.size | 2 +- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/library.js b/src/library.js index 132de3c2910b0..300c4e95fd691 100644 --- a/src/library.js +++ b/src/library.js @@ -128,7 +128,10 @@ addToLibrary({ if (keepRuntimeAlive() && !implicit) { var msg = `program exited (with status: ${status}), but keepRuntimeAlive() is set (counter=${runtimeKeepaliveCounter}) due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)`; #if MODULARIZE - readyPromiseReject(msg); + if (!runtimeInitialized) { + readyPromiseReject(msg); + return; + } #endif // MODULARIZE err(msg); } diff --git a/src/postamble.js b/src/postamble.js index 075ed1048e48d..64639e7a98503 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -163,21 +163,13 @@ function run() { #if WASM_WORKERS if (ENVIRONMENT_IS_WASM_WORKER) { -#if MODULARIZE - readyPromiseResolve(Module); -#endif // MODULARIZE - return initRuntime(); + initRuntime(); + return; } #endif #if PTHREADS if (ENVIRONMENT_IS_PTHREAD) { -#if MODULARIZE - // The promise resolve function typically gets called as part of the execution - // of `doRun` below. The workers/pthreads don't execute `doRun` so the - // creation promise can be resolved, marking the pthread-Module as initialized. - readyPromiseResolve(Module); -#endif // MODULARIZE initRuntime(); startWorker(Module); return; @@ -213,9 +205,6 @@ function run() { preMain(); #endif -#if MODULARIZE - readyPromiseResolve(Module); -#endif #if expectToReceiveOnModule('onRuntimeInitialized') Module['onRuntimeInitialized']?.(); #endif diff --git a/src/preamble.js b/src/preamble.js index 23ee003bb5592..74da3acfbb764 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -205,10 +205,7 @@ function preRun() { callRuntimeCallbacks(__ATPRERUN__); } -function initRuntime() { -#if RUNTIME_DEBUG - dbg('initRuntime'); -#endif +function doInitRuntime() { #if ASSERTIONS assert(!runtimeInitialized); #endif @@ -237,6 +234,16 @@ function initRuntime() { callRuntimeCallbacks(__ATINIT__); } +function initRuntime() { +#if RUNTIME_DEBUG + dbg('initRuntime'); +#endif + doInitRuntime(); +#if MODULARIZE + readyPromiseResolve(Module); +#endif +} + #if HAS_MAIN function preMain() { #if STACK_OVERFLOW_CHECK @@ -471,11 +478,13 @@ function abort(what) { var e = new WebAssembly.RuntimeError(what); #if MODULARIZE - readyPromiseReject(e); + if (!runtimeInitialized) { + // If the runtime has not yet been initializated then reject the + // ready promise instead of thowing the error; + readyPromiseReject(e); + return; + } #endif - // Throw the error whether or not MODULARIZE is set because abort is used - // in code paths apart from instantiation where an exception is expected - // to be thrown when abort is called. throw e; } diff --git a/test/other/test_unoptimized_code_size.js.size b/test/other/test_unoptimized_code_size.js.size index 48f4d8dcae999..70861ad7e25ca 100644 --- a/test/other/test_unoptimized_code_size.js.size +++ b/test/other/test_unoptimized_code_size.js.size @@ -1 +1 @@ -54928 +54786 diff --git a/test/other/test_unoptimized_code_size_no_asserts.js.size b/test/other/test_unoptimized_code_size_no_asserts.js.size index 3926438285887..3a436364b573f 100644 --- a/test/other/test_unoptimized_code_size_no_asserts.js.size +++ b/test/other/test_unoptimized_code_size_no_asserts.js.size @@ -1 +1 @@ -30574 +30432 diff --git a/test/other/test_unoptimized_code_size_strict.js.size b/test/other/test_unoptimized_code_size_strict.js.size index 5c3cf64221739..9c7cbb680befd 100644 --- a/test/other/test_unoptimized_code_size_strict.js.size +++ b/test/other/test_unoptimized_code_size_strict.js.size @@ -1 +1 @@ -53724 +53582