@@ -2318,19 +2318,37 @@ function integrateWasmJS() {
2318
2318
// later), so we save Module and check it later.
2319
2319
var trueModule = Module ;
2320
2320
#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.
2324
2323
// receiveInstance() will swap in the exports (to Module.asm) so they can be called
2325
2324
#if ASSERTIONS
2326
2325
assert ( Module === trueModule , 'the Module object should not be replaced during async compilation - perhaps the order of HTML elements is wrong?' ) ;
2327
2326
trueModule = null ;
2328
2327
#endif
2329
2328
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
+ }
2334
2352
return { } ; // no exports yet; we'll fill them in later
2335
2353
#else
2336
2354
var instance ;
0 commit comments