@@ -2230,6 +2230,9 @@ function integrateWasmJS() {
2230
2230
}
2231
2231
2232
2232
function getBinaryPromise ( ) {
2233
+ //if (!Module['wasmbinary'] && typeof WebAssembly.instantiateStreaming === 'function') {
2234
+ // return fetch(wasmBinaryFile, { credentials: 'same-origin' })
2235
+ //}
2233
2236
// if we don't have the binary yet, and have the Fetch api, use that
2234
2237
// 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
2235
2238
if ( ! Module [ 'wasmBinary' ] && ( ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER ) && typeof fetch === 'function' ) {
@@ -2318,19 +2321,39 @@ function integrateWasmJS() {
2318
2321
// later), so we save Module and check it later.
2319
2322
var trueModule = Module ;
2320
2323
#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.
2324
2326
// receiveInstance() will swap in the exports (to Module.asm) so they can be called
2325
2327
#if ASSERTIONS
2326
2328
assert ( Module === trueModule , 'the Module object should not be replaced during async compilation - perhaps the order of HTML elements is wrong?' ) ;
2327
2329
trueModule = null ;
2328
2330
#endif
2329
2331
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 ) ;
2334
2357
return { } ; // no exports yet; we'll fill them in later
2335
2358
#else
2336
2359
var instance ;
0 commit comments