diff --git a/src/parseTools.mjs b/src/parseTools.mjs index ca474affa3978..fa6da8556e314 100644 --- a/src/parseTools.mjs +++ b/src/parseTools.mjs @@ -785,24 +785,16 @@ export function modifyJSFunction(text, func) { } export function runIfMainThread(text) { - if (WASM_WORKERS && PTHREADS) { - return `if (!ENVIRONMENT_IS_WASM_WORKER && !ENVIRONMENT_IS_PTHREAD) { ${text} }`; - } else if (WASM_WORKERS) { - return `if (!ENVIRONMENT_IS_WASM_WORKER) { ${text} }`; - } else if (PTHREADS) { - return `if (!ENVIRONMENT_IS_PTHREAD) { ${text} }`; + if (WASM_WORKERS || PTHREADS) { + return `if (${ENVIRONMENT_IS_MAIN_THREAD()}) { ${text} }`; } else { return text; } } function runIfWorkerThread(text) { - if (WASM_WORKERS && PTHREADS) { - return `if (ENVIRONMENT_IS_WASM_WORKER || ENVIRONMENT_IS_PTHREAD) { ${text} }`; - } else if (WASM_WORKERS) { - return `if (ENVIRONMENT_IS_WASM_WORKER) { ${text} }`; - } else if (PTHREADS) { - return `if (ENVIRONMENT_IS_PTHREAD) { ${text} }`; + if (WASM_WORKERS || PTHREADS) { + return `if (${ENVIRONMENT_IS_WORKER_THREAD()}) { ${text} }`; } else { return ''; } @@ -1094,12 +1086,15 @@ function implicitSelf() { } function ENVIRONMENT_IS_MAIN_THREAD() { + return `(!${ENVIRONMENT_IS_WORKER_THREAD()})`; +} + +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 (AUDIO_WORKLET) envs.push('ENVIRONMENT_IS_AUDIO_WORKLET'); - if (envs.length == 0) return 'true'; - return '(!(' + envs.join('||') + '))'; + return '(' + envs.join('||') + ')'; } addToCompileTimeContext({ @@ -1120,6 +1115,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..c4c25cfb4a8d8 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -161,25 +161,12 @@ function run() { return; } -#if WASM_WORKERS - if (ENVIRONMENT_IS_WASM_WORKER) { +#if PTHREADS || WASM_WORKERS + if ({{{ ENVIRONMENT_IS_WORKER_THREAD() }}}) { #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 initRuntime(); - startWorker(Module); return; } #endif diff --git a/src/preamble.js b/src/preamble.js index 23ee003bb5592..1ca3da0389a93 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -219,7 +219,7 @@ function initRuntime() { #endif #if PTHREADS - if (ENVIRONMENT_IS_PTHREAD) return; + if (ENVIRONMENT_IS_PTHREAD) return startWorker(Module); #endif #if STACK_OVERFLOW_CHECK diff --git a/test/code_size/hello_wasm_worker_wasm.js b/test/code_size/hello_wasm_worker_wasm.js index 4e7446cc3dfa6..b1805d122420e 100644 --- a/test/code_size/hello_wasm_worker_wasm.js +++ b/test/code_size/hello_wasm_worker_wasm.js @@ -46,5 +46,5 @@ WebAssembly.instantiate(b.wasm, { h = a.h; c ? (a = b, q(a.sb, a.sz), removeEventListener("message", l), g = g.forEach(k), addEventListener("message", k)) : a.f(); - c || p(); + p(); })); \ No newline at end of file diff --git a/test/code_size/hello_wasm_worker_wasm.json b/test/code_size/hello_wasm_worker_wasm.json index 870b7172314c4..3d369c175bb03 100644 --- a/test/code_size/hello_wasm_worker_wasm.json +++ b/test/code_size/hello_wasm_worker_wasm.json @@ -1,12 +1,12 @@ { "a.html": 618, "a.html.gz": 384, - "a.js": 665, - "a.js.gz": 455, + "a.js": 662, + "a.js.gz": 452, "a.ww.js": 115, "a.ww.js.gz": 127, "a.wasm": 1881, "a.wasm.gz": 1065, - "total": 3279, - "total_gz": 2031 + "total": 3276, + "total_gz": 2028 } 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