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,