From c573d0543224604aa866ceb1c478bd5d608a2cd5 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/parseTools.mjs | 12 ++++++++- src/postamble.js | 23 ++-------------- src/preamble.js | 27 ++++++++++++------- .../test_codesize_minimal_pthreads.gzsize | 2 +- .../test_codesize_minimal_pthreads.jssize | 2 +- 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 +- 9 files changed, 40 insertions(+), 37 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/parseTools.mjs b/src/parseTools.mjs index ca474affa3978..4c05218ec63d4 100644 --- a/src/parseTools.mjs +++ b/src/parseTools.mjs @@ -1094,14 +1094,23 @@ function implicitSelf() { } function ENVIRONMENT_IS_MAIN_THREAD() { + assert(PTHREADS || WASM_WORKERS); var envs = []; if (PTHREADS) envs.push('ENVIRONMENT_IS_PTHREAD'); if (WASM_WORKERS) envs.push('ENVIRONMENT_IS_WASM_WORKER'); - if (AUDIO_WORKLET) envs.push('ENVIRONMENT_IS_AUDIO_WORKLET'); if (envs.length == 0) return 'true'; return '(!(' + envs.join('||') + '))'; } +function ENVIRONMENT_IS_WORKER_THREAD() { + assert(PTHREADS || WASM_WORKERS); + var envs = []; + if (PTHREADS) envs.push('ENVIRONMENT_IS_PTHREAD'); + if (WASM_WORKERS) envs.push('ENVIRONMENT_IS_WASM_WORKER'); + if (envs.length == 0) return 'true'; + return '(' + envs.join('||') + ')'; +} + addToCompileTimeContext({ ATEXITS, ATINITS, @@ -1120,6 +1129,7 @@ addToCompileTimeContext({ TARGET_NOT_SUPPORTED, WASM_PAGE_SIZE, ENVIRONMENT_IS_MAIN_THREAD, + ENVIRONMENT_IS_WORKER_THREAD, addAtExit, addAtInit, addReadyPromiseAssertions, diff --git a/src/postamble.js b/src/postamble.js index 075ed1048e48d..be35b43bb1f71 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -161,25 +161,9 @@ function run() { return; } -#if WASM_WORKERS - if (ENVIRONMENT_IS_WASM_WORKER) { -#if MODULARIZE - readyPromiseResolve(Module); -#endif // MODULARIZE - return initRuntime(); - } -#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 +#if PTHREADS || WASM_WORKERS + if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) { initRuntime(); - startWorker(Module); return; } #endif @@ -213,9 +197,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..7d455400552d4 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 @@ -219,7 +216,7 @@ function initRuntime() { #endif #if PTHREADS - if (ENVIRONMENT_IS_PTHREAD) return; + if (ENVIRONMENT_IS_PTHREAD) return startWorker(Module); #endif #if STACK_OVERFLOW_CHECK @@ -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/codesize/test_codesize_minimal_pthreads.gzsize b/test/other/codesize/test_codesize_minimal_pthreads.gzsize index 0f12704884a5d..e47cad5f248ac 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.gzsize +++ b/test/other/codesize/test_codesize_minimal_pthreads.gzsize @@ -1 +1 @@ -4208 +4209 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.jssize b/test/other/codesize/test_codesize_minimal_pthreads.jssize index db6dfb1df8651..7db41a45918a0 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.jssize +++ b/test/other/codesize/test_codesize_minimal_pthreads.jssize @@ -1 +1 @@ -8703 +8716 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