diff --git a/src/preamble.js b/src/preamble.js index 89f10bb379b38..a8c91274d6e6c 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -488,26 +488,38 @@ async function getWasmBinary(binaryFile) { #if SPLIT_MODULE {{{ makeModuleReceiveWithVar('loadSplitModule', undefined, 'instantiateSync') }}} var splitModuleProxyHandler = { - get(target, prop, receiver) { - return (...args) => { + get(target, moduleName, receiver) { + if (moduleName.startsWith('placeholder')) { + return new Proxy({}, { + 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.'); #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 +#if RUNTIME_DEBUG + dbg(`placeholder function called: ${base}`); +#endif + var imports = {'primary': wasmExports}; + // Replace '.wasm' suffix with '.deferred.wasm'. + var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm' + loadSplitModule(deferred, imports, base); +#if RUNTIME_DEBUG + dbg('instantiated deferred module, continuing'); +#endif #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 target[moduleName]; } }; #endif @@ -653,21 +665,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, splitModuleProxyHandler); +#endif + return imports; } // Create the wasm instance. diff --git a/test/codesize/test_codesize_file_preload.expected.js b/test/codesize/test_codesize_file_preload.expected.js index 6a54d91bcc8e2..c4e879be679a1 100644 --- a/test/codesize/test_codesize_file_preload.expected.js +++ b/test/codesize/test_codesize_file_preload.expected.js @@ -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. diff --git a/test/codesize/test_codesize_minimal_O0.expected.js b/test/codesize/test_codesize_minimal_O0.expected.js index cd9adf6366115..52577c13e659f 100644 --- a/test/codesize/test_codesize_minimal_O0.expected.js +++ b/test/codesize/test_codesize_minimal_O0.expected.js @@ -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. diff --git a/test/codesize/test_unoptimized_code_size.json b/test/codesize/test_unoptimized_code_size.json index c176785aaee1c..ae60057550c37 100644 --- a/test/codesize/test_unoptimized_code_size.json +++ b/test/codesize/test_unoptimized_code_size.json @@ -1,16 +1,16 @@ { - "hello_world.js": 56566, - "hello_world.js.gz": 17601, + "hello_world.js": 56592, + "hello_world.js.gz": 17608, "hello_world.wasm": 15119, "hello_world.wasm.gz": 7444, - "no_asserts.js": 26608, - "no_asserts.js.gz": 8874, + "no_asserts.js": 26634, + "no_asserts.js.gz": 8883, "no_asserts.wasm": 12219, "no_asserts.wasm.gz": 6005, - "strict.js": 54581, - "strict.js.gz": 16947, + "strict.js": 54607, + "strict.js.gz": 16955, "strict.wasm": 15119, "strict.wasm.gz": 7442, - "total": 180212, - "total_gz": 64313 + "total": 180290, + "total_gz": 64337 }