Skip to content
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

Unify readyPromiseResolve calls #23106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 11 additions & 1 deletion src/parseTools.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1120,6 +1129,7 @@ addToCompileTimeContext({
TARGET_NOT_SUPPORTED,
WASM_PAGE_SIZE,
ENVIRONMENT_IS_MAIN_THREAD,
ENVIRONMENT_IS_WORKER_THREAD,
addAtExit,
addAtInit,
addReadyPromiseAssertions,
Expand Down
23 changes: 2 additions & 21 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -213,9 +197,6 @@ function run() {
preMain();
#endif

#if MODULARIZE
readyPromiseResolve(Module);
#endif
#if expectToReceiveOnModule('onRuntimeInitialized')
Module['onRuntimeInitialized']?.();
#endif
Expand Down
27 changes: 18 additions & 9 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ function preRun() {
callRuntimeCallbacks(__ATPRERUN__);
}

function initRuntime() {
#if RUNTIME_DEBUG
dbg('initRuntime');
#endif
function doInitRuntime() {
#if ASSERTIONS
assert(!runtimeInitialized);
#endif
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4208
4209
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8703
8716
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54928
54786
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30574
30432
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53724
53582
Loading