From 53bed91a92e6c39afda459919e75f221cb9131c7 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 9 Jun 2026 13:01:04 -0700 Subject: [PATCH] Use await for loadDylibs This avoids the need for addRunDependency/removeRunDependency. --- src/lib/libdylink.js | 4 +--- src/preamble.js | 16 ++++++++-------- src/runtime_pthread.js | 4 ++-- test/codesize/test_codesize_hello_dylink.json | 8 ++++---- .../codesize/test_codesize_hello_dylink_all.json | 4 ++-- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/lib/libdylink.js b/src/lib/libdylink.js index 2cb4a896d081d..30438454eefc7 100644 --- a/src/lib/libdylink.js +++ b/src/lib/libdylink.js @@ -1235,7 +1235,7 @@ var LibraryDylink = { }, $loadDylibs__internal: true, - $loadDylibs__deps: ['$loadDynamicLibrary', '$reportUndefinedSymbols', '$addRunDependency', '$removeRunDependency'], + $loadDylibs__deps: ['$loadDynamicLibrary', '$reportUndefinedSymbols'], $loadDylibs: async () => { if (!dynamicLibraries.length) { #if DYLINK_DEBUG @@ -1248,7 +1248,6 @@ var LibraryDylink = { #if DYLINK_DEBUG dbg('loadDylibs:', dynamicLibraries); #endif - addRunDependency('loadDylibs'); // Load binaries asynchronously for (var lib of dynamicLibraries) { @@ -1260,7 +1259,6 @@ var LibraryDylink = { #if DYLINK_DEBUG dbg('loadDylibs done!'); #endif - removeRunDependency('loadDylibs'); }, // void* dlopen(const char* filename, int flags); diff --git a/src/preamble.js b/src/preamble.js index 1cff33aaf2d68..97f6366910344 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -716,7 +716,7 @@ function getWasmImports() { // handle a generated wasm instance, receiving its exports and // performing other necessary setup /** @param {WebAssembly.Module=} module*/ - function receiveInstance(instance, module) { + {{{ asyncIf(MAIN_MODULE) }}}function receiveInstance(instance, module) { #if RUNTIME_DEBUG dbg('receiveInstance') #endif @@ -776,21 +776,21 @@ function getWasmImports() { Module['wasmExports'] = wasmExports; #endif +#if !IMPORTED_MEMORY + updateMemoryViews(); +#endif + #if MAIN_MODULE #if '$LDSO' in addedLibraryItems LDSO.init(); #endif - loadDylibs(); + await loadDylibs(); #endif #if ABORT_ON_WASM_EXCEPTIONS instrumentWasmTableWithAbort(); #endif -#if !IMPORTED_MEMORY - updateMemoryViews(); -#endif - #if PTHREADS || WASM_WORKERS // We now have the Wasm module loaded up, keep a reference to the compiled module so we can post it to the workers. wasmModule = module; @@ -864,7 +864,7 @@ function getWasmImports() { #if SOURCE_PHASE_IMPORTS var instance = await WebAssembly.instantiate(wasmModule, info); - var exports = receiveInstantiationResult({instance, 'module':wasmModule}); + var exports = {{{ awaitIf(MAIN_MODULE) }}}receiveInstantiationResult({instance, 'module':wasmModule}); return exports; #else wasmBinaryFile ??= findWasmBinary(); @@ -873,7 +873,7 @@ function getWasmImports() { dbg('asynchronously preparing wasm'); #endif var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info); - var exports = receiveInstantiationResult(result); + var exports = {{{ awaitIf(MAIN_MODULE) }}}receiveInstantiationResult(result); return exports; #else // WASM_ASYNC_COMPILATION var result = instantiateSync(wasmBinaryFile, info); diff --git a/src/runtime_pthread.js b/src/runtime_pthread.js index de70f2460d5c9..9423a22822487 100644 --- a/src/runtime_pthread.js +++ b/src/runtime_pthread.js @@ -42,7 +42,7 @@ if (ENVIRONMENT_IS_PTHREAD) { // notified about them. self.onunhandledrejection = (e) => { throw e.reason || e; }; - {{{ asyncIf(ASYNCIFY == 2) }}}function handleMessage(e) { + {{{ asyncIf(ASYNCIFY == 2 || MAIN_MODULE) }}}function handleMessage(e) { try { var msgData = e.data; //dbg('msgData: ' + Object.keys(msgData)); @@ -123,7 +123,7 @@ if (ENVIRONMENT_IS_PTHREAD) { #if MODULARIZE == 'instance' init(); #else - createWasm(); + {{{ awaitIf(MAIN_MODULE) }}}createWasm(); run(); #endif #endif // MINIMAL_RUNTIME diff --git a/test/codesize/test_codesize_hello_dylink.json b/test/codesize/test_codesize_hello_dylink.json index 13c13029343ec..f654bb7291bba 100644 --- a/test/codesize/test_codesize_hello_dylink.json +++ b/test/codesize/test_codesize_hello_dylink.json @@ -1,10 +1,10 @@ { - "a.out.js": 26397, - "a.out.js.gz": 11245, + "a.out.js": 26265, + "a.out.js.gz": 11182, "a.out.nodebug.wasm": 17861, "a.out.nodebug.wasm.gz": 9019, - "total": 44258, - "total_gz": 20264, + "total": 44126, + "total_gz": 20201, "sent": [ "__syscall_stat64", "emscripten_resize_heap", diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index ca6d3ba83aa7d..3b3d33f3aff90 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 268126, + "a.out.js": 268089, "a.out.nodebug.wasm": 587575, - "total": 855701, + "total": 855664, "sent": [ "IMG_Init", "IMG_Load",