@@ -771,54 +771,56 @@ function resetPrototype(constructor, attrs) {
771771#endif
772772
773773#if WASM_ASYNC_COMPILATION
774- function instantiateArrayBuffer ( binaryFile , imports , receiver ) {
774+ function instantiateArrayBuffer ( binaryFile , imports ) {
775775#if USE_OFFSET_CONVERTER
776776 var savedBinary ;
777777#endif
778- return getBinaryPromise ( binaryFile ) . then ( ( binary ) => {
778+ return new Promise ( ( resolve , reject ) => {
779+ getBinaryPromise ( binaryFile ) . then ( ( binary ) => {
779780#if USE_OFFSET_CONVERTER
780- savedBinary = binary ;
781+ savedBinary = binary ;
781782#endif
782- return WebAssembly . instantiate ( binary , imports ) ;
783+ return WebAssembly . instantiate ( binary , imports ) ;
783784#if USE_OFFSET_CONVERTER
784- } ) . then ( ( instance ) => {
785- // wasmOffsetConverter needs to be assigned before calling the receiver
786- // (receiveInstantiationResult). See comments below in instantiateAsync.
787- wasmOffsetConverter = new WasmOffsetConverter ( savedBinary , instance . module ) ;
788- return instance ;
785+ } ) . then ( ( instance ) => {
786+ // wasmOffsetConverter needs to be assigned before calling resolve.
787+ // See comments below in instantiateAsync.
788+ wasmOffsetConverter = new WasmOffsetConverter ( savedBinary , instance . module ) ;
789+ return instance ;
789790#endif
790- } ) . then ( receiver , ( reason ) => {
791- err ( `failed to asynchronously prepare wasm: ${ reason } ` ) ;
791+ } ) . then ( resolve , ( reason ) => {
792+ err ( `failed to asynchronously prepare wasm: ${ reason } ` ) ;
792793
793794#if WASM == 2
794795#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
795- if ( typeof location != 'undefined' ) {
796- #endif
797- // WebAssembly compilation failed, try running the JS fallback instead.
798- var search = location . search ;
799- if ( search . indexOf ( '_rwasm=0' ) < 0 ) {
800- location . href += ( search ? search + '&' : '?' ) + '_rwasm=0' ;
801- // Return here to avoid calling abort() below. The application
802- // still has a chance to start successfully do we don't want to
803- // trigger onAbort or onExit handlers.
804- return ;
805- }
796+ if ( typeof location != 'undefined' ) {
797+ #endif
798+ // WebAssembly compilation failed, try running the JS fallback instead.
799+ var search = location . search ;
800+ if ( search . indexOf ( '_rwasm=0' ) < 0 ) {
801+ location . href += ( search ? search + '&' : '?' ) + '_rwasm=0' ;
802+ // Return here to avoid calling abort() below. The application
803+ // still has a chance to start successfully do we don't want to
804+ // trigger onAbort or onExit handlers.
805+ return ;
806+ }
806807#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
807- }
808+ }
808809#endif
809810#endif // WASM == 2
810811
811812#if ASSERTIONS
812- // Warn on some common problems.
813- if ( isFileURI ( wasmBinaryFile ) ) {
814- err ( `warning: Loading from a file URI (${ wasmBinaryFile } ) is not supported in most browsers. See https://emscripten.org/docs/getting_started/FAQ.html#how-do-i-run-a-local-webserver-for-testing-why-does-my-program-stall-in-downloading-or-preparing` ) ;
815- }
813+ // Warn on some common problems.
814+ if ( isFileURI ( wasmBinaryFile ) ) {
815+ err ( `warning: Loading from a file URI (${ wasmBinaryFile } ) is not supported in most browsers. See https://emscripten.org/docs/getting_started/FAQ.html#how-do-i-run-a-local-webserver-for-testing-why-does-my-program-stall-in-downloading-or-preparing` ) ;
816+ }
816817#endif
817- abort ( reason ) ;
818+ reject ( reason ) ;
819+ } ) ;
818820 } ) ;
819821}
820822
821- function instantiateAsync ( binary , binaryFile , imports , callback ) {
823+ function instantiateAsync ( binary , binaryFile , imports ) {
822824#if ! SINGLE_FILE
823825 if ( ! binary &&
824826 typeof WebAssembly . instantiateStreaming == 'function' &&
@@ -837,56 +839,59 @@ function instantiateAsync(binary, binaryFile, imports, callback) {
837839 ! ENVIRONMENT_IS_NODE &&
838840#endif
839841 typeof fetch == 'function' ) {
840- return fetch ( binaryFile , { { { makeModuleReceiveExpr ( 'fetchSettings' , "{ credentials: 'same-origin' }" ) } } } ) . then ( ( response ) => {
841- // Suppress closure warning here since the upstream definition for
842- // instantiateStreaming only allows Promise<Repsponse> rather than
843- // an actual Response.
844- // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure is fixed.
845- /** @suppress {checkTypes} */
846- var result = WebAssembly . instantiateStreaming ( response , imports ) ;
842+ return new Promise ( ( resolve ) => {
843+ fetch ( binaryFile , { { { makeModuleReceiveExpr ( 'fetchSettings' , "{ credentials: 'same-origin' }" ) } } } ) . then ( ( response ) => {
844+ // Suppress closure warning here since the upstream definition for
845+ // instantiateStreaming only allows Promise<Repsponse> rather than
846+ // an actual Response.
847+ // TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure is fixed.
848+ /** @suppress {checkTypes} */
849+ var result = WebAssembly . instantiateStreaming ( response , imports ) ;
850+
851+ #if USE_OFFSET_CONVERTER
852+ // We need the wasm binary for the offset converter. Clone the response
853+ // in order to get its arrayBuffer (cloning should be more efficient
854+ // than doing another entire request).
855+ // (We must clone the response now in order to use it later, as if we
856+ // try to clone it asynchronously lower down then we will get a
857+ // "response was already consumed" error.)
858+ var clonedResponsePromise = response . clone ( ) . arrayBuffer ( ) ;
859+ #endif
847860
861+ result . then (
848862#if USE_OFFSET_CONVERTER
849- // We need the wasm binary for the offset converter. Clone the response
850- // in order to get its arrayBuffer (cloning should be more efficient
851- // than doing another entire request).
852- // (We must clone the response now in order to use it later, as if we
853- // try to clone it asynchronously lower down then we will get a
854- // "response was already consumed" error.)
855- var clonedResponsePromise = response . clone ( ) . arrayBuffer ( ) ;
856- #endif
857-
858- return result . then (
859- #if ! USE_OFFSET_CONVERTER
860- callback ,
863+ ( instantiationResult ) => {
864+ // When using the offset converter, we must interpose here. First,
865+ // the instantiation result must arrive (if it fails, the error
866+ // handling later down will handle it). Once it arrives, we can
867+ // initialize the offset converter. And only then is it valid to
868+ // call receiveInstantiationResult, as that function will use the
869+ // offset converter (in the case of pthreads, it will create the
870+ // pthreads and send them the offsets along with the wasm instance).
871+
872+ clonedResponsePromise . then ( ( arrayBufferResult ) => {
873+ wasmOffsetConverter = new WasmOffsetConverter ( new Uint8Array ( arrayBufferResult ) , instantiationResult . module ) ;
874+ resolve ( instantiationResult ) ;
875+ } ,
876+ ( reason ) => err ( `failed to initialize offset-converter: ${ reason } ` )
877+ ) ;
878+ } ,
861879#else
862- function ( instantiationResult ) {
863- // When using the offset converter, we must interpose here. First,
864- // the instantiation result must arrive (if it fails, the error
865- // handling later down will handle it). Once it arrives, we can
866- // initialize the offset converter. And only then is it valid to
867- // call receiveInstantiationResult, as that function will use the
868- // offset converter (in the case of pthreads, it will create the
869- // pthreads and send them the offsets along with the wasm instance).
870-
871- clonedResponsePromise . then ( ( arrayBufferResult ) => {
872- wasmOffsetConverter = new WasmOffsetConverter ( new Uint8Array ( arrayBufferResult ) , instantiationResult . module ) ;
873- callback ( instantiationResult ) ;
874- } ,
875- ( reason ) => err ( `failed to initialize offset-converter: ${ reason } ` )
876- ) ;
877- } ,
878- #endif
879- function ( reason ) {
880- // We expect the most common failure cause to be a bad MIME type for the binary,
881- // in which case falling back to ArrayBuffer instantiation should work.
882- err ( `wasm streaming compile failed: ${ reason } ` ) ;
883- err ( 'falling back to ArrayBuffer instantiation' ) ;
884- return instantiateArrayBuffer ( binaryFile , imports , callback ) ;
885- } ) ;
880+ resolve ,
881+ #endif
882+ ( reason ) => {
883+ // We expect the most common failure cause to be a bad MIME type for the binary,
884+ // in which case falling back to ArrayBuffer instantiation should work.
885+ err ( `wasm streaming compile failed: ${ reason } ` ) ;
886+ err ( 'falling back to ArrayBuffer instantiation' ) ;
887+ return resolve ( instantiateArrayBuffer ( binaryFile , imports ) ) ;
888+ }
889+ ) ;
890+ } ) ;
886891 } ) ;
887- }
892+ } else
888893#endif
889- return instantiateArrayBuffer ( binaryFile , imports , callback ) ;
894+ return instantiateArrayBuffer ( binaryFile , imports ) ;
890895}
891896#endif // WASM_ASYNC_COMPILATION
892897
@@ -1097,12 +1102,12 @@ function createWasm() {
10971102#if RUNTIME_DEBUG
10981103 dbg ( 'asynchronously preparing wasm' ) ;
10991104#endif
1105+ instantiateAsync ( wasmBinary , wasmBinaryFile , info ) . then ( receiveInstantiationResult )
11001106#if MODULARIZE
11011107 // If instantiation fails, reject the module ready promise.
1102- instantiateAsync ( wasmBinary , wasmBinaryFile , info , receiveInstantiationResult ) . catch ( readyPromiseReject ) ;
1103- #else
1104- instantiateAsync ( wasmBinary , wasmBinaryFile , info , receiveInstantiationResult ) ;
1108+ . catch ( readyPromiseReject ) ;
11051109#endif
1110+ ;
11061111#if LOAD_SOURCE_MAP
11071112 getSourceMapPromise ( ) . then ( receiveSourceMapJSON ) ;
11081113#endif
0 commit comments