Skip to content

Use WebAssembly.compileStreaming API if available #5606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -2318,19 +2318,37 @@ function integrateWasmJS() {
// later), so we save Module and check it later.
var trueModule = Module;
#endif
getBinaryPromise().then(function(binary) {
return WebAssembly.instantiate(binary, info)
}).then(function(output) {
function receiveInstantiatedSource(output) {
// 'output' is a WebAssemblyInstantiatedSource object which has both the module and instance.
// receiveInstance() will swap in the exports (to Module.asm) so they can be called
#if ASSERTIONS
assert(Module === trueModule, 'the Module object should not be replaced during async compilation - perhaps the order of HTML elements is wrong?');
trueModule = null;
#endif
receiveInstance(output['instance']);
}).catch(function(reason) {
Module['printErr']('failed to asynchronously prepare wasm: ' + reason);
abort(reason);
});
}
function instantiateArrayBuffer(receiver) {
getBinaryPromise().then(function(binary) {
return WebAssembly.instantiate(binary, info);
}).then(receiver).catch(function(reason) {
Module['printErr']('failed to asynchronously prepare wasm: ' + reason);
abort(reason);
});
}
// Prefer streaming instantiation if available.
if (!Module['wasmBinary'] && typeof WebAssembly.instantiateStreaming === 'function') {
WebAssembly.instantiateStreaming(fetch(wasmBinaryFile, { credentials: 'same-origin' }), info)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation made this a little hard for me to read (to see where the .s go etc.). How about

WebAssembly.instantiateStreaming(
  fetch(wasmBinaryFile, { credentials: 'same-origin' }), info)
    .then(receiveInstantiatedSource)
    .catch(function(reason) {
      // We expect the most common failure cause to be a bad MIME type for the binary,
      // in which case falling back to ArrayBuffer instantiation should work.
      Module['printErr']('wasm streaming compile failed: ' + reason);
      Module['printErr']('falling back to ArrayBuffer instantiation');
      instantiateArrayBuffer(receiveInstantiatedSource);
    })
);

Also I had to add a ) so I think one might have been missing before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that indentation isn't right because the .then and .catch are not attached to the promise returned by fetch, they go with the return of instantiateStreaming. The fetch's promise is the first argument to instantiateStreaming. (That is also the reason why the number of parens is correct). I did make the .catch line up with the .then though, agreed that's better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, looks nice.

.then(receiveInstantiatedSource)
.catch(function(reason) {
// We expect the most common failure cause to be a bad MIME type for the binary,
// in which case falling back to ArrayBuffer instantiation should work.
Module['printErr']('wasm streaming compile failed: ' + reason);
Module['printErr']('falling back to ArrayBuffer instantiation');
instantiateArrayBuffer(receiveInstantiatedSource);
});
} else {
instantiateArrayBuffer(receiveInstantiatedSource);
}
return {}; // no exports yet; we'll fill them in later
#else
var instance;
Expand Down
2 changes: 2 additions & 0 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ def do_GET(self):
def log_request(code=0, size=0):
# don't log; too noisy
pass

SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map['.wasm'] = 'application/wasm'
os.chdir(dir)
httpd = BaseHTTPServer.HTTPServer(('localhost', 8888), TestServerHandler)
httpd.serve_forever() # test runner will kill us
Expand Down