From b330159b412819b632bf20f26db37bed1d8b2aa5 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 18 Dec 2024 17:25:55 -0800 Subject: [PATCH] Convert `createWasm` async/await where possible (#23157) The advantage if using `await` in the cases where we can is that it avoids the generation or wrapper function for each export. So instead of: ``` var wasmExport = createWasm(); // returns empty object ... var malloc = (..) => (malloc = wasmExports['malloc'])(..); ``` We can generate: ``` var wasmExport = await createWasm(); // returns actual exports ... var malloc = wasmExports['malloc']; ``` This only currently works in MODULARIZE mode where the code is running inside a factory function. Otherwise it would end up using top-level-await. One wrinkle here is that this is not currently supported when closure is enabled because we run closure only on the contents of the factory function so closure ends up seeing this as a top level await when its not. There are two minor observable effects of this change: 1. In `MODULARIZE` mode its no longer possible to call `new Foo()` with factory function. We already added a debug error for in #23210. 2. Because we now can `await` for createWasm to return, the `run` method can run on first call, which means it runs `main` on first call, which means main will now run before `postjs` code, just like it would with sync instantiation. --- ChangeLog.md | 6 ++++++ src/preamble.js | 13 ++++++------- test/browser/test_small_js_flags.js.size | 2 +- test/code_size/hello_webgl2_wasm.json | 8 ++++---- test/code_size/hello_webgl2_wasm2js.json | 8 ++++---- test/code_size/hello_webgl_wasm.json | 8 ++++---- test/code_size/hello_webgl_wasm2js.json | 8 ++++---- .../codesize/test_codesize_cxx_ctors1.gzsize | 2 +- .../codesize/test_codesize_cxx_ctors1.jssize | 2 +- .../codesize/test_codesize_cxx_ctors2.gzsize | 2 +- .../codesize/test_codesize_cxx_ctors2.jssize | 2 +- .../codesize/test_codesize_cxx_except.gzsize | 2 +- .../codesize/test_codesize_cxx_except.jssize | 2 +- .../test_codesize_cxx_except_wasm.jssize | 2 +- ...test_codesize_cxx_except_wasm_exnref.jssize | 2 +- .../codesize/test_codesize_cxx_lto.gzsize | 2 +- .../codesize/test_codesize_cxx_lto.jssize | 2 +- .../codesize/test_codesize_cxx_mangle.gzsize | 2 +- .../codesize/test_codesize_cxx_mangle.jssize | 2 +- .../codesize/test_codesize_cxx_noexcept.gzsize | 2 +- .../codesize/test_codesize_cxx_noexcept.jssize | 2 +- .../codesize/test_codesize_cxx_wasmfs.gzsize | 2 +- .../codesize/test_codesize_cxx_wasmfs.jssize | 2 +- .../codesize/test_codesize_files_js_fs.gzsize | 2 +- .../codesize/test_codesize_files_js_fs.jssize | 2 +- .../codesize/test_codesize_files_wasmfs.jssize | 2 +- .../codesize/test_codesize_hello_O0.gzsize | 2 +- .../codesize/test_codesize_hello_O0.jssize | 2 +- .../codesize/test_codesize_hello_O1.gzsize | 2 +- .../codesize/test_codesize_hello_O1.jssize | 2 +- .../codesize/test_codesize_hello_O2.gzsize | 2 +- .../codesize/test_codesize_hello_O2.jssize | 2 +- .../codesize/test_codesize_hello_O3.gzsize | 2 +- .../codesize/test_codesize_hello_O3.jssize | 2 +- .../codesize/test_codesize_hello_Os.gzsize | 2 +- .../codesize/test_codesize_hello_Os.jssize | 2 +- .../codesize/test_codesize_hello_Oz.gzsize | 2 +- .../codesize/test_codesize_hello_Oz.jssize | 2 +- .../codesize/test_codesize_hello_dylink.gzsize | 2 +- .../codesize/test_codesize_hello_dylink.jssize | 2 +- .../test_codesize_hello_export_nothing.gzsize | 2 +- .../test_codesize_hello_export_nothing.jssize | 2 +- .../codesize/test_codesize_hello_wasmfs.gzsize | 2 +- .../codesize/test_codesize_hello_wasmfs.jssize | 2 +- .../test_codesize_libcxxabi_message_O3.gzsize | 2 +- .../test_codesize_libcxxabi_message_O3.jssize | 2 +- ...size_libcxxabi_message_O3_standalone.gzsize | 2 +- ...size_libcxxabi_message_O3_standalone.jssize | 2 +- .../other/codesize/test_codesize_mem_O3.gzsize | 2 +- .../other/codesize/test_codesize_mem_O3.jssize | 2 +- .../codesize/test_codesize_mem_O3_grow.gzsize | 2 +- .../codesize/test_codesize_mem_O3_grow.jssize | 2 +- ...test_codesize_mem_O3_grow_standalone.gzsize | 2 +- ...test_codesize_mem_O3_grow_standalone.jssize | 2 +- .../test_codesize_mem_O3_standalone.gzsize | 2 +- .../test_codesize_mem_O3_standalone.jssize | 2 +- .../test_codesize_mem_O3_standalone_lib.gzsize | 2 +- .../test_codesize_mem_O3_standalone_lib.jssize | 2 +- ...test_codesize_mem_O3_standalone_narg.gzsize | 2 +- ...test_codesize_mem_O3_standalone_narg.jssize | 2 +- ...codesize_mem_O3_standalone_narg_flto.gzsize | 2 +- ...codesize_mem_O3_standalone_narg_flto.jssize | 2 +- .../codesize/test_codesize_minimal_64.gzsize | 2 +- .../codesize/test_codesize_minimal_64.jssize | 2 +- .../codesize/test_codesize_minimal_O1.gzsize | 2 +- .../codesize/test_codesize_minimal_O1.jssize | 2 +- .../codesize/test_codesize_minimal_O2.gzsize | 2 +- .../codesize/test_codesize_minimal_O2.jssize | 2 +- .../codesize/test_codesize_minimal_O3.gzsize | 2 +- .../codesize/test_codesize_minimal_O3.jssize | 2 +- .../codesize/test_codesize_minimal_Os.gzsize | 2 +- .../codesize/test_codesize_minimal_Os.jssize | 2 +- .../test_codesize_minimal_Oz-ctors.jssize | 2 +- .../codesize/test_codesize_minimal_Oz.gzsize | 2 +- .../codesize/test_codesize_minimal_Oz.jssize | 2 +- .../codesize/test_codesize_minimal_esm.gzsize | 2 +- .../codesize/test_codesize_minimal_esm.jssize | 2 +- .../test_codesize_minimal_pthreads.gzsize | 2 +- .../test_codesize_minimal_pthreads.jssize | 2 +- .../test_codesize_minimal_wasmfs.gzsize | 2 +- .../test_codesize_minimal_wasmfs.jssize | 2 +- test/other/test_INCOMING_MODULE_JS_API.js.size | 2 +- test/other/test_unoptimized_code_size.js.size | 2 +- ...st_unoptimized_code_size_no_asserts.js.size | 2 +- .../test_unoptimized_code_size_strict.js.size | 2 +- test/test_unicode_js_library.out | 2 +- tools/emscripten.py | 18 ++++++++++++++++-- tools/link.py | 14 ++++++++------ 88 files changed, 132 insertions(+), 111 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 7bbade9d415c2..8400bcaa9a81d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -33,6 +33,12 @@ See docs/process.md for more on how version tagging works. `PATH.basename("a/b/..")` returns `".."` instead of `"a"`. This is in line with the behaviour of both node and coreutils, and is already the case when using NODERAWFS". (#23180) +- The factory function exposed in `-sMODULARIZE` mode is now marked as `async` + when `WASM_ASYNC_COMPILATION` is enabled (the default). This allows us to use + `await` during module creation. One side effect of this is that code in + `--post-js` files will now be delayed until after module creation and after + `main` runs. This matches the existing behaviour when using sync instantation + (`-sWASM_ASYNC_COMPILATION=0`) but is an observable difference. (#23157) 3.1.74 - 12/14/24 ----------------- diff --git a/src/preamble.js b/src/preamble.js index 4f4dc32dba0ca..2e91449ac76fc 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -1036,11 +1036,11 @@ function getWasmImports() { trueModule = null; #endif #if SHARED_MEMORY || RELOCATABLE - receiveInstance(result['instance'], result['module']); + return receiveInstance(result['instance'], result['module']); #else // TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193, the above line no longer optimizes out down to the following line. // When the regression is fixed, can restore the above PTHREADS-enabled path. - receiveInstance(result['instance']); + return receiveInstance(result['instance']); #endif } #endif // WASM_ASYNC_COMPILATION @@ -1076,8 +1076,7 @@ function getWasmImports() { // Instantiate from the module posted from the main thread. // We can just use sync instantiation in the worker. var instance = new WebAssembly.Instance(module, getWasmImports()); - receiveInstance(instance, module); - resolve(); + resolve(receiveInstance(instance, module)); }; }); } @@ -1095,16 +1094,16 @@ function getWasmImports() { try { #endif var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info); - receiveInstantiationResult(result); + var exports = receiveInstantiationResult(result); #if LOAD_SOURCE_MAP receiveSourceMapJSON(await getSourceMapAsync()); #endif - return result; + return exports; #if MODULARIZE } catch (e) { // If instantiation fails, reject the module ready promise. readyPromiseReject(e); - return; + return Promise.reject(e); } #endif #else // WASM_ASYNC_COMPILATION diff --git a/test/browser/test_small_js_flags.js.size b/test/browser/test_small_js_flags.js.size index 6502cf0229698..bce5ffdd8fabd 100644 --- a/test/browser/test_small_js_flags.js.size +++ b/test/browser/test_small_js_flags.js.size @@ -1 +1 @@ -4380 +4374 diff --git a/test/code_size/hello_webgl2_wasm.json b/test/code_size/hello_webgl2_wasm.json index 13919b590d957..d7738773ebc50 100644 --- a/test/code_size/hello_webgl2_wasm.json +++ b/test/code_size/hello_webgl2_wasm.json @@ -1,10 +1,10 @@ { "a.html": 454, "a.html.gz": 328, - "a.js": 4532, - "a.js.gz": 2315, + "a.js": 4538, + "a.js.gz": 2320, "a.wasm": 10402, "a.wasm.gz": 6703, - "total": 15388, - "total_gz": 9346 + "total": 15394, + "total_gz": 9351 } diff --git a/test/code_size/hello_webgl2_wasm2js.json b/test/code_size/hello_webgl2_wasm2js.json index d54f14a652c99..da83c09580bbe 100644 --- a/test/code_size/hello_webgl2_wasm2js.json +++ b/test/code_size/hello_webgl2_wasm2js.json @@ -1,8 +1,8 @@ { "a.html": 346, "a.html.gz": 262, - "a.js": 22200, - "a.js.gz": 11583, - "total": 22546, - "total_gz": 11845 + "a.js": 22206, + "a.js.gz": 11589, + "total": 22552, + "total_gz": 11851 } diff --git a/test/code_size/hello_webgl_wasm.json b/test/code_size/hello_webgl_wasm.json index e619b3f3321c3..e4c3af111aac8 100644 --- a/test/code_size/hello_webgl_wasm.json +++ b/test/code_size/hello_webgl_wasm.json @@ -1,10 +1,10 @@ { "a.html": 454, "a.html.gz": 328, - "a.js": 4070, - "a.js.gz": 2158, + "a.js": 4076, + "a.js.gz": 2163, "a.wasm": 10402, "a.wasm.gz": 6703, - "total": 14926, - "total_gz": 9189 + "total": 14932, + "total_gz": 9194 } diff --git a/test/code_size/hello_webgl_wasm2js.json b/test/code_size/hello_webgl_wasm2js.json index a01758dd16374..c2cb2b2deec67 100644 --- a/test/code_size/hello_webgl_wasm2js.json +++ b/test/code_size/hello_webgl_wasm2js.json @@ -1,8 +1,8 @@ { "a.html": 346, "a.html.gz": 262, - "a.js": 21726, - "a.js.gz": 11413, - "total": 22072, - "total_gz": 11675 + "a.js": 21732, + "a.js.gz": 11419, + "total": 22078, + "total_gz": 11681 } diff --git a/test/other/codesize/test_codesize_cxx_ctors1.gzsize b/test/other/codesize/test_codesize_cxx_ctors1.gzsize index 629d814f19953..7c05c8455e746 100644 --- a/test/other/codesize/test_codesize_cxx_ctors1.gzsize +++ b/test/other/codesize/test_codesize_cxx_ctors1.gzsize @@ -1 +1 @@ -8352 +8351 diff --git a/test/other/codesize/test_codesize_cxx_ctors1.jssize b/test/other/codesize/test_codesize_cxx_ctors1.jssize index a22508fe1954b..82d8332d892e9 100644 --- a/test/other/codesize/test_codesize_cxx_ctors1.jssize +++ b/test/other/codesize/test_codesize_cxx_ctors1.jssize @@ -1 +1 @@ -20347 +20343 diff --git a/test/other/codesize/test_codesize_cxx_ctors2.gzsize b/test/other/codesize/test_codesize_cxx_ctors2.gzsize index 66543c9213fe0..e7f2f2aa09e70 100644 --- a/test/other/codesize/test_codesize_cxx_ctors2.gzsize +++ b/test/other/codesize/test_codesize_cxx_ctors2.gzsize @@ -1 +1 @@ -8336 +8334 diff --git a/test/other/codesize/test_codesize_cxx_ctors2.jssize b/test/other/codesize/test_codesize_cxx_ctors2.jssize index ee93d493df28c..f8c47ad8dd2c0 100644 --- a/test/other/codesize/test_codesize_cxx_ctors2.jssize +++ b/test/other/codesize/test_codesize_cxx_ctors2.jssize @@ -1 +1 @@ -20315 +20311 diff --git a/test/other/codesize/test_codesize_cxx_except.gzsize b/test/other/codesize/test_codesize_cxx_except.gzsize index 1e8ce7e708846..26f242e26fb55 100644 --- a/test/other/codesize/test_codesize_cxx_except.gzsize +++ b/test/other/codesize/test_codesize_cxx_except.gzsize @@ -1 +1 @@ -9358 +9356 diff --git a/test/other/codesize/test_codesize_cxx_except.jssize b/test/other/codesize/test_codesize_cxx_except.jssize index 108fb27854780..c495dc33171d4 100644 --- a/test/other/codesize/test_codesize_cxx_except.jssize +++ b/test/other/codesize/test_codesize_cxx_except.jssize @@ -1 +1 @@ -24116 +24112 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm.jssize b/test/other/codesize/test_codesize_cxx_except_wasm.jssize index b4fb6fa3b6ed0..276f80508b53d 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm.jssize +++ b/test/other/codesize/test_codesize_cxx_except_wasm.jssize @@ -1 +1 @@ -20240 +20236 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize index b4fb6fa3b6ed0..276f80508b53d 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize +++ b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.jssize @@ -1 +1 @@ -20240 +20236 diff --git a/test/other/codesize/test_codesize_cxx_lto.gzsize b/test/other/codesize/test_codesize_cxx_lto.gzsize index 7947190fcc750..a665dbc2e401e 100644 --- a/test/other/codesize/test_codesize_cxx_lto.gzsize +++ b/test/other/codesize/test_codesize_cxx_lto.gzsize @@ -1 +1 @@ -8348 +8347 diff --git a/test/other/codesize/test_codesize_cxx_lto.jssize b/test/other/codesize/test_codesize_cxx_lto.jssize index 6525ff13db521..cd0660982b7d0 100644 --- a/test/other/codesize/test_codesize_cxx_lto.jssize +++ b/test/other/codesize/test_codesize_cxx_lto.jssize @@ -1 +1 @@ -20371 +20367 diff --git a/test/other/codesize/test_codesize_cxx_mangle.gzsize b/test/other/codesize/test_codesize_cxx_mangle.gzsize index be24a385bb8fd..886a28e6c91b7 100644 --- a/test/other/codesize/test_codesize_cxx_mangle.gzsize +++ b/test/other/codesize/test_codesize_cxx_mangle.gzsize @@ -1 +1 @@ -9361 +9359 diff --git a/test/other/codesize/test_codesize_cxx_mangle.jssize b/test/other/codesize/test_codesize_cxx_mangle.jssize index 108fb27854780..c495dc33171d4 100644 --- a/test/other/codesize/test_codesize_cxx_mangle.jssize +++ b/test/other/codesize/test_codesize_cxx_mangle.jssize @@ -1 +1 @@ -24116 +24112 diff --git a/test/other/codesize/test_codesize_cxx_noexcept.gzsize b/test/other/codesize/test_codesize_cxx_noexcept.gzsize index 629d814f19953..7c05c8455e746 100644 --- a/test/other/codesize/test_codesize_cxx_noexcept.gzsize +++ b/test/other/codesize/test_codesize_cxx_noexcept.gzsize @@ -1 +1 @@ -8352 +8351 diff --git a/test/other/codesize/test_codesize_cxx_noexcept.jssize b/test/other/codesize/test_codesize_cxx_noexcept.jssize index a22508fe1954b..82d8332d892e9 100644 --- a/test/other/codesize/test_codesize_cxx_noexcept.jssize +++ b/test/other/codesize/test_codesize_cxx_noexcept.jssize @@ -1 +1 @@ -20347 +20343 diff --git a/test/other/codesize/test_codesize_cxx_wasmfs.gzsize b/test/other/codesize/test_codesize_cxx_wasmfs.gzsize index 799c77152539d..384632eff7fe6 100644 --- a/test/other/codesize/test_codesize_cxx_wasmfs.gzsize +++ b/test/other/codesize/test_codesize_cxx_wasmfs.gzsize @@ -1 +1 @@ -3653 +3651 diff --git a/test/other/codesize/test_codesize_cxx_wasmfs.jssize b/test/other/codesize/test_codesize_cxx_wasmfs.jssize index 5e97e16bfa535..122c645d8cd89 100644 --- a/test/other/codesize/test_codesize_cxx_wasmfs.jssize +++ b/test/other/codesize/test_codesize_cxx_wasmfs.jssize @@ -1 +1 @@ -7914 +7910 diff --git a/test/other/codesize/test_codesize_files_js_fs.gzsize b/test/other/codesize/test_codesize_files_js_fs.gzsize index a6589474f60cb..309e97c7a9f6c 100644 --- a/test/other/codesize/test_codesize_files_js_fs.gzsize +++ b/test/other/codesize/test_codesize_files_js_fs.gzsize @@ -1 +1 @@ -7656 +7654 diff --git a/test/other/codesize/test_codesize_files_js_fs.jssize b/test/other/codesize/test_codesize_files_js_fs.jssize index bf4d7a5e763f4..79d0a5b02487f 100644 --- a/test/other/codesize/test_codesize_files_js_fs.jssize +++ b/test/other/codesize/test_codesize_files_js_fs.jssize @@ -1 +1 @@ -18842 +18838 diff --git a/test/other/codesize/test_codesize_files_wasmfs.jssize b/test/other/codesize/test_codesize_files_wasmfs.jssize index 2c55bb009ea6c..d665f60e5a1bc 100644 --- a/test/other/codesize/test_codesize_files_wasmfs.jssize +++ b/test/other/codesize/test_codesize_files_wasmfs.jssize @@ -1 +1 @@ -6202 +6198 diff --git a/test/other/codesize/test_codesize_hello_O0.gzsize b/test/other/codesize/test_codesize_hello_O0.gzsize index 42b603432aefe..f36af27d0621f 100644 --- a/test/other/codesize/test_codesize_hello_O0.gzsize +++ b/test/other/codesize/test_codesize_hello_O0.gzsize @@ -1 +1 @@ -8017 +8012 diff --git a/test/other/codesize/test_codesize_hello_O0.jssize b/test/other/codesize/test_codesize_hello_O0.jssize index ca3736fbbb012..0c10608bd8f7e 100644 --- a/test/other/codesize/test_codesize_hello_O0.jssize +++ b/test/other/codesize/test_codesize_hello_O0.jssize @@ -1 +1 @@ -21597 +21588 diff --git a/test/other/codesize/test_codesize_hello_O1.gzsize b/test/other/codesize/test_codesize_hello_O1.gzsize index 690c09f1c702d..8933964377fbf 100644 --- a/test/other/codesize/test_codesize_hello_O1.gzsize +++ b/test/other/codesize/test_codesize_hello_O1.gzsize @@ -1 +1 @@ -2697 +2694 diff --git a/test/other/codesize/test_codesize_hello_O1.jssize b/test/other/codesize/test_codesize_hello_O1.jssize index 8d2e9dfdb7ff4..a25b0436b3627 100644 --- a/test/other/codesize/test_codesize_hello_O1.jssize +++ b/test/other/codesize/test_codesize_hello_O1.jssize @@ -1 +1 @@ -6911 +6899 diff --git a/test/other/codesize/test_codesize_hello_O2.gzsize b/test/other/codesize/test_codesize_hello_O2.gzsize index 7d945b944dabc..da797840923f0 100644 --- a/test/other/codesize/test_codesize_hello_O2.gzsize +++ b/test/other/codesize/test_codesize_hello_O2.gzsize @@ -1 +1 @@ -2359 +2357 diff --git a/test/other/codesize/test_codesize_hello_O2.jssize b/test/other/codesize/test_codesize_hello_O2.jssize index 11336821115bf..7e8c8fed96c82 100644 --- a/test/other/codesize/test_codesize_hello_O2.jssize +++ b/test/other/codesize/test_codesize_hello_O2.jssize @@ -1 +1 @@ -4835 +4831 diff --git a/test/other/codesize/test_codesize_hello_O3.gzsize b/test/other/codesize/test_codesize_hello_O3.gzsize index 3e263d2e556cd..616e2787a51d3 100644 --- a/test/other/codesize/test_codesize_hello_O3.gzsize +++ b/test/other/codesize/test_codesize_hello_O3.gzsize @@ -1 +1 @@ -2298 +2296 diff --git a/test/other/codesize/test_codesize_hello_O3.jssize b/test/other/codesize/test_codesize_hello_O3.jssize index c6a2b270f3b7e..3658ed8053766 100644 --- a/test/other/codesize/test_codesize_hello_O3.jssize +++ b/test/other/codesize/test_codesize_hello_O3.jssize @@ -1 +1 @@ -4757 +4753 diff --git a/test/other/codesize/test_codesize_hello_Os.gzsize b/test/other/codesize/test_codesize_hello_Os.gzsize index 3e263d2e556cd..616e2787a51d3 100644 --- a/test/other/codesize/test_codesize_hello_Os.gzsize +++ b/test/other/codesize/test_codesize_hello_Os.gzsize @@ -1 +1 @@ -2298 +2296 diff --git a/test/other/codesize/test_codesize_hello_Os.jssize b/test/other/codesize/test_codesize_hello_Os.jssize index c6a2b270f3b7e..3658ed8053766 100644 --- a/test/other/codesize/test_codesize_hello_Os.jssize +++ b/test/other/codesize/test_codesize_hello_Os.jssize @@ -1 +1 @@ -4757 +4753 diff --git a/test/other/codesize/test_codesize_hello_Oz.gzsize b/test/other/codesize/test_codesize_hello_Oz.gzsize index 9294c7754fde1..20c65f21df41d 100644 --- a/test/other/codesize/test_codesize_hello_Oz.gzsize +++ b/test/other/codesize/test_codesize_hello_Oz.gzsize @@ -1 +1 @@ -2281 +2279 diff --git a/test/other/codesize/test_codesize_hello_Oz.jssize b/test/other/codesize/test_codesize_hello_Oz.jssize index 65e5941a842c5..4f652d136a346 100644 --- a/test/other/codesize/test_codesize_hello_Oz.jssize +++ b/test/other/codesize/test_codesize_hello_Oz.jssize @@ -1 +1 @@ -4724 +4720 diff --git a/test/other/codesize/test_codesize_hello_dylink.gzsize b/test/other/codesize/test_codesize_hello_dylink.gzsize index 416368238e9f1..45a2573f0aa4b 100644 --- a/test/other/codesize/test_codesize_hello_dylink.gzsize +++ b/test/other/codesize/test_codesize_hello_dylink.gzsize @@ -1 +1 @@ -6023 +6028 diff --git a/test/other/codesize/test_codesize_hello_dylink.jssize b/test/other/codesize/test_codesize_hello_dylink.jssize index 3e898f1efeb39..c7ce89ced910f 100644 --- a/test/other/codesize/test_codesize_hello_dylink.jssize +++ b/test/other/codesize/test_codesize_hello_dylink.jssize @@ -1 +1 @@ -13300 +13317 diff --git a/test/other/codesize/test_codesize_hello_export_nothing.gzsize b/test/other/codesize/test_codesize_hello_export_nothing.gzsize index 889f56ca7ed18..357a340dae7ae 100644 --- a/test/other/codesize/test_codesize_hello_export_nothing.gzsize +++ b/test/other/codesize/test_codesize_hello_export_nothing.gzsize @@ -1 +1 @@ -1682 +1681 diff --git a/test/other/codesize/test_codesize_hello_export_nothing.jssize b/test/other/codesize/test_codesize_hello_export_nothing.jssize index ee72a28badb95..00987dc4e5708 100644 --- a/test/other/codesize/test_codesize_hello_export_nothing.jssize +++ b/test/other/codesize/test_codesize_hello_export_nothing.jssize @@ -1 +1 @@ -3637 +3633 diff --git a/test/other/codesize/test_codesize_hello_wasmfs.gzsize b/test/other/codesize/test_codesize_hello_wasmfs.gzsize index 3e263d2e556cd..616e2787a51d3 100644 --- a/test/other/codesize/test_codesize_hello_wasmfs.gzsize +++ b/test/other/codesize/test_codesize_hello_wasmfs.gzsize @@ -1 +1 @@ -2298 +2296 diff --git a/test/other/codesize/test_codesize_hello_wasmfs.jssize b/test/other/codesize/test_codesize_hello_wasmfs.jssize index c6a2b270f3b7e..3658ed8053766 100644 --- a/test/other/codesize/test_codesize_hello_wasmfs.jssize +++ b/test/other/codesize/test_codesize_hello_wasmfs.jssize @@ -1 +1 @@ -4757 +4753 diff --git a/test/other/codesize/test_codesize_libcxxabi_message_O3.gzsize b/test/other/codesize/test_codesize_libcxxabi_message_O3.gzsize index 1ea816877702e..988fde4112c52 100644 --- a/test/other/codesize/test_codesize_libcxxabi_message_O3.gzsize +++ b/test/other/codesize/test_codesize_libcxxabi_message_O3.gzsize @@ -1 +1 @@ -1876 +1873 diff --git a/test/other/codesize/test_codesize_libcxxabi_message_O3.jssize b/test/other/codesize/test_codesize_libcxxabi_message_O3.jssize index 26a4dd33f0015..1263586a20af3 100644 --- a/test/other/codesize/test_codesize_libcxxabi_message_O3.jssize +++ b/test/other/codesize/test_codesize_libcxxabi_message_O3.jssize @@ -1 +1 @@ -4000 +3996 diff --git a/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.gzsize b/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.gzsize index a7568adae3387..23e07fb71b3f9 100644 --- a/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.gzsize +++ b/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.gzsize @@ -1 +1 @@ -1914 +1912 diff --git a/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.jssize b/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.jssize index 965493a318306..24b2d1ddd384c 100644 --- a/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.jssize +++ b/test/other/codesize/test_codesize_libcxxabi_message_O3_standalone.jssize @@ -1 +1 @@ -4048 +4044 diff --git a/test/other/codesize/test_codesize_mem_O3.gzsize b/test/other/codesize/test_codesize_mem_O3.gzsize index ffcd151c41f04..a4e114468e7e2 100644 --- a/test/other/codesize/test_codesize_mem_O3.gzsize +++ b/test/other/codesize/test_codesize_mem_O3.gzsize @@ -1 +1 @@ -2328 +2327 diff --git a/test/other/codesize/test_codesize_mem_O3.jssize b/test/other/codesize/test_codesize_mem_O3.jssize index 86a2a1bb17b2e..a155f5afbd071 100644 --- a/test/other/codesize/test_codesize_mem_O3.jssize +++ b/test/other/codesize/test_codesize_mem_O3.jssize @@ -1 +1 @@ -4892 +4888 diff --git a/test/other/codesize/test_codesize_mem_O3_grow.gzsize b/test/other/codesize/test_codesize_mem_O3_grow.gzsize index 721cbb13b4aee..9c251745dda72 100644 --- a/test/other/codesize/test_codesize_mem_O3_grow.gzsize +++ b/test/other/codesize/test_codesize_mem_O3_grow.gzsize @@ -1 +1 @@ -2478 +2476 diff --git a/test/other/codesize/test_codesize_mem_O3_grow.jssize b/test/other/codesize/test_codesize_mem_O3_grow.jssize index 5d539952599b9..cd147334a1386 100644 --- a/test/other/codesize/test_codesize_mem_O3_grow.jssize +++ b/test/other/codesize/test_codesize_mem_O3_grow.jssize @@ -1 +1 @@ -5177 +5172 diff --git a/test/other/codesize/test_codesize_mem_O3_grow_standalone.gzsize b/test/other/codesize/test_codesize_mem_O3_grow_standalone.gzsize index a7ffd709f6523..d1fc75efc83f6 100644 --- a/test/other/codesize/test_codesize_mem_O3_grow_standalone.gzsize +++ b/test/other/codesize/test_codesize_mem_O3_grow_standalone.gzsize @@ -1 +1 @@ -2180 +2177 diff --git a/test/other/codesize/test_codesize_mem_O3_grow_standalone.jssize b/test/other/codesize/test_codesize_mem_O3_grow_standalone.jssize index d6dab2a5d3793..5b9e54fbbe923 100644 --- a/test/other/codesize/test_codesize_mem_O3_grow_standalone.jssize +++ b/test/other/codesize/test_codesize_mem_O3_grow_standalone.jssize @@ -1 +1 @@ -4585 +4581 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone.gzsize b/test/other/codesize/test_codesize_mem_O3_standalone.gzsize index 5cc2201e666e5..1ed292725657e 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone.gzsize +++ b/test/other/codesize/test_codesize_mem_O3_standalone.gzsize @@ -1 +1 @@ -2143 +2140 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone.jssize b/test/other/codesize/test_codesize_mem_O3_standalone.jssize index 4d4bc2f8db86e..570a753145afa 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone.jssize +++ b/test/other/codesize/test_codesize_mem_O3_standalone.jssize @@ -1 +1 @@ -4517 +4513 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone_lib.gzsize b/test/other/codesize/test_codesize_mem_O3_standalone_lib.gzsize index 62a85b862fd04..3964950a079db 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone_lib.gzsize +++ b/test/other/codesize/test_codesize_mem_O3_standalone_lib.gzsize @@ -1 +1 @@ -1900 +1898 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone_lib.jssize b/test/other/codesize/test_codesize_mem_O3_standalone_lib.jssize index 61050f20447d0..4a3a63211e267 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone_lib.jssize +++ b/test/other/codesize/test_codesize_mem_O3_standalone_lib.jssize @@ -1 +1 @@ -4047 +4043 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone_narg.gzsize b/test/other/codesize/test_codesize_mem_O3_standalone_narg.gzsize index a7568adae3387..23e07fb71b3f9 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone_narg.gzsize +++ b/test/other/codesize/test_codesize_mem_O3_standalone_narg.gzsize @@ -1 +1 @@ -1914 +1912 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone_narg.jssize b/test/other/codesize/test_codesize_mem_O3_standalone_narg.jssize index 965493a318306..24b2d1ddd384c 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone_narg.jssize +++ b/test/other/codesize/test_codesize_mem_O3_standalone_narg.jssize @@ -1 +1 @@ -4048 +4044 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.gzsize b/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.gzsize index a7568adae3387..23e07fb71b3f9 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.gzsize +++ b/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.gzsize @@ -1 +1 @@ -1914 +1912 diff --git a/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.jssize b/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.jssize index 965493a318306..24b2d1ddd384c 100644 --- a/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.jssize +++ b/test/other/codesize/test_codesize_mem_O3_standalone_narg_flto.jssize @@ -1 +1 @@ -4048 +4044 diff --git a/test/other/codesize/test_codesize_minimal_64.gzsize b/test/other/codesize/test_codesize_minimal_64.gzsize index 2a63ccf651217..4d851a7800ac5 100644 --- a/test/other/codesize/test_codesize_minimal_64.gzsize +++ b/test/other/codesize/test_codesize_minimal_64.gzsize @@ -1 +1 @@ -1439 +1441 diff --git a/test/other/codesize/test_codesize_minimal_64.jssize b/test/other/codesize/test_codesize_minimal_64.jssize index 31410c0cab693..9bcc9f43c3d2c 100644 --- a/test/other/codesize/test_codesize_minimal_64.jssize +++ b/test/other/codesize/test_codesize_minimal_64.jssize @@ -1 +1 @@ -3064 +3058 diff --git a/test/other/codesize/test_codesize_minimal_O1.gzsize b/test/other/codesize/test_codesize_minimal_O1.gzsize index 45ff3acb5f914..effd6e7865f0d 100644 --- a/test/other/codesize/test_codesize_minimal_O1.gzsize +++ b/test/other/codesize/test_codesize_minimal_O1.gzsize @@ -1 +1 @@ -1531 +1534 diff --git a/test/other/codesize/test_codesize_minimal_O1.jssize b/test/other/codesize/test_codesize_minimal_O1.jssize index f76268c8b2ac3..496991636dc10 100644 --- a/test/other/codesize/test_codesize_minimal_O1.jssize +++ b/test/other/codesize/test_codesize_minimal_O1.jssize @@ -1 +1 @@ -3713 +3702 diff --git a/test/other/codesize/test_codesize_minimal_O2.gzsize b/test/other/codesize/test_codesize_minimal_O2.gzsize index a3d3785badfdf..1eea322546e5a 100644 --- a/test/other/codesize/test_codesize_minimal_O2.gzsize +++ b/test/other/codesize/test_codesize_minimal_O2.gzsize @@ -1 +1 @@ -1380 +1381 diff --git a/test/other/codesize/test_codesize_minimal_O2.jssize b/test/other/codesize/test_codesize_minimal_O2.jssize index e88d628aa7ae4..f06c1c5aa9471 100644 --- a/test/other/codesize/test_codesize_minimal_O2.jssize +++ b/test/other/codesize/test_codesize_minimal_O2.jssize @@ -1 +1 @@ -2812 +2806 diff --git a/test/other/codesize/test_codesize_minimal_O3.gzsize b/test/other/codesize/test_codesize_minimal_O3.gzsize index cd692fa07fb1d..79abba8435fb0 100644 --- a/test/other/codesize/test_codesize_minimal_O3.gzsize +++ b/test/other/codesize/test_codesize_minimal_O3.gzsize @@ -1 +1 @@ -1346 +1348 diff --git a/test/other/codesize/test_codesize_minimal_O3.jssize b/test/other/codesize/test_codesize_minimal_O3.jssize index 9f876b5c8336c..0ece43ab5c071 100644 --- a/test/other/codesize/test_codesize_minimal_O3.jssize +++ b/test/other/codesize/test_codesize_minimal_O3.jssize @@ -1 +1 @@ -2762 +2756 diff --git a/test/other/codesize/test_codesize_minimal_Os.gzsize b/test/other/codesize/test_codesize_minimal_Os.gzsize index cd692fa07fb1d..79abba8435fb0 100644 --- a/test/other/codesize/test_codesize_minimal_Os.gzsize +++ b/test/other/codesize/test_codesize_minimal_Os.gzsize @@ -1 +1 @@ -1346 +1348 diff --git a/test/other/codesize/test_codesize_minimal_Os.jssize b/test/other/codesize/test_codesize_minimal_Os.jssize index 9f876b5c8336c..0ece43ab5c071 100644 --- a/test/other/codesize/test_codesize_minimal_Os.jssize +++ b/test/other/codesize/test_codesize_minimal_Os.jssize @@ -1 +1 @@ -2762 +2756 diff --git a/test/other/codesize/test_codesize_minimal_Oz-ctors.jssize b/test/other/codesize/test_codesize_minimal_Oz-ctors.jssize index 0e4b8c8ebc97d..32dec52aa5d71 100644 --- a/test/other/codesize/test_codesize_minimal_Oz-ctors.jssize +++ b/test/other/codesize/test_codesize_minimal_Oz-ctors.jssize @@ -1 +1 @@ -2747 +2741 diff --git a/test/other/codesize/test_codesize_minimal_Oz.gzsize b/test/other/codesize/test_codesize_minimal_Oz.gzsize index cd692fa07fb1d..79abba8435fb0 100644 --- a/test/other/codesize/test_codesize_minimal_Oz.gzsize +++ b/test/other/codesize/test_codesize_minimal_Oz.gzsize @@ -1 +1 @@ -1346 +1348 diff --git a/test/other/codesize/test_codesize_minimal_Oz.jssize b/test/other/codesize/test_codesize_minimal_Oz.jssize index 9f876b5c8336c..0ece43ab5c071 100644 --- a/test/other/codesize/test_codesize_minimal_Oz.jssize +++ b/test/other/codesize/test_codesize_minimal_Oz.jssize @@ -1 +1 @@ -2762 +2756 diff --git a/test/other/codesize/test_codesize_minimal_esm.gzsize b/test/other/codesize/test_codesize_minimal_esm.gzsize index 9639a4ce0392a..499020422191c 100644 --- a/test/other/codesize/test_codesize_minimal_esm.gzsize +++ b/test/other/codesize/test_codesize_minimal_esm.gzsize @@ -1 +1 @@ -1496 +1509 diff --git a/test/other/codesize/test_codesize_minimal_esm.jssize b/test/other/codesize/test_codesize_minimal_esm.jssize index 46b6da4851e58..7f7703d078eb4 100644 --- a/test/other/codesize/test_codesize_minimal_esm.jssize +++ b/test/other/codesize/test_codesize_minimal_esm.jssize @@ -1 +1 @@ -3138 +3157 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.gzsize b/test/other/codesize/test_codesize_minimal_pthreads.gzsize index 5255d8eda08aa..025479c9c2a8b 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.gzsize +++ b/test/other/codesize/test_codesize_minimal_pthreads.gzsize @@ -1 +1 @@ -4184 +4190 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.jssize b/test/other/codesize/test_codesize_minimal_pthreads.jssize index c9e63f57e5ed2..7a53a34d4eeef 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.jssize +++ b/test/other/codesize/test_codesize_minimal_pthreads.jssize @@ -1 +1 @@ -8711 +8728 diff --git a/test/other/codesize/test_codesize_minimal_wasmfs.gzsize b/test/other/codesize/test_codesize_minimal_wasmfs.gzsize index cd692fa07fb1d..79abba8435fb0 100644 --- a/test/other/codesize/test_codesize_minimal_wasmfs.gzsize +++ b/test/other/codesize/test_codesize_minimal_wasmfs.gzsize @@ -1 +1 @@ -1346 +1348 diff --git a/test/other/codesize/test_codesize_minimal_wasmfs.jssize b/test/other/codesize/test_codesize_minimal_wasmfs.jssize index 9f876b5c8336c..0ece43ab5c071 100644 --- a/test/other/codesize/test_codesize_minimal_wasmfs.jssize +++ b/test/other/codesize/test_codesize_minimal_wasmfs.jssize @@ -1 +1 @@ -2762 +2756 diff --git a/test/other/test_INCOMING_MODULE_JS_API.js.size b/test/other/test_INCOMING_MODULE_JS_API.js.size index 791d82540e015..8151f378a5913 100644 --- a/test/other/test_INCOMING_MODULE_JS_API.js.size +++ b/test/other/test_INCOMING_MODULE_JS_API.js.size @@ -1 +1 @@ -3772 +3766 diff --git a/test/other/test_unoptimized_code_size.js.size b/test/other/test_unoptimized_code_size.js.size index 6defabfd2bdbe..e904f31f54481 100644 --- a/test/other/test_unoptimized_code_size.js.size +++ b/test/other/test_unoptimized_code_size.js.size @@ -1 +1 @@ -53866 +53888 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 a1bca43a18bc5..a66da51014b62 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 @@ -29065 +29087 diff --git a/test/other/test_unoptimized_code_size_strict.js.size b/test/other/test_unoptimized_code_size_strict.js.size index 2bc64bf3c0783..aee5ffc51afd6 100644 --- a/test/other/test_unoptimized_code_size_strict.js.size +++ b/test/other/test_unoptimized_code_size_strict.js.size @@ -1 +1 @@ -52649 +52671 diff --git a/test/test_unicode_js_library.out b/test/test_unicode_js_library.out index c96bed51b0188..aa42466399fb6 100644 --- a/test/test_unicode_js_library.out +++ b/test/test_unicode_js_library.out @@ -1,2 +1,2 @@ -Unicode postjs: àČšñéáúÍųåêâăščếệüçλληνικάбългарскиPусскийСрпскиУкраїнська한국어中文普通话(中国大陆)普通话(香港)中文(台灣)粵語(香港)日本語हिन्दीภาษาไทย Unicode snowman ☃ says hello! àČšñéáúÍųåêâăščếệüçλληνικάбългарскиPусскийСрпскиУкраїнська한국어中文普通话(中国大陆)普通话(香港)中文(台灣)粵語(香港)日本語हिन्दीภาษาไทย +Unicode postjs: àČšñéáúÍųåêâăščếệüçλληνικάбългарскиPусскийСрпскиУкраїнська한국어中文普通话(中国大陆)普通话(香港)中文(台灣)粵語(香港)日本語हिन्दीภาษาไทย diff --git a/tools/emscripten.py b/tools/emscripten.py index 250432f5462f7..0db61d81e9c61 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -893,6 +893,16 @@ def create_sending(metadata, library_symbols): return '{\n ' + ',\n '.join(elems) + '\n}' +def can_use_await(): + # In MODULARIZE mode we can use `await` since the factory function itself + # is marked as `async` and the generated code all lives inside that factory + # function. + # However, because closure does not see this (it runs only on the inner code), + # it sees this as a top-level-await, which it does not yet support. + # FIXME(https://github.com/emscripten-core/emscripten/issues/23158) + return settings.MODULARIZE and not settings.USE_CLOSURE_COMPILER + + def make_export_wrappers(function_exports): assert not settings.MINIMAL_RUNTIME @@ -932,7 +942,7 @@ def install_wrapper(sym): # With assertions enabled we create a wrapper that are calls get routed through, for # the lifetime of the program. wrapper += f"createExportWrapper('{name}', {nargs});" - elif settings.WASM_ASYNC_COMPILATION or settings.PTHREADS: + elif (settings.WASM_ASYNC_COMPILATION and not can_use_await()) or settings.PTHREADS: # With WASM_ASYNC_COMPILATION wrapper will replace the global var and Module var on # first use. args = [f'a{i}' for i in range(nargs)] @@ -998,7 +1008,11 @@ def create_module(receiving, metadata, global_exports, library_symbols): if not settings.MINIMAL_RUNTIME: if settings.WASM_ASYNC_COMPILATION: - module.append("var wasmExports;\ncreateWasm();\n") + if can_use_await(): + # In modularize mode the generated code is within a factory function. + module.append("var wasmExports = await createWasm();\n") + else: + module.append("var wasmExports;\ncreateWasm();\n") else: module.append("var wasmExports = createWasm();\n") diff --git a/tools/link.py b/tools/link.py index 2d7b6c603d7fb..fe10af00857ef 100644 --- a/tools/link.py +++ b/tools/link.py @@ -2392,13 +2392,14 @@ def modularize(): # When targetting node and ES6 we use `await import ..` in the generated code # so the outer function needs to be marked as async. - async_emit = '' - if settings.EXPORT_ES6 and settings.ENVIRONMENT_MAY_BE_NODE: - async_emit = 'async ' + if settings.WASM_ASYNC_COMPILATION or (settings.EXPORT_ES6 and settings.ENVIRONMENT_MAY_BE_NODE): + maybe_async = 'async ' + else: + maybe_async = '' if settings.MODULARIZE == 'instance': wrapper_function = ''' -export default async function init(moduleArg = {}) { +export default %(maybe_async)s function init(moduleArg = {}) { var moduleRtn; %(generated_js)s @@ -2406,7 +2407,8 @@ def modularize(): return moduleRtn; } ''' % { - 'generated_js': generated_js + 'generated_js': generated_js, + 'maybe_async': maybe_async, } else: wrapper_function = ''' @@ -2418,7 +2420,7 @@ def modularize(): return moduleRtn; } ''' % { - 'maybe_async': async_emit, + 'maybe_async': maybe_async, 'generated_js': generated_js }