Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,36 @@ async function getWasmBinary(binaryFile) {

#if SPLIT_MODULE
{{{ makeModuleReceiveWithVar('loadSplitModule', undefined, 'instantiateSync') }}}
var splitModuleProxyHandler = {
get(target, prop, receiver) {
return (...args) => {
var wasmImportsProxyHandler = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep the splitModule prefix (or at least include it somewhere in the name) so its clear when this needed/used?

Copy link
Member Author

@aheejin aheejin Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed it to splitImportsProxyHandler: e918916

get(target, env, receiver) {
if (env.startsWith('placeholder')) {
var splitModuleProxyHandler = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just call this one innerHandler then maybe?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inner name is less important since its not in the global namespace. It could even just be inlined (anonymous) bat that could make harder to grok maybe?

Copy link
Member Author

@aheejin aheejin Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed the outer handler to splitModuleProxyHandler and the inner one to innerHandler: eaea168

The inner handler name is used here, so now sure how to anonymize it.

      return new Proxy({}, innerHandler); 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you could just write return new Proxy({}, { ... inner handler code }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 67e5380

get(target, base, receiver) {
return (...args) => {
#if ASYNCIFY == 2
throw new Error('Placeholder function "' + prop + '" should not be called when using JSPI.');
throw new Error('Placeholder function "' + base + '" should not be called when using JSPI.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(As an aside I wonder if this line should be an assert).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure... Can't this be triggered by user errors? If so an Error might be fine?

#else
err(`placeholder function called: ${prop}`);
var imports = {'primary': wasmExports};
// Replace '.wasm' suffix with '.deferred.wasm'.
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm'
loadSplitModule(deferred, imports, prop);
err('instantiated deferred module, continuing');
// TODO: Implement multi-split module loading
err(`placeholder function called: ${base}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing, but should this be dbg and wrapped in ASSERTIONS or somesuch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used dbg in two places and wrapped them in RUNTIME_DEBUG, given that the places where dbg() is used in this file are all seem to be wrapped in it: 4f2f3b3

var imports = {'primary': wasmExports};
// Replace '.wasm' suffix with '.deferred.wasm'.
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm'
loadSplitModule(deferred, imports, base);
err('instantiated deferred module, continuing');
#if RELOCATABLE
// When the table is dynamically laid out, the placeholder functions names
// are offsets from the table base. In the main module, the table base is
// always 1.
prop = 1 + parseInt(prop);
// When the table is dynamically laid out, the placeholder functions names
// are offsets from the table base. In the main module, the table base is
// always 1.
base = 1 + parseInt(base);
#endif
return wasmTable.get({{{ toIndexType('prop') }}})(...args);
return wasmTable.get({{{ toIndexType('base') }}})(...args);
#endif
}
}
};
return new Proxy({}, splitModuleProxyHandler);
}
return target[env];
}
};
#endif
Expand Down Expand Up @@ -633,6 +642,7 @@ async function instantiateAsync(binary, binaryFile, imports) {
#endif // SOURCE_PHASE_IMPORTS

#if !WASM_ESM_INTEGRATION

function getWasmImports() {
#if PTHREADS || WASM_WORKERS || (IMPORTED_MEMORY && MODULARIZE == 'instance')
assignWasmImports();
Expand All @@ -653,21 +663,22 @@ function getWasmImports() {
#endif
#endif
// prepare imports
return {
var imports = {
#if MINIFY_WASM_IMPORTED_MODULES
'a': wasmImports,
#else // MINIFY_WASM_IMPORTED_MODULES
'env': wasmImports,
'{{{ WASI_MODULE_NAME }}}': wasmImports,
#endif // MINIFY_WASM_IMPORTED_MODULES
#if SPLIT_MODULE
'placeholder': new Proxy({}, splitModuleProxyHandler),
#endif
#if RELOCATABLE
'GOT.mem': new Proxy(wasmImports, GOTHandler),
'GOT.func': new Proxy(wasmImports, GOTHandler),
#endif
}
};
#if SPLIT_MODULE
imports = new Proxy(imports, wasmImportsProxyHandler);
#endif
return imports;
}

// Create the wasm instance.
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 7059,
"a.out.js.gz": 3330,
"a.out.nodebug.wasm": 169721,
"a.out.nodebug.wasm.gz": 62983,
"a.out.nodebug.wasm.gz": 62981,
"total": 176780,
"total_gz": 66313,
"total_gz": 66311,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down
3 changes: 2 additions & 1 deletion test/codesize/test_codesize_file_preload.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,10 @@ async function instantiateAsync(binary, binaryFile, imports) {

function getWasmImports() {
// prepare imports
return {
var imports = {
"a": wasmImports
};
return imports;
}

// Create the wasm instance.
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_files_wasmfs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.out.js": 5465,
"a.out.js.gz": 2576,
"a.out.nodebug.wasm": 50218,
"a.out.nodebug.wasm.gz": 18082,
"a.out.nodebug.wasm.gz": 18086,
"total": 55683,
"total_gz": 20658,
"total_gz": 20662,
"sent": [
"a (emscripten_date_now)",
"b (emscripten_err)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_hello_O0.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 24070,
"a.out.js.gz": 8666,
"a.out.nodebug.wasm": 15127,
"a.out.nodebug.wasm.gz": 7450,
"total": 39197,
"total_gz": 16116,
"a.out.nodebug.wasm": 15119,
"a.out.nodebug.wasm.gz": 7444,
"total": 39189,
"total_gz": 16110,
"sent": [
"fd_write"
],
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 245829,
"a.out.nodebug.wasm": 597724,
"total": 843553,
"a.out.nodebug.wasm": 597730,
"total": 843559,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down
5 changes: 3 additions & 2 deletions test/codesize/test_codesize_minimal_O0.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,11 @@ async function instantiateAsync(binary, binaryFile, imports) {

function getWasmImports() {
// prepare imports
return {
var imports = {
'env': wasmImports,
'wasi_snapshot_preview1': wasmImports,
}
};
return imports;
}

// Create the wasm instance.
Expand Down
28 changes: 14 additions & 14 deletions test/codesize/test_unoptimized_code_size.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"hello_world.js": 56566,
"hello_world.js.gz": 17601,
"hello_world.wasm": 15127,
"hello_world.wasm.gz": 7450,
"no_asserts.js": 26608,
"no_asserts.js.gz": 8874,
"no_asserts.wasm": 12227,
"no_asserts.wasm.gz": 6010,
"strict.js": 54581,
"strict.js.gz": 16947,
"strict.wasm": 15127,
"strict.wasm.gz": 7447,
"total": 180236,
"total_gz": 64329
"hello_world.js": 56592,
"hello_world.js.gz": 17608,
"hello_world.wasm": 15119,
"hello_world.wasm.gz": 7444,
"no_asserts.js": 26634,
"no_asserts.js.gz": 8883,
"no_asserts.wasm": 12219,
"no_asserts.wasm.gz": 6005,
"strict.js": 54607,
"strict.js.gz": 16955,
"strict.wasm": 15119,
"strict.wasm.gz": 7442,
"total": 180290,
"total_gz": 64337
}
Loading