Skip to content

Commit e2bd1bb

Browse files
Avoid refetching and recompiling the WASM.
1 parent 9480205 commit e2bd1bb

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/board/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export class Board {
167167
fs: this.fs,
168168
conversions,
169169
noInitialRun: true,
170+
instantiateWasm,
170171
});
171172
const module = new ModuleWrapper(wrapped);
172173
this.audio.initializeCallbacks({
@@ -675,3 +676,34 @@ function isFileSystem(
675676
([k, v]) => typeof k === "string" && v instanceof Uint8Array
676677
);
677678
}
679+
680+
const fetchWasm = async () => {
681+
const response = await fetch("/build/firmware.wasm");
682+
if (!response.ok) {
683+
throw new Error(response.statusText);
684+
}
685+
return response.arrayBuffer();
686+
};
687+
688+
const compileWasm = async () => {
689+
// Can't use streaming in Safari 14 but would be nice to feature detect.
690+
return WebAssembly.compile(new Uint8Array(await fetchWasm()));
691+
};
692+
693+
let compiledWasmPromise: Promise<WebAssembly.Module> = compileWasm();
694+
695+
const instantiateWasm = function (imports: any, successCallback: any) {
696+
// No easy way to communicate failure here so hard to add retries.
697+
compiledWasmPromise
698+
.then(async (wasmModule) => {
699+
const instance = await WebAssembly.instantiate(wasmModule, imports);
700+
console.log("Woah!");
701+
successCallback(instance);
702+
})
703+
.catch((e) => {
704+
console.error("Failed to instantiate WASM");
705+
console.error(e);
706+
});
707+
// Result via callback.
708+
return {};
709+
};

0 commit comments

Comments
 (0)