Skip to content

Commit 2245635

Browse files
authored
Remove the extra closure in MODULARIZE=instance mode (#24073)
This mode now uses the same technique developed for `WASM_ESM_INTEGRATRION` where we delay the processing `moduleArgs` and the assignment to the Module object.
1 parent 0994edc commit 2245635

14 files changed

+72
-83
lines changed

src/jsifier.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ function(${args}) {
736736
contentText = `var ${mangled} = ${snippet};`;
737737
}
738738

739-
if (WASM_ESM_INTEGRATION && (EXPORT_ALL || EXPORTED_FUNCTIONS.has(mangled)) && !isStub) {
740-
// It ESM integration mode we mark JS library symbols are exported at
739+
if (MODULARIZE == 'instance' && (EXPORT_ALL || EXPORTED_FUNCTIONS.has(mangled)) && !isStub) {
740+
// It MODULARIZE=instance mode mark JS library symbols are exported at
741741
// the point of declaration.
742742
contentText = 'export ' + contentText;
743743
}
@@ -860,7 +860,7 @@ var proxiedFunctionTable = [
860860
includeFile(fileName);
861861
}
862862

863-
if (MODULARIZE && !WASM_ESM_INTEGRATION) {
863+
if (MODULARIZE && MODULARIZE != 'instance') {
864864
includeSystemFile('postamble_modularize.js');
865865
}
866866

src/modules.mjs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,10 @@ function addMissingLibraryStubs(unusedLibSymbols) {
406406
}
407407

408408
function exportSymbol(name) {
409-
if (WASM_ESM_INTEGRATION) {
410-
// In ESM integration mode symbols are exported by being included in
411-
// an export { foo, bar } list so we build up the simple list of names
412-
return name;
413-
}
409+
// In MODULARIZE=instance mode symbols are exported by being included in
410+
// an export { foo, bar } list so we build up the simple list of names
414411
if (MODULARIZE === 'instance') {
415-
return `__exp_${name} = ${name};`;
412+
return name;
416413
}
417414
return `Module['${name}'] = ${name};`;
418415
}
@@ -423,9 +420,9 @@ function exportRuntimeSymbols() {
423420
function maybeExport(name) {
424421
// If requested to be exported, export it.
425422
if (EXPORTED_RUNTIME_METHODS.has(name)) {
426-
// Unless we are in WASM_ESM_INTEGRATION mode then HEAP objects are
423+
// Unless we are in MODULARIZE=instance mode then HEAP objects are
427424
// exported separately in updateMemoryViews
428-
if (WASM_ESM_INTEGRATION || !name.startsWith('HEAP')) {
425+
if (MODULARIZE == 'instance' || !name.startsWith('HEAP')) {
429426
return exportSymbol(name);
430427
}
431428
}
@@ -526,7 +523,7 @@ function exportRuntimeSymbols() {
526523
const exports = runtimeElements.map(maybeExport);
527524
const results = exports.filter((name) => name);
528525

529-
if (WASM_ESM_INTEGRATION) {
526+
if (MODULARIZE == 'instance') {
530527
return '// Runtime exports\nexport { ' + results.join(', ') + ' };\n';
531528
}
532529

@@ -562,6 +559,7 @@ function exportRuntimeSymbols() {
562559
}
563560

564561
function exportLibrarySymbols() {
562+
assert(!MODULARIZE != 'instance');
565563
const results = ['// Begin JS library exports'];
566564
for (const ident of librarySymbols) {
567565
if (EXPORT_ALL || EXPORTED_FUNCTIONS.has(ident)) {
@@ -573,9 +571,9 @@ function exportLibrarySymbols() {
573571
}
574572

575573
function exportJSSymbols() {
576-
// In WASM_ESM_INTEGRATION mode JS library symbols are marked with `export`
574+
// In MODULARIZE=instance mode JS library symbols are marked with `export`
577575
// at the point of declaration.
578-
if (WASM_ESM_INTEGRATION) return exportRuntimeSymbols();
576+
if (MODULARIZE == 'instance') return exportRuntimeSymbols();
579577
return exportRuntimeSymbols() + ' ' + exportLibrarySymbols();
580578
}
581579

src/postamble.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,22 @@ function preInit() {
299299
#endif
300300
}
301301

302-
#if WASM_ESM_INTEGRATION
303-
export default function init(moduleArg = {}) {
302+
#if MODULARIZE == 'instance'
303+
export default async function init(moduleArg = {}) {
304304
Module = moduleArg;
305-
updateMemoryViews();
306305
processModuleArgs();
306+
#if WASM_ESM_INTEGRATION
307+
updateMemoryViews();
308+
#else
309+
wasmExports = await createWasm();
310+
assignWasmExports();
311+
#endif
307312
preInit();
308313
run();
309314
}
315+
#if PTHREADS
316+
if (ENVIRONMENT_IS_PTHREAD) await init()
317+
#endif
310318
#else
311319
preInit();
312320
run();

src/postlibrary.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// This file is included after the automatically-generated JS library code
22
// but before the wasm module is created.
3-
4-
#if WASM_ESM_INTEGRATION
3+
4+
#if MODULARIZE == 'instance'
55
function processModuleArgs()
66
#endif
77
{
8+
#if IMPORTED_MEMORY
9+
initMemory();
10+
#endif
11+
812
<<< ATMODULES >>>
913

1014
#if ASSERTIONS

src/preamble_minimal.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ var runtimeExited = false;
5353

5454
#if IMPORTED_MEMORY
5555
#include "runtime_init_memory.js"
56-
#endif // IMPORTED_MEMORY
56+
initMemory();
57+
#endif

src/runtime_init_memory.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
// check for full engine support (use string 'subarray' to avoid closure compiler confusion)
1313

14+
function initMemory() {
1415
#if PTHREADS
15-
if (!ENVIRONMENT_IS_PTHREAD) {
16+
if (ENVIRONMENT_IS_PTHREAD) return
1617
#endif // PTHREADS
1718

1819
#if expectToReceiveOnModule('wasmMemory')
@@ -53,7 +54,5 @@ if (!ENVIRONMENT_IS_PTHREAD) {
5354
}
5455

5556
updateMemoryViews();
56-
#if PTHREADS
5757
}
58-
#endif
5958

src/runtime_shared.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ var wasmOffsetConverter;
3838
{{{
3939
// Helper function to export a heap symbol on the module object,
4040
// if requested.
41-
const maybeExportHeap = (x) => {
42-
// For now, we export all heap object when not building with MINIMAL_RUNTIME
41+
const shouldExportHeap = (x) => {
4342
let shouldExport = !MINIMAL_RUNTIME && !STRICT;
4443
if (!shouldExport) {
4544
if (MODULARIZE && EXPORT_ALL) {
@@ -53,10 +52,10 @@ var wasmOffsetConverter;
5352
shouldExport = true;
5453
}
5554
}
56-
if (shouldExport) {
57-
if (MODULARIZE === 'instance' && !WASM_ESM_INTEGRATION) {
58-
return `__exp_${x} = `
59-
}
55+
return shouldExport;
56+
}
57+
const maybeExportHeap = (x) => {
58+
if (shouldExportHeap(x) && MODULARIZE != 'instance') {
6059
return `Module['${x}'] = `;
6160
}
6261
return '';

src/shell.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
// after the generated code, you will need to define var Module = {};
2121
// before the code. Then that object will be used in the code, and you
2222
// can continue to use Module afterwards as well.
23-
#if WASM_ESM_INTEGRATION
23+
#if MODULARIZE
24+
#if MODULARIZE == 'instance'
2425
var Module;
25-
#elif MODULARIZE
26+
#else
2627
var Module = moduleArg;
28+
#endif
2729
#elif USE_CLOSURE_COMPILER
2830
/** @type{Object} */
2931
var Module;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5898
1+
5916
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12953
1+
12961

0 commit comments

Comments
 (0)