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