Skip to content

Commit 24ea1a9

Browse files
authored
Use WebAssembly.compileStreaming API if available (#5606)
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 82e9691 commit 24ea1a9

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/preamble.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -2318,19 +2318,37 @@ function integrateWasmJS() {
23182318
// later), so we save Module and check it later.
23192319
var trueModule = Module;
23202320
#endif
2321-
getBinaryPromise().then(function(binary) {
2322-
return WebAssembly.instantiate(binary, info)
2323-
}).then(function(output) {
2321+
function receiveInstantiatedSource(output) {
2322+
// 'output' is a WebAssemblyInstantiatedSource object which has both the module and instance.
23242323
// receiveInstance() will swap in the exports (to Module.asm) so they can be called
23252324
#if ASSERTIONS
23262325
assert(Module === trueModule, 'the Module object should not be replaced during async compilation - perhaps the order of HTML elements is wrong?');
23272326
trueModule = null;
23282327
#endif
23292328
receiveInstance(output['instance']);
2330-
}).catch(function(reason) {
2331-
Module['printErr']('failed to asynchronously prepare wasm: ' + reason);
2332-
abort(reason);
2333-
});
2329+
}
2330+
function instantiateArrayBuffer(receiver) {
2331+
getBinaryPromise().then(function(binary) {
2332+
return WebAssembly.instantiate(binary, info);
2333+
}).then(receiver).catch(function(reason) {
2334+
Module['printErr']('failed to asynchronously prepare wasm: ' + reason);
2335+
abort(reason);
2336+
});
2337+
}
2338+
// Prefer streaming instantiation if available.
2339+
if (!Module['wasmBinary'] && typeof WebAssembly.instantiateStreaming === 'function') {
2340+
WebAssembly.instantiateStreaming(fetch(wasmBinaryFile, { credentials: 'same-origin' }), info)
2341+
.then(receiveInstantiatedSource)
2342+
.catch(function(reason) {
2343+
// We expect the most common failure cause to be a bad MIME type for the binary,
2344+
// in which case falling back to ArrayBuffer instantiation should work.
2345+
Module['printErr']('wasm streaming compile failed: ' + reason);
2346+
Module['printErr']('falling back to ArrayBuffer instantiation');
2347+
instantiateArrayBuffer(receiveInstantiatedSource);
2348+
});
2349+
} else {
2350+
instantiateArrayBuffer(receiveInstantiatedSource);
2351+
}
23342352
return {}; // no exports yet; we'll fill them in later
23352353
#else
23362354
var instance;

tests/runner.py

+2
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ def do_GET(self):
746746
def log_request(code=0, size=0):
747747
# don't log; too noisy
748748
pass
749+
750+
SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map['.wasm'] = 'application/wasm'
749751
os.chdir(dir)
750752
httpd = BaseHTTPServer.HTTPServer(('localhost', 8888), TestServerHandler)
751753
httpd.serve_forever() # test runner will kill us

0 commit comments

Comments
 (0)