From 39141f17332fc9fec363af9a7c5e0e63738b0c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Tue, 1 Dec 2026 16:23:08 +0200 Subject: [PATCH] Keep mutable Wasm globals as WebAssembly.Global with DECLARE_ASM_MODULE_EXPORTS=0 Since #25530, mutable Wasm globals are exported to JS as WebAssembly.Global objects rather than plain numbers. This was only done in the static create_receiving path in emscripten.py; the dynamic $assignWasmExports used with DECLARE_ASM_MODULE_EXPORTS=0 still unwrapped every global into a number. Rather than embedding the names of all exported mutable globals in the JS, probe each exported global by writing back its current value: this throws for immutable WebAssembly.Global objects, which are then unwrapped to plain numbers as before. The probe is only emitted when the module exports at least one mutable global (new internal setting HAS_MUTABLE_GLOBAL_EXPORTS), so code size is unchanged in the common case. Under wasm2js, immutable globals are also exported with a setter, so when any mutable global is exported, all exported globals stay as objects. Fixes the omitexports0 part of #25550. Co-Authored-By: Claude Opus 5.5 --- src/lib/libcore.js | 15 +++++++++++---- src/settings_internal.js | 4 ++++ test/test_core.py | 1 - tools/emscripten.py | 3 +++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib/libcore.js b/src/lib/libcore.js index 7c3d9d149fd93..6f2ff0130a2ab 100644 --- a/src/lib/libcore.js +++ b/src/lib/libcore.js @@ -1720,10 +1720,17 @@ addToLibrary({ // Special handling for Wasm globals. See `create_receiving` for the // static version of this code. if (typeof exportedSymbol.value !== 'undefined') { -#if MEMORY64 - exportedSymbol = Number(exportedSymbol.value); -#else - exportedSymbol = exportedSymbol.value +#if HAS_MUTABLE_GLOBAL_EXPORTS + try { + // Throws for immutable globals, which are then unwrapped below. + // Mutable globals are left as WebAssembly.Global objects. + exportedSymbol.value = exportedSymbol.value; + } catch { +#endif + // Unwrap immutable global to a bare Number. + exportedSymbol = {{{ from64Expr('exportedSymbol.value') }}}; +#if HAS_MUTABLE_GLOBAL_EXPORTS + } #endif } #if MINIMAL_RUNTIME diff --git a/src/settings_internal.js b/src/settings_internal.js index 96f6a1170f3da..013fb3c475949 100644 --- a/src/settings_internal.js +++ b/src/settings_internal.js @@ -16,6 +16,10 @@ // underscore. var WASM_EXPORTS = []; +// Whether compiled code exports any mutable Wasm globals. These are exported +// to JS as WebAssembly.Global objects rather than as plain numbers. +var HAS_MUTABLE_GLOBAL_EXPORTS = false; + // An array of all symbols exported from all the side modules specified on the // command line. // These are raw symbol names and are not mangled to include the leading diff --git a/test/test_core.py b/test/test_core.py index 5ecbf3a4e3bf7..e2483032f5325 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -9775,7 +9775,6 @@ def test_externref_emjs(self, dynlink): 'dylink': (True,), }) @no_esm_integration('https://github.com/emscripten-core/emscripten/issues/25543') - @no_omit_asm_module_exports('https://github.com/emscripten-core/emscripten/issues/25550') def test_wasm_global(self, dynlink): if '-flto' in self.cflags or '-flto=thin' in self.cflags: self.skipTest('https://github.com/emscripten-core/emscripten/issues/25555') diff --git a/tools/emscripten.py b/tools/emscripten.py index c347f7fd59e39..a1db89686ad13 100644 --- a/tools/emscripten.py +++ b/tools/emscripten.py @@ -153,8 +153,11 @@ def update_settings_glue(wasm_file, metadata, base_metadata): if base_metadata: settings.WASM_EXPORTS = base_metadata.all_exports + other_exports = base_metadata.other_exports else: settings.WASM_EXPORTS = metadata.all_exports + other_exports = metadata.other_exports + settings.HAS_MUTABLE_GLOBAL_EXPORTS = any(e.kind == webassembly.ExternType.GLOBAL and info.mutable for e, info in other_exports) if metadata.empty_ctors and not settings.EVAL_CTORS: # There are no static constructors to run, so there is no point in calling # `__wasm_call_ctors` from JS. Unless the user asked for it to be exported,