@@ -645,7 +645,7 @@ function getBinarySync(file) {
645
645
#endif
646
646
}
647
647
648
- function getBinaryPromise ( binaryFile ) {
648
+ async function getWasmBinary ( binaryFile ) {
649
649
#if ! SINGLE_FILE
650
650
// If we don't have the binary yet, load it asynchronously using readAsync.
651
651
if ( ! wasmBinary
@@ -654,16 +654,18 @@ function getBinaryPromise(binaryFile) {
654
654
#endif
655
655
) {
656
656
// Fetch the binary using readAsync
657
- return readAsync ( binaryFile ) . then (
658
- ( response ) => new Uint8Array ( /** @type {!ArrayBuffer } */ ( response ) ) ,
659
- // Fall back to getBinarySync if readAsync fails
660
- ( ) => getBinarySync ( binaryFile )
661
- ) ;
657
+ try {
658
+ /** @type {!ArrayBuffer } */
659
+ var response = await readAsync ( binaryFile ) ;
660
+ return new Uint8Array ( response ) ;
661
+ } catch {
662
+ // Fall back to getBinarySync below;
663
+ }
662
664
}
663
665
#endif
664
666
665
667
// Otherwise, getBinarySync should be able to get it synchronously
666
- return Promise . resolve ( getBinarySync ( binaryFile ) ) ;
668
+ return getBinarySync ( binaryFile ) ;
667
669
}
668
670
669
671
#if LOAD_SOURCE_MAP
@@ -771,56 +773,47 @@ function resetPrototype(constructor, attrs) {
771
773
#endif
772
774
773
775
#if WASM_ASYNC_COMPILATION
774
- function instantiateArrayBuffer ( binaryFile , imports ) {
776
+ async function instantiateArrayBuffer ( binaryFile , imports ) {
777
+ try {
778
+ var binary = await getWasmBinary ( binaryFile ) ;
779
+ var instance = await WebAssembly . instantiate ( binary , imports ) ;
775
780
#if USE_OFFSET_CONVERTER
776
- var savedBinary ;
781
+ // wasmOffsetConverter needs to be assigned before calling resolve.
782
+ // See comments below in instantiateAsync.
783
+ wasmOffsetConverter = new WasmOffsetConverter ( binary , instance . module ) ;
777
784
#endif
778
- return new Promise ( ( resolve , reject ) => {
779
- getBinaryPromise ( binaryFile ) . then ( ( binary ) => {
780
- #if USE_OFFSET_CONVERTER
781
- savedBinary = binary ;
782
- #endif
783
- return WebAssembly . instantiate ( binary , imports ) ;
784
- #if USE_OFFSET_CONVERTER
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 ;
790
- #endif
791
- } ) . then ( resolve , ( reason ) => {
792
- err ( `failed to asynchronously prepare wasm: ${ reason } ` ) ;
793
-
785
+ return instance ;
786
+ } catch ( reason ) {
787
+ err ( `failed to asynchronously prepare wasm: ${ reason } ` ) ;
794
788
#if WASM == 2
795
789
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
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
- }
807
- #if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
790
+ if ( typeof location != 'undefined' ) {
791
+ #endif
792
+ // WebAssembly compilation failed, try running the JS fallback instead.
793
+ var search = location . search ;
794
+ if ( search . indexOf ( '_rwasm=0' ) < 0 ) {
795
+ location . href += ( search ? search + '&' : '?' ) + '_rwasm=0' ;
796
+ // Return here to avoid calling abort() below. The application
797
+ // still has a chance to start successfully do we don't want to
798
+ // trigger onAbort or onExit handlers.
799
+ return ;
808
800
}
801
+ #if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
802
+ }
809
803
#endif
810
804
#endif // WASM == 2
811
805
812
806
#if ASSERTIONS
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
- }
807
+ // Warn on some common problems.
808
+ if ( isFileURI ( wasmBinaryFile ) ) {
809
+ 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` ) ;
810
+ }
817
811
#endif
818
- abort ( reason ) ;
819
- } ) ;
820
- } ) ;
812
+ abort ( reason ) ;
813
+ }
821
814
}
822
815
823
- function instantiateAsync ( binary , binaryFile , imports ) {
816
+ async function instantiateAsync ( binary , binaryFile , imports ) {
824
817
#if ! SINGLE_FILE
825
818
if ( ! binary &&
826
819
typeof WebAssembly . instantiateStreaming == 'function' &&
@@ -839,56 +832,44 @@ function instantiateAsync(binary, binaryFile, imports) {
839
832
! ENVIRONMENT_IS_NODE &&
840
833
#endif
841
834
typeof fetch == 'function' ) {
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
-
835
+ try {
836
+ var response = fetch ( binaryFile , { { { makeModuleReceiveExpr ( 'fetchSettings' , "{ credentials: 'same-origin' }" ) } } } ) ;
851
837
#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
860
-
861
- result . then (
838
+ // We need the wasm binary for the offset converter. Clone the response
839
+ // in order to get its arrayBuffer (cloning should be more efficient
840
+ // than doing another entire request).
841
+ // (We must clone the response now in order to use it later, as if we
842
+ // try to clone it asynchronously lower down then we will get a
843
+ // "response was already consumed" error.)
844
+ var clonedResponse = ( await response ) . clone ( ) ;
845
+ #endif
846
+ var result = WebAssembly . instantiateStreaming ( response , imports ) ;
862
847
#if USE_OFFSET_CONVERTER
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
- } ,
848
+ // When using the offset converter, we must interpose here. First,
849
+ // the instantiation result must arrive (if it fails, the error
850
+ // handling later down will handle it). Once it arrives, we can
851
+ // initialize the offset converter. And only then is it valid to
852
+ // call receiveInstantiationResult, as that function will use the
853
+ // offset converter (in the case of pthreads, it will create the
854
+ // pthreads and send them the offsets along with the wasm instance).
855
+ var instantiationResult = await result ;
856
+ var arrayBufferResult = await clonedResponse . arrayBuffer ( ) ;
857
+ try {
858
+ wasmOffsetConverter = new WasmOffsetConverter ( new Uint8Array ( arrayBufferResult ) , instantiationResult . module ) ;
859
+ } catch ( reason ) {
860
+ err ( `failed to initialize offset-converter: ${ reason } ` ) ;
861
+ }
862
+ return instantiationResult ;
879
863
#else
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
- } ) ;
891
- } ) ;
864
+ return await result ;
865
+ #endif
866
+ } catch ( reason ) {
867
+ // We expect the most common failure cause to be a bad MIME type for the binary,
868
+ // in which case falling back to ArrayBuffer instantiation should work.
869
+ err ( `wasm streaming compile failed: ${ reason } ` ) ;
870
+ err ( 'falling back to ArrayBuffer instantiation' ) ;
871
+ // fall back of instantiateArrayBuffer below
872
+ } ;
892
873
}
893
874
#endif
894
875
return instantiateArrayBuffer ( binaryFile , imports ) ;
@@ -934,7 +915,13 @@ function getWasmImports() {
934
915
935
916
// Create the wasm instance.
936
917
// Receives the wasm imports, returns the exports.
918
+ #if WASM_ASYNC_COMPILATION
919
+ // Funnily enough in JS the `async` keyword has to be on the same line as the
920
+ // function keyword.
921
+ async function createWasm ( ) {
922
+ #else
937
923
function createWasm ( ) {
924
+ #endif
938
925
// Load the wasm module and create an instance of using native support in the JS engine.
939
926
// handle a generated wasm instance, receiving its exports and
940
927
// performing other necessary setup
@@ -1102,17 +1089,23 @@ function createWasm() {
1102
1089
#if RUNTIME_DEBUG
1103
1090
dbg ( 'asynchronously preparing wasm' ) ;
1104
1091
#endif
1105
- instantiateAsync ( wasmBinary , wasmBinaryFile , info ) . then ( receiveInstantiationResult )
1106
1092
#if MODULARIZE
1107
- // If instantiation fails, reject the module ready promise.
1108
- . catch ( readyPromiseReject )
1093
+ try {
1109
1094
#endif
1110
- ;
1095
+ var result = await instantiateAsync ( wasmBinary , wasmBinaryFile , info ) ;
1096
+ receiveInstantiationResult ( result ) ;
1111
1097
#if LOAD_SOURCE_MAP
1112
- getSourceMapPromise ( ) . then ( receiveSourceMapJSON ) ;
1098
+ receiveSourceMapJSON ( await getSourceMapPromise ( ) ) ;
1113
1099
#endif
1114
- return { } ; // no exports yet; we'll fill them in later
1115
- #else
1100
+ return result ;
1101
+ #if MODULARIZE
1102
+ } catch ( e ) {
1103
+ // If instantiation fails, reject the module ready promise.
1104
+ readyPromiseReject ( e ) ;
1105
+ throw e ;
1106
+ }
1107
+ #endif
1108
+ #else // WASM_ASYNC_COMPILATION
1116
1109
var result = instantiateSync ( wasmBinaryFile , info ) ;
1117
1110
#if PTHREADS || MAIN_MODULE
1118
1111
return receiveInstance ( result [ 0 ] , result [ 1 ] ) ;
@@ -1122,7 +1115,7 @@ function createWasm() {
1122
1115
// When the regression is fixed, we can remove this if/else.
1123
1116
return receiveInstance ( result [ 0 ] ) ;
1124
1117
#endif
1125
- #endif
1118
+ #endif // WASM_ASYNC_COMPILATION
1126
1119
}
1127
1120
1128
1121
#if ! WASM_BIGINT
0 commit comments