Skip to content

Commit ccdf34c

Browse files
committed
Use WebAssembly.compileStreaming API if available
This API is a post-MVP addition (http://webassembly.org/docs/web/#additional-web-embedding-api) that does streaming compilation (i.e compiles the wasm binary as it downloads), resulting in faster instantiation in the non-cached case.
1 parent 8aea22d commit ccdf34c

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/preamble.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,9 @@ function integrateWasmJS() {
22302230
}
22312231

22322232
function getBinaryPromise() {
2233+
//if (!Module['wasmbinary'] && typeof WebAssembly.instantiateStreaming === 'function') {
2234+
// return fetch(wasmBinaryFile, { credentials: 'same-origin' })
2235+
//}
22332236
// if we don't have the binary yet, and have the Fetch api, use that
22342237
// in some environments, like Electron's render process, Fetch api may be present, but have a different context than expected, let's only use it on the Web
22352238
if (!Module['wasmBinary'] && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === 'function') {
@@ -2318,19 +2321,39 @@ function integrateWasmJS() {
23182321
// later), so we save Module and check it later.
23192322
var trueModule = Module;
23202323
#endif
2321-
getBinaryPromise().then(function(binary) {
2322-
return WebAssembly.instantiate(binary, info)
2323-
}).then(function(output) {
2324+
function receiveInstantiatedSource(output) {
2325+
// 'output' is a WebAssemblyInstantiatedSource object which has both the module and instance.
23242326
// receiveInstance() will swap in the exports (to Module.asm) so they can be called
23252327
#if ASSERTIONS
23262328
assert(Module === trueModule, 'the Module object should not be replaced during async compilation - perhaps the order of HTML elements is wrong?');
23272329
trueModule = null;
23282330
#endif
23292331
receiveInstance(output['instance']);
2330-
}).catch(function(reason) {
2331-
Module['printErr']('failed to asynchronously prepare wasm: ' + reason);
2332-
abort(reason);
2333-
});
2332+
}
2333+
function instantiateArrayBuffer(receiver) {
2334+
getBinaryPromise().then(function(binary) {
2335+
return WebAssembly.instantiate(binary, info);
2336+
}).then(receiver).catch(function(reason) {
2337+
Module['printErr']('failed to asynchronously prepare wasm: ' + reason);
2338+
abort(reason);
2339+
});
2340+
}
2341+
// Prefer streaming instantiation if available.
2342+
if (!Module['wasmBinary'] && typeof WebAssembly.instantiateStreaming === 'function') {
2343+
console.log("Fetching");
2344+
WebAssembly.instantiateStreaming(fetch(wasmBinaryFile, { credentials: 'same-origin' }), info)
2345+
.then(receiveInstantiatedSource).catch(
2346+
function(reason) {
2347+
// We expect the most common failure cause to be a bad MIME type for the binary,
2348+
// in which case falling back to ArrayBuffer instantiation should work.
2349+
Module['printErr']('wasm streaming compile failed: ' + reason);
2350+
Module['printErr']('falling back to ArrayBuffer instantiation');
2351+
instantiateArrayBuffer(receiveInstantiatedSource);
2352+
}
2353+
);
2354+
return {};
2355+
}
2356+
instantiateArrayBuffer(receiveInstantiatedSource);
23342357
return {}; // no exports yet; we'll fill them in later
23352358
#else
23362359
var instance;

0 commit comments

Comments
 (0)