Use WebAssembly.instantiateStreaming when presented in browser (#2477)
This commit is contained in:
committed by
Nikolay Igotti
parent
5220478f15
commit
09d082c678
@@ -210,15 +210,27 @@ function instantiateAndRunSync(arraybuffer, args) {
|
|||||||
return invokeModule(instance, 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) {
|
konan.moduleEntry = function (args) {
|
||||||
if (isBrowser()) {
|
if (isBrowser()) {
|
||||||
if (!document.currentScript.hasAttribute("wasm")) {
|
if (!document.currentScript.hasAttribute("wasm")) {
|
||||||
throw new Error('Could not find the wasm attribute pointing to the WebAssembly binary.');
|
throw new Error('Could not find the wasm attribute pointing to the WebAssembly binary.');
|
||||||
}
|
}
|
||||||
const filename = document.currentScript.getAttribute("wasm");
|
const filename = document.currentScript.getAttribute("wasm");
|
||||||
fetch(filename)
|
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
||||||
.then(response => response.arrayBuffer())
|
instantiateAndRunStreaming(filename);
|
||||||
.then(arraybuffer => instantiateAndRun(arraybuffer, [filename]));
|
} else {
|
||||||
|
fetch(filename)
|
||||||
|
.then(response => response.arrayBuffer())
|
||||||
|
.then(arraybuffer => instantiateAndRun(arraybuffer, [filename]));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Invoke from d8.
|
// Invoke from d8.
|
||||||
const arrayBuffer = readbuffer(args[0]);
|
const arrayBuffer = readbuffer(args[0]);
|
||||||
|
|||||||
Reference in New Issue
Block a user