From 476868d4e2a00627ca8e8df5990a10cbcff9c843 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 30 Jun 2026 22:53:59 -0700 Subject: [PATCH 1/2] Emit source phase import for MODULARIZE=instance In MODULARIZE=instance mode the modularize() wrapper is not applied, so the top-level 'import source wasmModule' statement in src/modularize.js was never emitted, leaving preamble.js referencing an undefined wasmModule. Emit the source phase import from preamble.js (module scope in instance mode), next to where wasmModule is declared and used, so that -sSOURCE_PHASE_IMPORTS works alongside -sMODULARIZE=instance/EXPORT_ES6. --- src/preamble.js | 7 ++++++- test/test_other.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/preamble.js b/src/preamble.js index d03f77c880588..7585eaef04c2a 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -41,7 +41,12 @@ if (!globalThis.WebAssembly) { // Wasm globals -#if SHARED_MEMORY +#if SOURCE_PHASE_IMPORTS && MODULARIZE == 'instance' +// In MODULARIZE=instance mode the output is itself an ES module (it is not +// wrapped by modularize.js), so the source phase import is emitted here at +// module scope, next to where wasmModule is used. +import source wasmModule from './{{{ WASM_BINARY_FILE }}}'; +#elif SHARED_MEMORY // For sending to workers. var wasmModule; #endif // SHARED_MEMORY diff --git a/test/test_other.py b/test/test_other.py index 6c8c26e4906d7..8edf887dee810 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -440,6 +440,18 @@ def test_esm_source_phase_imports(self, args): self.assertContained('import source wasmModule from', read_file('hello_world.mjs')) self.assertContained('Hello, world!', self.run_js('hello_world.mjs')) + @requires_node_25 + @parameterized({ + '': ([],), + 'O3': (['-O3'],), + }) + def test_esm_source_phase_imports_instance(self, args): + self.set_setting('SOURCE_PHASE_IMPORTS') + self.set_setting('MODULARIZE', 'instance') + self.cflags += ['-Wno-experimental'] + args + self.do_runf('hello_world.c', 'Hello, world!') + self.assertContained('import source wasmModule from', read_file(self.output_name('hello_world'))) + @parameterized({ '': ([],), 'node': (['-sENVIRONMENT=node'],), From ef110e96d8fec9edca7c366443942d2520de53c5 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 2 Jul 2026 11:34:57 -0700 Subject: [PATCH 2/2] codesize --- test/codesize/test_codesize_minimal_O0.expected.js | 2 +- test/codesize/test_unoptimized_code_size.json | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/codesize/test_codesize_minimal_O0.expected.js b/test/codesize/test_codesize_minimal_O0.expected.js index d51078ecef6c7..2768cbc816e18 100644 --- a/test/codesize/test_codesize_minimal_O0.expected.js +++ b/test/codesize/test_codesize_minimal_O0.expected.js @@ -20,7 +20,7 @@ // globalThis because older browsers might not have globalThis defined. // We skip the node version checking when running on Bun/Deno since the node - // version they report is not completely. + // version they report doesn't seem to be useful. if (typeof process !== 'undefined' && !process.versions?.bun && typeof Deno == "undefined") { var currentNodeVersion = process.versions?.node ? humanReadableVersionToPacked(process.versions.node) : TARGET_NOT_SUPPORTED; if (currentNodeVersion < 180300) { diff --git a/test/codesize/test_unoptimized_code_size.json b/test/codesize/test_unoptimized_code_size.json index cdd575261b0d5..ef39819ba2646 100644 --- a/test/codesize/test_unoptimized_code_size.json +++ b/test/codesize/test_unoptimized_code_size.json @@ -1,16 +1,16 @@ { - "hello_world.js": 56114, - "hello_world.js.gz": 17666, + "hello_world.js": 56122, + "hello_world.js.gz": 17671, "hello_world.wasm": 15115, "hello_world.wasm.gz": 7464, "no_asserts.js": 25585, "no_asserts.js.gz": 8688, "no_asserts.wasm": 12229, "no_asserts.wasm.gz": 6004, - "strict.js": 53216, - "strict.js.gz": 16637, + "strict.js": 53224, + "strict.js.gz": 16642, "strict.wasm": 15115, "strict.wasm.gz": 7461, - "total": 177374, - "total_gz": 63920 + "total": 177390, + "total_gz": 63930 }