Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
Use WebAssembly.instantiateStreaming when presented in browser (#2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
chicoxyzzy authored and olonho committed Jan 16, 2019
1 parent 5220478 commit 09d082c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions runtime/src/launcher/js/launcher.js
Expand Up @@ -210,15 +210,27 @@ function instantiateAndRunSync(arraybuffer, args) {
return invokeModule(instance, args)
}


// Instantiate module in Browser as a promise of streaming instantiation.
function instantiateAndRunStreaming(filename) {
linkJavaScriptLibraries();
WebAssembly.instantiateStreaming(fetch(filename), konan_dependencies)
.then(resultObject => invokeModule(resultObject.instance, [filename]));
}

konan.moduleEntry = function (args) {
if (isBrowser()) {
if (!document.currentScript.hasAttribute("wasm")) {
throw new Error('Could not find the wasm attribute pointing to the WebAssembly binary.');
}
const filename = document.currentScript.getAttribute("wasm");
fetch(filename)
.then(response => response.arrayBuffer())
.then(arraybuffer => instantiateAndRun(arraybuffer, [filename]));
if (typeof WebAssembly.instantiateStreaming === 'function') {
instantiateAndRunStreaming(filename);
} else {
fetch(filename)
.then(response => response.arrayBuffer())
.then(arraybuffer => instantiateAndRun(arraybuffer, [filename]));
}
} else {
// Invoke from d8.
const arrayBuffer = readbuffer(args[0]);
Expand Down

0 comments on commit 09d082c

Please sign in to comment.