Skip to content

Commit 00e9f24

Browse files
committed
Replace Module["asm"] with wasmExports global
This brings the regular runtime closer `MINIMAL_RUNTIME` which uses the global `asm` to refer to exports object. As a followup we should consider merging these two symbols.
1 parent 353d9ee commit 00e9f24

File tree

65 files changed

+191
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+191
-172
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ See docs/process.md for more on how version tagging works.
2727
- The internal `read_` and `readAsync` functions no longer handle date URIs.
2828
This only effects builds that use `-sSINGLE_FILE` or `--memory-init-file`.
2929
(#19792)
30+
- The `asm` property of the Module object (which held the raw exports of the
31+
wasm module) has been removed. Internally, this is now accessed via the
32+
`wasmExports` global. If necessary, it is possible to export `wasmExports`
33+
on the Module object using `-sEXPORTED_RUNTIME_METHODS=wasmExports`. (#19816)
3034

3135
3.1.43 - 07/10/23
3236
-----------------

emscripten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def install_wrapper(sym):
762762
# first use.
763763
args = [f'a{i}' for i in range(nargs)]
764764
args = ', '.join(args)
765-
wrapper += f"({args}) => ({mangled} = {exported}Module['asm']['{name}'])({args});"
765+
wrapper += f"({args}) => ({mangled} = {exported}wasmExports['{name}'])({args});"
766766
else:
767767
wrapper += 'asm["%s"]' % name
768768

site/source/docs/optimizing/Module-Splitting.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ included in the profile.
124124
Here’s the function to write the profile and our new main function::
125125

126126
EM_JS(void, write_profile, (), {
127-
var __write_profile = Module['asm']['__write_profile'];
127+
var __write_profile = wasmExports['__write_profile'];
128128
if (__write_profile) {
129129

130130
// Get the size of the profile and allocate a buffer for it.
@@ -338,7 +338,7 @@ be called either.
338338

339339
When eagerly instantiating the secondary module, the imports object should be::
340340

341-
{'primary': Module['asm']}
341+
{'primary': wasmExports}
342342

343343
Debugging
344344
---------

src/library_async.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ mergeInto(LibraryManager.library, {
267267
getDataRewindFunc: function(ptr) {
268268
var id = {{{ makeGetValue('ptr', C_STRUCTS.asyncify_data_s.rewind_id, 'i32') }}};
269269
var name = Asyncify.callStackIdToName[id];
270-
var func = Module['asm'][name];
270+
var func = wasmExports[name];
271271
#if RELOCATABLE
272-
// Exported functions in side modules are not listed in `Module['asm']`,
272+
// Exported functions in side modules are not listed in `wasmExports`,
273273
// So we should use `resolveGlobalSymbol` helper function, which is defined in `library_dylink.js`.
274274
if (!func) {
275275
func = resolveGlobalSymbol(name, false).sym;
@@ -522,8 +522,8 @@ mergeInto(LibraryManager.library, {
522522
_load_secondary_module__sig: 'v',
523523
_load_secondary_module: async function() {
524524
// Mark the module as loading for the wasm module (so it doesn't try to load it again).
525-
Module['asm']['load_secondary_module_status'].value = 1;
526-
var imports = {'primary': Module['asm']};
525+
wasmExports['load_secondary_module_status'].value = 1;
526+
var imports = {'primary': wasmExports};
527527
// Replace '.wasm' suffix with '.deferred.wasm'.
528528
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm';
529529
await new Promise((resolve) => {

src/library_exceptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ var LibraryExceptions = {
331331
#if RELOCATABLE
332332
return ___cpp_exception; // defined in library.js
333333
#else
334-
return Module['asm']['__cpp_exception'];
334+
return wasmExports['__cpp_exception'];
335335
#endif
336336
},
337337

src/library_exports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mergeInto(LibraryManager.library, {
1616
var exportedFunc = asm[name];
1717
#else
1818
// In regular runtime, exports are available on the Module object.
19-
var exportedFunc = Module['asm'][name];
19+
var exportedFunc = wasmExports[name];
2020
#endif
2121
if (exportedFunc) {
2222
// Record the created function pointer to each function object,

src/modules.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ function exportRuntime() {
382382
'abort',
383383
'keepRuntimeAlive',
384384
'wasmMemory',
385+
'wasmTable',
386+
'wasmExports',
385387
];
386388

387389
// These are actually native wasm functions these days but we allow exporting

src/parseTools.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,13 @@ function hasExportedSymbol(sym) {
865865
return WASM_EXPORTS.has(sym);
866866
}
867867

868+
function receivedSymbol(sym) {
869+
if (EXPORTED_RUNTIME_METHODS.includes(sym)) {
870+
return `Module['${sym}'] = ${sym};`
871+
}
872+
return '';
873+
}
874+
868875
// JS API I64 param handling: if we have BigInt support, the ABI is simple,
869876
// it is a BigInt. Otherwise, we legalize into pairs of i32s.
870877
function defineI64Param(name) {

src/postamble_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
142142

143143
#if !LibraryManager.has('library_exports.js') && !EMBIND
144144
// If not using the emscripten_get_exported_function() API or embind, keep the
145-
// 'asm' exports variable in local scope to this instantiate function to save
145+
// `asm` exports variable in local scope to this instantiate function to save
146146
// code size. (otherwise access it without to export it to outer scope)
147147
var
148148
#endif

src/preamble.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ if (typeof WebAssembly != 'object') {
5353
// Wasm globals
5454

5555
var wasmMemory;
56+
var wasmExports;
5657

5758
#if SHARED_MEMORY
5859
// For sending to workers.
@@ -471,7 +472,7 @@ function abort(what) {
471472
#if WASM_EXCEPTIONS == 1
472473
// See above, in the meantime, we resort to wasm code for trapping.
473474
//
474-
// In case abort() is called before the module is initialized, Module['asm']
475+
// In case abort() is called before the module is initialized, wasmExports
475476
// and its exported '__trap' function is not available, in which case we throw
476477
// a RuntimeError.
477478
//
@@ -527,7 +528,7 @@ function createExportWrapper(name) {
527528
#if EXIT_RUNTIME
528529
assert(!runtimeExited, `native function \`${name}\` called after runtime exit (use NO_EXIT_RUNTIME to keep it alive after main() exits)`);
529530
#endif
530-
var f = Module['asm'][name];
531+
var f = wasmExports[name];
531532
assert(f, `exported native function \`${name}\` not found`);
532533
return f.apply(null, arguments);
533534
};
@@ -714,7 +715,7 @@ var splitModuleProxyHandler = {
714715
throw new Error('Placeholder function "' + prop + '" should not be called when using JSPI.');
715716
#else
716717
err('placeholder function called: ' + prop);
717-
var imports = {'primary': Module['asm']};
718+
var imports = {'primary': wasmExports};
718719
// Replace '.wasm' suffix with '.deferred.wasm'.
719720
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm'
720721
loadSplitModule(deferred, imports, prop);
@@ -979,18 +980,20 @@ function createWasm() {
979980
exports = applySignatureConversions(exports);
980981
#endif
981982

982-
Module['asm'] = exports;
983+
wasmExports = exports;
984+
{{{ receivedSymbol('wasmExports') }}}
983985

984986
#if PTHREADS
985987
#if MAIN_MODULE
986-
registerTLSInit(Module['asm']['_emscripten_tls_init'], instance.exports, metadata);
988+
registerTLSInit(wasmExports['_emscripten_tls_init'], instance.exports, metadata);
987989
#else
988-
registerTLSInit(Module['asm']['_emscripten_tls_init']);
990+
registerTLSInit(wasmExports['_emscripten_tls_init']);
989991
#endif
990992
#endif
991993

992994
#if !IMPORTED_MEMORY
993-
wasmMemory = Module['asm']['memory'];
995+
wasmMemory = wasmExports['memory'];
996+
{{{ receivedSymbol('wasmMemory') }}}
994997
#if ASSERTIONS
995998
assert(wasmMemory, "memory not found in wasm exports");
996999
// This assertion doesn't hold when emscripten is run in --post-link
@@ -1005,7 +1008,8 @@ function createWasm() {
10051008
#endif
10061009

10071010
#if !RELOCATABLE
1008-
wasmTable = Module['asm']['__indirect_function_table'];
1011+
wasmTable = wasmExports['__indirect_function_table'];
1012+
{{{ receivedSymbol('wasmTable') }}}
10091013
#if ASSERTIONS && !PURE_WASI
10101014
assert(wasmTable, "table not found in wasm exports");
10111015
#endif
@@ -1019,11 +1023,11 @@ function createWasm() {
10191023
#endif
10201024

10211025
#if hasExportedSymbol('__wasm_call_ctors')
1022-
addOnInit(Module['asm']['__wasm_call_ctors']);
1026+
addOnInit(wasmExports['__wasm_call_ctors']);
10231027
#endif
10241028

10251029
#if hasExportedSymbol('__wasm_apply_data_relocs')
1026-
__RELOC_FUNCS__.push(Module['asm']['__wasm_apply_data_relocs']);
1030+
__RELOC_FUNCS__.push(wasmExports['__wasm_apply_data_relocs']);
10271031
#endif
10281032

10291033
#if ABORT_ON_WASM_EXCEPTIONS

0 commit comments

Comments
 (0)