Skip to content

Commit 2e22a8d

Browse files
committed
[esm-integration] Allow for module argument processing
This change delays module argument processing until the init function is run. This means that at least some INCOMING_MODULE_JS_API properties can be supported with ESM integration.
1 parent 9dbd397 commit 2e22a8d

File tree

79 files changed

+173
-136
lines changed

Some content is hidden

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

79 files changed

+173
-136
lines changed

Diff for: src/jsifier.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import assert from 'node:assert';
1111
import * as fs from 'node:fs/promises';
1212
import {
13+
ATPREINITS,
1314
ATEXITS,
1415
ATINITS,
1516
ATPOSTCTORS,
@@ -873,6 +874,7 @@ var proxiedFunctionTable = [
873874
asyncFuncs,
874875
libraryDefinitions: LibraryManager.libraryDefinitions,
875876
ATPRERUNS: ATPRERUNS.join('\n'),
877+
ATPREINITS: ATPREINITS.join('\n'),
876878
ATINITS: ATINITS.join('\n'),
877879
ATPOSTCTORS: ATPOSTCTORS.join('\n'),
878880
ATMAINS: ATMAINS.join('\n'),

Diff for: src/lib/libcore.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,8 @@ addToLibrary({
22832283
$wasmTable: undefined,
22842284
#endif
22852285

2286-
$noExitRuntime: "{{{ makeModuleReceiveExpr('noExitRuntime', !EXIT_RUNTIME) }}}",
2286+
$noExitRuntime__postset: () => addAtPreInit(makeModuleReceive('noExitRuntime')),
2287+
$noExitRuntime: {{{ !EXIT_RUNTIME }}},
22872288

22882289
// The following addOn<X> functions are for adding runtime callbacks at
22892290
// various executions points. Each addOn<X> function has a corresponding

Diff for: src/lib/libfs_shared.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66

77
addToLibrary({
8-
$preloadPlugins: "{{{ makeModuleReceiveExpr('preloadPlugins', '[]') }}}",
8+
$preloadPlugins__postset: () => addAtPreInit(makeModuleReceive('preloadPlugins')),
9+
$preloadPlugins: [],
910

1011
#if !MINIMAL_RUNTIME
1112
// Tries to handle an input byteArray using preload plugins. Returns true if

Diff for: src/modules.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ function exportRuntime() {
537537
}
538538
}
539539

540+
results.unshift('// Begin runtime exports')
541+
results.push('// End runtime exports')
542+
540543
return results.join('\n') + '\n';
541544
}
542545

Diff for: src/parseTools.mjs

+16-12
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,14 @@ function makeEval(code) {
720720
return ret;
721721
}
722722

723+
// Add code that runs before the wasm modules is loaded. This is the first
724+
// point at which the global `Module` object is guaranteed to exist. This hook
725+
// is mostly used to read incoming `Module` properties.
726+
export const ATPREINITS = [];
727+
function addAtPreInit(code) {
728+
ATPREINITS.push(code);
729+
}
730+
723731
// Add code to run soon after the Wasm module has been loaded. This is the first
724732
// injection point before all the other addAt<X> functions below. The code will
725733
// be executed after the runtime `onPreRuns` callbacks.
@@ -882,7 +890,7 @@ function makeModuleReceive(localName, moduleName) {
882890
if (expectToReceiveOnModule(moduleName)) {
883891
// Usually the local we use is the same as the Module property name,
884892
// but sometimes they must differ.
885-
ret = `\nif (Module['${moduleName}']) ${localName} = Module['${moduleName}'];`;
893+
ret = `if (Module['${moduleName}']) ${localName} = Module['${moduleName}'];`;
886894
}
887895
return ret;
888896
}
@@ -900,17 +908,12 @@ function makeModuleReceiveWithVar(localName, moduleName, defaultValue) {
900908
moduleName ||= localName;
901909
checkReceiving(moduleName);
902910
let ret = `var ${localName}`;
903-
if (!expectToReceiveOnModule(moduleName)) {
904-
if (defaultValue) {
905-
ret += ` = ${defaultValue}`;
906-
}
907-
ret += ';';
908-
} else {
909-
if (defaultValue) {
910-
ret += ` = Module['${moduleName}'] || ${defaultValue};`;
911-
} else {
912-
ret += ` = Module['${moduleName}'];`;
913-
}
911+
if (defaultValue) {
912+
ret += ` = ${defaultValue}`;
913+
}
914+
ret += ';';
915+
if (expectToReceiveOnModule(moduleName)) {
916+
addAtPreInit(`if (Module['${moduleName}']) ${localName} = Module['${moduleName}'];`);
914917
}
915918
return ret;
916919
}
@@ -1119,6 +1122,7 @@ addToCompileTimeContext({
11191122
ENVIRONMENT_IS_WORKER_THREAD,
11201123
addAtExit,
11211124
addAtPreRun,
1125+
addAtPreInit,
11221126
addAtInit,
11231127
addAtPostCtor,
11241128
addAtPreMain,

Diff for: src/postamble.js

+4-17
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ if (ENVIRONMENT_IS_WORKER) {
2424
#include "deterministic.js"
2525
#endif
2626

27-
{{{ exportRuntime() }}}
28-
2927
#if ASSERTIONS
3028
var calledRun;
3129
#endif
@@ -199,7 +197,7 @@ function run() {
199197
#endif
200198

201199
#if HAS_MAIN
202-
{{{ makeModuleReceiveWithVar('noInitialRun', undefined, !INVOKE_RUN) }}}
200+
var noInitialRun = {{{ makeModuleReceiveExpr('noInitialRun', !INVOKE_RUN) }}};
203201
#if MAIN_READS_PARAMS
204202
if (!noInitialRun) callMain(args);
205203
#else
@@ -287,26 +285,15 @@ function checkUnflushedContent() {
287285
#endif // EXIT_RUNTIME
288286
#endif // ASSERTIONS
289287

290-
#if expectToReceiveOnModule('preInit')
291-
if (Module['preInit']) {
292-
if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']];
293-
while (Module['preInit'].length > 0) {
294-
Module['preInit'].shift()();
295-
}
296-
}
297-
#if ASSERTIONS
298-
consumedModuleProp('preInit');
299-
#endif
300-
#endif
301-
302-
303288
#if WASM_ESM_INTEGRATION
304289
export default function init(moduleArg = {}) {
305-
// TODO(sbc): moduleArg processing
290+
Module = moduleArg;
306291
updateMemoryViews();
292+
processModuleArgs();
307293
run();
308294
}
309295
#else
296+
processModuleArgs();
310297
run();
311298
#endif
312299

Diff for: src/postamble_minimal.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66

77
// === Auto-generated postamble setup entry stuff ===
8-
{{{ exportRuntime() }}}
9-
108
#if HAS_MAIN // Only if user is exporting a C main(), we will generate a run() function that can be used to launch main.
119
function run() {
1210
#if MEMORYPROFILER
@@ -136,6 +134,10 @@ WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports)
136134
if (!Module['wasm']) throw 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM';
137135
#endif
138136

137+
<<< ATPREINITS >>>
138+
139+
{{{ exportRuntime() }}}
140+
139141
WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
140142
#endif
141143

Diff for: src/preamble.js

+51-4
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ assert(typeof Int32Array != 'undefined' && typeof Float64Array !== 'undefined' &
158158
#if IMPORTED_MEMORY
159159
// In non-standalone/normal mode, we create the memory here.
160160
#include "runtime_init_memory.js"
161-
#elif ASSERTIONS
162-
// If memory is defined in wasm, the user can't provide it, or set INITIAL_MEMORY
163-
assert(!Module['wasmMemory'], 'Use of `wasmMemory` detected. Use -sIMPORTED_MEMORY to define wasmMemory externally');
164-
assert(!Module['INITIAL_MEMORY'], 'Detected runtime INITIAL_MEMORY setting. Use -sIMPORTED_MEMORY to define wasmMemory dynamically');
165161
#endif // !IMPORTED_MEMORY && ASSERTIONS
166162

167163
#if RELOCATABLE
@@ -461,8 +457,13 @@ var FS = {
461457

462458
ErrnoError() { FS.error() },
463459
};
460+
{{{
461+
addAtPreInit(`
464462
Module['FS_createDataFile'] = FS.createDataFile;
465463
Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
464+
`);
465+
null
466+
}}}
466467
#endif
467468

468469
#if ASSERTIONS
@@ -1086,3 +1087,49 @@ function getCompilerSetting(name) {
10861087
// dynamic linker as symbols are loaded.
10871088
var asyncifyStubs = {};
10881089
#endif
1090+
1091+
function processModuleArgs() {
1092+
#if ASSERTIONS
1093+
checkIncomingModuleAPI();
1094+
#endif
1095+
1096+
<<< ATPREINITS >>>
1097+
1098+
#if expectToReceiveOnModule('preInit')
1099+
if (Module['preInit']) {
1100+
if (typeof Module['preInit'] == 'function') Module['preInit'] = [Module['preInit']];
1101+
while (Module['preInit'].length > 0) {
1102+
Module['preInit'].shift()();
1103+
}
1104+
}
1105+
#if ASSERTIONS
1106+
consumedModuleProp('preInit');
1107+
#endif
1108+
#endif
1109+
1110+
{{{ makeModuleReceive('arguments_', 'arguments') }}}
1111+
{{{ makeModuleReceive('thisProgram') }}}
1112+
1113+
1114+
#if ASSERTIONS
1115+
// Assertions on removed incoming Module JS APIs.
1116+
assert(typeof Module['memoryInitializerPrefixURL'] == 'undefined', 'Module.memoryInitializerPrefixURL option was removed, use Module.locateFile instead');
1117+
assert(typeof Module['pthreadMainPrefixURL'] == 'undefined', 'Module.pthreadMainPrefixURL option was removed, use Module.locateFile instead');
1118+
assert(typeof Module['cdInitializerPrefixURL'] == 'undefined', 'Module.cdInitializerPrefixURL option was removed, use Module.locateFile instead');
1119+
assert(typeof Module['filePackagePrefixURL'] == 'undefined', 'Module.filePackagePrefixURL option was removed, use Module.locateFile instead');
1120+
assert(typeof Module['read'] == 'undefined', 'Module.read option was removed');
1121+
assert(typeof Module['readAsync'] == 'undefined', 'Module.readAsync option was removed (modify readAsync in JS)');
1122+
assert(typeof Module['readBinary'] == 'undefined', 'Module.readBinary option was removed (modify readBinary in JS)');
1123+
assert(typeof Module['setWindowTitle'] == 'undefined', 'Module.setWindowTitle option was removed (modify emscripten_set_window_title in JS)');
1124+
assert(typeof Module['TOTAL_MEMORY'] == 'undefined', 'Module.TOTAL_MEMORY has been renamed Module.INITIAL_MEMORY');
1125+
assert(typeof Module['ENVIRONMENT'] == 'undefined', 'Module.ENVIRONMENT has been deprecated. To force the environment, use the ENVIRONMENT compile-time option (for example, -sENVIRONMENT=web or -sENVIRONMENT=node)');
1126+
assert(typeof Module['STACK_SIZE'] == 'undefined', 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')
1127+
#if !IMPORTED_MEMORY
1128+
// If memory is defined in wasm, the user can't provide it, or set INITIAL_MEMORY
1129+
assert(typeof Module['wasmMemory'] == 'undefined', 'Use of `wasmMemory` detected. Use -sIMPORTED_MEMORY to define wasmMemory externally');
1130+
assert(typeof Module['INITIAL_MEMORY'] == 'undefined', 'Detected runtime INITIAL_MEMORY setting. Use -sIMPORTED_MEMORY to define wasmMemory dynamically');
1131+
#endif
1132+
#endif // ASSERTIONS
1133+
1134+
{{{ exportRuntime() }}}
1135+
}

Diff for: src/runtime_init_memory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (!ENVIRONMENT_IS_PTHREAD) {
2121
} else
2222
#endif
2323
{
24-
{{{ makeModuleReceiveWithVar('INITIAL_MEMORY', undefined, INITIAL_MEMORY) }}}
24+
var INITIAL_MEMORY = {{{ makeModuleReceiveExpr('INITIAL_MEMORY', INITIAL_MEMORY) }}}
2525

2626
#if ASSERTIONS
2727
assert(INITIAL_MEMORY >= {{{STACK_SIZE}}}, 'INITIAL_MEMORY should be larger than STACK_SIZE, was ' + INITIAL_MEMORY + '! (STACK_SIZE=' + {{{STACK_SIZE}}} + ')');

Diff for: src/shell.js

+3-25
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// before the code. Then that object will be used in the code, and you
2222
// can continue to use Module afterwards as well.
2323
#if WASM_ESM_INTEGRATION
24-
var Module = {};
24+
var Module;
2525
#elif MODULARIZE
2626
var Module = moduleArg;
2727
#elif USE_CLOSURE_COMPILER
@@ -403,30 +403,6 @@ if (ENVIRONMENT_IS_NODE) {
403403
#endif
404404

405405
#if ASSERTIONS
406-
checkIncomingModuleAPI();
407-
#endif
408-
409-
// Emit code to handle expected values on the Module object. This applies Module.x
410-
// to the proper local x. This has two benefits: first, we only emit it if it is
411-
// expected to arrive, and second, by using a local everywhere else that can be
412-
// minified.
413-
{{{ makeModuleReceive('arguments_', 'arguments') }}}
414-
{{{ makeModuleReceive('thisProgram') }}}
415-
416-
// perform assertions in shell.js after we set up out() and err(), as otherwise if an assertion fails it cannot print the message
417-
#if ASSERTIONS
418-
// Assertions on removed incoming Module JS APIs.
419-
assert(typeof Module['memoryInitializerPrefixURL'] == 'undefined', 'Module.memoryInitializerPrefixURL option was removed, use Module.locateFile instead');
420-
assert(typeof Module['pthreadMainPrefixURL'] == 'undefined', 'Module.pthreadMainPrefixURL option was removed, use Module.locateFile instead');
421-
assert(typeof Module['cdInitializerPrefixURL'] == 'undefined', 'Module.cdInitializerPrefixURL option was removed, use Module.locateFile instead');
422-
assert(typeof Module['filePackagePrefixURL'] == 'undefined', 'Module.filePackagePrefixURL option was removed, use Module.locateFile instead');
423-
assert(typeof Module['read'] == 'undefined', 'Module.read option was removed');
424-
assert(typeof Module['readAsync'] == 'undefined', 'Module.readAsync option was removed (modify readAsync in JS)');
425-
assert(typeof Module['readBinary'] == 'undefined', 'Module.readBinary option was removed (modify readBinary in JS)');
426-
assert(typeof Module['setWindowTitle'] == 'undefined', 'Module.setWindowTitle option was removed (modify emscripten_set_window_title in JS)');
427-
assert(typeof Module['TOTAL_MEMORY'] == 'undefined', 'Module.TOTAL_MEMORY has been renamed Module.INITIAL_MEMORY');
428-
assert(typeof Module['ENVIRONMENT'] == 'undefined', 'Module.ENVIRONMENT has been deprecated. To force the environment, use the ENVIRONMENT compile-time option (for example, -sENVIRONMENT=web or -sENVIRONMENT=node)');
429-
assert(typeof Module['STACK_SIZE'] == 'undefined', 'STACK_SIZE can no longer be set at runtime. Use -sSTACK_SIZE at link time')
430406

431407
{{{ makeRemovedFSAssert('IDBFS') }}}
432408
{{{ makeRemovedFSAssert('PROXYFS') }}}
@@ -440,6 +416,8 @@ assert(typeof Module['STACK_SIZE'] == 'undefined', 'STACK_SIZE can no longer be
440416
{{{ makeRemovedFSAssert('NODEFS') }}}
441417
#endif
442418

419+
// perform assertions in shell.js after we set up out() and err(), as otherwise
420+
// if an assertion fails it cannot print the message
443421
#if PTHREADS
444422
assert(
445423
#if AUDIO_WORKLET

Diff for: test/other/codesize/test_codesize_cxx_ctors1.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8243
1+
8270

Diff for: test/other/codesize/test_codesize_cxx_ctors1.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19969
1+
20032

Diff for: test/other/codesize/test_codesize_cxx_ctors2.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8230
1+
8258

Diff for: test/other/codesize/test_codesize_cxx_ctors2.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19947
1+
20010

Diff for: test/other/codesize/test_codesize_cxx_except.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9233
1+
9261

Diff for: test/other/codesize/test_codesize_cxx_except.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
23707
1+
23770
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8187
1+
8212
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19861
1+
19925
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8187
1+
8212
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19861
1+
19925

Diff for: test/other/codesize/test_codesize_cxx_lto.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8259
1+
8281

Diff for: test/other/codesize/test_codesize_cxx_lto.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20043
1+
20107

Diff for: test/other/codesize/test_codesize_cxx_mangle.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9272
1+
9300

Diff for: test/other/codesize/test_codesize_cxx_mangle.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
23821
1+
23884
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8243
1+
8270
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19969
1+
20032

Diff for: test/other/codesize/test_codesize_cxx_wasmfs.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3424
1+
3442

Diff for: test/other/codesize/test_codesize_cxx_wasmfs.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7366
1+
7428

Diff for: test/other/codesize/test_codesize_files_js_fs.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7540
1+
7563

Diff for: test/other/codesize/test_codesize_files_js_fs.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18547
1+
18615
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2681
1+
2704
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5749
1+
5814

Diff for: test/other/codesize/test_codesize_hello_O0.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7775
1+
7783

Diff for: test/other/codesize/test_codesize_hello_O0.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20699
1+
20815

Diff for: test/other/codesize/test_codesize_hello_O1.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2546
1+
2573

Diff for: test/other/codesize/test_codesize_hello_O1.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6580
1+
6657

Diff for: test/other/codesize/test_codesize_hello_O2.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2202
1+
2226

Diff for: test/other/codesize/test_codesize_hello_O2.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4542
1+
4607

Diff for: test/other/codesize/test_codesize_hello_O3.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2161
1+
2178

Diff for: test/other/codesize/test_codesize_hello_O3.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4484
1+
4549

Diff for: test/other/codesize/test_codesize_hello_Os.gzsize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2161
1+
2178

Diff for: test/other/codesize/test_codesize_hello_Os.jssize

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4484
1+
4549

0 commit comments

Comments
 (0)