Skip to content

Commit 56ed8e9

Browse files
authored
[SPLIT_MODULE] Support multi-split loading (#25571)
Currently, if the primary module name is `test.wasm` and when a placeholder function is called, the emscripten JS runtime loads `test.deferred.wasm`, which is hardcoded. But as we want emscripten to support multi-split, the runtime has to know which secondary module to load. Binaryen's WebAssembly/binaryen#7975 changes the placeholder import module naming scheme. Currently placeholder's import modules are all `placeholder`: https://github.com/WebAssembly/binaryen/blob/dcc704ce20e4723ae42012039bdb3b84ec2035f9/test/lit/wasm-split/multi-split.wast#L291-L295 But the Binaryen's companion PR will change this to the format of `placeholder.[secondaryName]`. Then Emscripten runtime will parse this module name to learn which secondary file to load. For example, if the import module is `placeholder.2` and the import base is `3` and the primary wasm file is `test.wasm`, ```wast (import "placeholder.2" "3" (func $placeholder_3 (result i32))) ``` when `placeholder_3` function is loaded, the runtime will parse `placeholder.2` and correctly figure out that it should load `test.2.wasm`. This PR still supports the old `placeholder` namespace, because this PR has to land first for the Binaryen companion PR to land. After it lands, we can remove the `placeholder` handling part. Companion PR: WebAssembly/binaryen#7975
1 parent 0b127ff commit 56ed8e9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/preamble.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,20 +490,25 @@ async function getWasmBinary(binaryFile) {
490490
var splitModuleProxyHandler = {
491491
get(target, moduleName, receiver) {
492492
if (moduleName.startsWith('placeholder')) {
493+
let secondaryFile;
494+
if (moduleName == 'placeholder') { // old format
495+
secondaryFile = wasmBinaryFile.slice(0, -5) + '.deferred.wasm';
496+
} else { // new format
497+
let moduleID = moduleName.split('.')[1];
498+
secondaryFile = wasmBinaryFile.slice(0, -5) + '.' + moduleID + '.wasm';
499+
}
493500
return new Proxy({}, {
494501
get(target, base, receiver) {
495502
return (...args) => {
496503
#if ASYNCIFY == 2
497504
throw new Error('Placeholder function "' + base + '" should not be called when using JSPI.');
498505
#else
499-
// TODO: Implement multi-split module loading
500506
#if RUNTIME_DEBUG
501507
dbg(`placeholder function called: ${base}`);
502508
#endif
503509
var imports = {'primary': wasmExports};
504510
// Replace '.wasm' suffix with '.deferred.wasm'.
505-
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm'
506-
loadSplitModule(deferred, imports, base);
511+
loadSplitModule(secondaryFile, imports, base);
507512
#if RUNTIME_DEBUG
508513
dbg('instantiated deferred module, continuing');
509514
#endif

0 commit comments

Comments
 (0)