Skip to content

Commit 4283c50

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 61e6b14 commit 4283c50

File tree

61 files changed

+134
-153
lines changed

Some content is hidden

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

61 files changed

+134
-153
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.
2020

2121
3.1.43 (in development)
2222
-----------------------
23+
- The `asm` proporty of the Module object (which olds the exports of the wasm
24+
module) has been removed by default. Internally, this is now known as
25+
`wasmExports`.
2326
- Handling i64 arguments and return values in JS functions is now much simpler
2427
with the new `__i53abi` decorator. When this is set to true, i64 values are
2528
automatically converted to JS numbers (i53) at the JS boundary. Parameters

emscripten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def install_wrapper(sym):
771771
# With assertions disabled the wrapper will replace the global var and Module var on
772772
# first use.
773773
wrapper += f'''function() {{
774-
return ({mangled} = {exported}Module['asm']['{name}']).apply(null, arguments);
774+
return ({mangled} = {exported}wasmExports['{name}']).apply(null, arguments);
775775
}};
776776
'''
777777
else:

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
@@ -336,7 +336,7 @@ var LibraryExceptions = {
336336
#if RELOCATABLE
337337
return ___cpp_exception; // defined in library.js
338338
#else
339-
return Module['asm']['__cpp_exception'];
339+
return wasmExports['__cpp_exception'];
340340
#endif
341341
},
342342

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/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: 11 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.
@@ -468,7 +469,7 @@ function abort(what) {
468469
#if WASM_EXCEPTIONS == 1
469470
// See above, in the meantime, we resort to wasm code for trapping.
470471
//
471-
// In case abort() is called before the module is initialized, Module['asm']
472+
// In case abort() is called before the module is initialized, wasmExports
472473
// and its exported '__trap' function is not available, in which case we throw
473474
// a RuntimeError.
474475
//
@@ -524,7 +525,7 @@ function createExportWrapper(name, fixedasm) {
524525
var displayName = name;
525526
var asm = fixedasm;
526527
if (!fixedasm) {
527-
asm = Module['asm'];
528+
asm = wasmExports;
528529
}
529530
assert(runtimeInitialized, 'native function `' + displayName + '` called before runtime initialization');
530531
#if EXIT_RUNTIME
@@ -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,18 @@ function createWasm() {
979980
exports = applySignatureConversions(exports);
980981
#endif
981982

982-
Module['asm'] = exports;
983+
wasmExports = exports;
983984

984985
#if PTHREADS
985986
#if MAIN_MODULE
986-
registerTLSInit(Module['asm']['_emscripten_tls_init'], instance.exports, metadata);
987+
registerTLSInit(wasmExports['_emscripten_tls_init'], instance.exports, metadata);
987988
#else
988-
registerTLSInit(Module['asm']['_emscripten_tls_init']);
989+
registerTLSInit(wasmExports['_emscripten_tls_init']);
989990
#endif
990991
#endif
991992

992993
#if !IMPORTED_MEMORY
993-
wasmMemory = Module['asm']['memory'];
994+
wasmMemory = wasmExports['memory'];
994995
#if ASSERTIONS
995996
assert(wasmMemory, "memory not found in wasm exports");
996997
// This assertion doesn't hold when emscripten is run in --post-link
@@ -1005,7 +1006,7 @@ function createWasm() {
10051006
#endif
10061007

10071008
#if !RELOCATABLE
1008-
wasmTable = Module['asm']['__indirect_function_table'];
1009+
wasmTable = wasmExports['__indirect_function_table'];
10091010
#if ASSERTIONS && !PURE_WASI
10101011
assert(wasmTable, "table not found in wasm exports");
10111012
#endif
@@ -1019,11 +1020,11 @@ function createWasm() {
10191020
#endif
10201021

10211022
#if hasExportedSymbol('__wasm_call_ctors')
1022-
addOnInit(Module['asm']['__wasm_call_ctors']);
1023+
addOnInit(wasmExports['__wasm_call_ctors']);
10231024
#endif
10241025

10251026
#if hasExportedSymbol('__wasm_apply_data_relocs')
1026-
__RELOC_FUNCS__.push(Module['asm']['__wasm_apply_data_relocs']);
1027+
__RELOC_FUNCS__.push(wasmExports['__wasm_apply_data_relocs']);
10271028
#endif
10281029

10291030
#if ABORT_ON_WASM_EXCEPTIONS

src/shell.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ assert(typeof Module['readAsync'] == 'undefined', 'Module.readAsync option was r
492492
assert(typeof Module['readBinary'] == 'undefined', 'Module.readBinary option was removed (modify readBinary in JS)');
493493
assert(typeof Module['setWindowTitle'] == 'undefined', 'Module.setWindowTitle option was removed (modify setWindowTitle in JS)');
494494
assert(typeof Module['TOTAL_MEMORY'] == 'undefined', 'Module.TOTAL_MEMORY has been renamed Module.INITIAL_MEMORY');
495+
{{{ makeRemovedModuleAPIAssert('arm', 'wasmExports') }}}
495496
{{{ makeRemovedModuleAPIAssert('read', 'read_') }}}
496497
{{{ makeRemovedModuleAPIAssert('readAsync') }}}
497498
{{{ makeRemovedModuleAPIAssert('readBinary') }}}

test/optimizer/applyDCEGraphRemovals-output.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ var expD5 = asm["expD5"];
1818
var expD6;
1919

2020
var expI1 = Module["expI1"] = function() {
21-
return (expI1 = Module["expI1"] = Module["asm"]["expI1"]).apply(null, arguments);
21+
return (expI1 = Module["expI1"] = wasmExports["expI1"]).apply(null, arguments);
2222
};
2323

2424
var expI2 = Module["expI2"] = function() {
25-
return (expI2 = Module["expI2"] = Module["asm"]["expI2"]).apply(null, arguments);
25+
return (expI2 = Module["expI2"] = wasmExports["expI2"]).apply(null, arguments);
2626
};
2727

2828
var expI3 = Module["expI3"] = function() {
29-
return (expI3 = Module["expI3"] = Module["asm"]["expI3"]).apply(null, arguments);
29+
return (expI3 = Module["expI3"] = wasmExports["expI3"]).apply(null, arguments);
3030
};
3131

3232
var expI4;
3333

3434
var expI5 = function() {
35-
return (expI5 = Module["asm"]["expI5"]).apply(null, arguments);
35+
return (expI5 = wasmExports["expI5"]).apply(null, arguments);
3636
};
3737

3838
var expI6;

0 commit comments

Comments
 (0)