Skip to content

Commit 0e6dd26

Browse files
committed
Use async/await instead of promises for wasm loading. NFC
These get lowered away by babel when targetting older engines.
1 parent 89a5b12 commit 0e6dd26

File tree

82 files changed

+195
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+195
-194
lines changed

src/preamble.js

+93-100
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ function getBinarySync(file) {
647647
#endif
648648
}
649649

650-
function getBinaryPromise(binaryFile) {
650+
async function getWasmBinary(binaryFile) {
651651
#if !SINGLE_FILE
652652
// If we don't have the binary yet, load it asynchronously using readAsync.
653653
if (!wasmBinary
@@ -656,16 +656,18 @@ function getBinaryPromise(binaryFile) {
656656
#endif
657657
) {
658658
// Fetch the binary using readAsync
659-
return readAsync(binaryFile).then(
660-
(response) => new Uint8Array(/** @type{!ArrayBuffer} */(response)),
661-
// Fall back to getBinarySync if readAsync fails
662-
() => getBinarySync(binaryFile)
663-
);
659+
try {
660+
/** @type{!ArrayBuffer} */
661+
var response = await readAsync(binaryFile);
662+
return new Uint8Array(response);
663+
} catch {
664+
// Fall back to getBinarySync below;
665+
}
664666
}
665667
#endif
666668

667669
// Otherwise, getBinarySync should be able to get it synchronously
668-
return Promise.resolve(getBinarySync(binaryFile));
670+
return getBinarySync(binaryFile);
669671
}
670672

671673
#if LOAD_SOURCE_MAP
@@ -773,56 +775,47 @@ function resetPrototype(constructor, attrs) {
773775
#endif
774776

775777
#if WASM_ASYNC_COMPILATION
776-
function instantiateArrayBuffer(binaryFile, imports) {
778+
async function instantiateArrayBuffer(binaryFile, imports) {
779+
try {
780+
var binary = await getWasmBinary(binaryFile);
781+
var instance = await WebAssembly.instantiate(binary, imports);
777782
#if USE_OFFSET_CONVERTER
778-
var savedBinary;
783+
// wasmOffsetConverter needs to be assigned before calling resolve.
784+
// See comments below in instantiateAsync.
785+
wasmOffsetConverter = new WasmOffsetConverter(binary, instance.module);
779786
#endif
780-
return new Promise((resolve, reject) => {
781-
getBinaryPromise(binaryFile).then((binary) => {
782-
#if USE_OFFSET_CONVERTER
783-
savedBinary = binary;
784-
#endif
785-
return WebAssembly.instantiate(binary, imports);
786-
#if USE_OFFSET_CONVERTER
787-
}).then((instance) => {
788-
// wasmOffsetConverter needs to be assigned before calling resolve.
789-
// See comments below in instantiateAsync.
790-
wasmOffsetConverter = new WasmOffsetConverter(savedBinary, instance.module);
791-
return instance;
792-
#endif
793-
}).then(resolve, (reason) => {
794-
err(`failed to asynchronously prepare wasm: ${reason}`);
795-
787+
return instance;
788+
} catch (reason) {
789+
err(`failed to asynchronously prepare wasm: ${reason}`);
796790
#if WASM == 2
797791
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
798-
if (typeof location != 'undefined') {
799-
#endif
800-
// WebAssembly compilation failed, try running the JS fallback instead.
801-
var search = location.search;
802-
if (search.indexOf('_rwasm=0') < 0) {
803-
location.href += (search ? search + '&' : '?') + '_rwasm=0';
804-
// Return here to avoid calling abort() below. The application
805-
// still has a chance to start successfully do we don't want to
806-
// trigger onAbort or onExit handlers.
807-
return;
808-
}
809-
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
792+
if (typeof location != 'undefined') {
793+
#endif
794+
// WebAssembly compilation failed, try running the JS fallback instead.
795+
var search = location.search;
796+
if (search.indexOf('_rwasm=0') < 0) {
797+
location.href += (search ? search + '&' : '?') + '_rwasm=0';
798+
// Return here to avoid calling abort() below. The application
799+
// still has a chance to start successfully do we don't want to
800+
// trigger onAbort or onExit handlers.
801+
return;
810802
}
803+
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
804+
}
811805
#endif
812806
#endif // WASM == 2
813807

814808
#if ASSERTIONS
815-
// Warn on some common problems.
816-
if (isFileURI(wasmBinaryFile)) {
817-
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`);
818-
}
809+
// Warn on some common problems.
810+
if (isFileURI(wasmBinaryFile)) {
811+
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`);
812+
}
819813
#endif
820-
abort(reason);
821-
});
822-
});
814+
abort(reason);
815+
}
823816
}
824817

825-
function instantiateAsync(binary, binaryFile, imports) {
818+
async function instantiateAsync(binary, binaryFile, imports) {
826819
#if !SINGLE_FILE
827820
if (!binary &&
828821
typeof WebAssembly.instantiateStreaming == 'function' &&
@@ -841,56 +834,44 @@ function instantiateAsync(binary, binaryFile, imports) {
841834
!ENVIRONMENT_IS_NODE &&
842835
#endif
843836
typeof fetch == 'function') {
844-
return new Promise((resolve) => {
845-
fetch(binaryFile, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}}).then((response) => {
846-
// Suppress closure warning here since the upstream definition for
847-
// instantiateStreaming only allows Promise<Repsponse> rather than
848-
// an actual Response.
849-
// TODO(https://github.com/google/closure-compiler/pull/3913): Remove if/when upstream closure is fixed.
850-
/** @suppress {checkTypes} */
851-
var result = WebAssembly.instantiateStreaming(response, imports);
852-
837+
try {
838+
var response = fetch(binaryFile, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}});
853839
#if USE_OFFSET_CONVERTER
854-
// We need the wasm binary for the offset converter. Clone the response
855-
// in order to get its arrayBuffer (cloning should be more efficient
856-
// than doing another entire request).
857-
// (We must clone the response now in order to use it later, as if we
858-
// try to clone it asynchronously lower down then we will get a
859-
// "response was already consumed" error.)
860-
var clonedResponsePromise = response.clone().arrayBuffer();
861-
#endif
862-
863-
result.then(
840+
// We need the wasm binary for the offset converter. Clone the response
841+
// in order to get its arrayBuffer (cloning should be more efficient
842+
// than doing another entire request).
843+
// (We must clone the response now in order to use it later, as if we
844+
// try to clone it asynchronously lower down then we will get a
845+
// "response was already consumed" error.)
846+
var clonedResponse = (await response).clone();
847+
#endif
848+
var result = WebAssembly.instantiateStreaming(response, imports);
864849
#if USE_OFFSET_CONVERTER
865-
(instantiationResult) => {
866-
// When using the offset converter, we must interpose here. First,
867-
// the instantiation result must arrive (if it fails, the error
868-
// handling later down will handle it). Once it arrives, we can
869-
// initialize the offset converter. And only then is it valid to
870-
// call receiveInstantiationResult, as that function will use the
871-
// offset converter (in the case of pthreads, it will create the
872-
// pthreads and send them the offsets along with the wasm instance).
873-
874-
clonedResponsePromise.then((arrayBufferResult) => {
875-
wasmOffsetConverter = new WasmOffsetConverter(new Uint8Array(arrayBufferResult), instantiationResult.module);
876-
resolve(instantiationResult);
877-
},
878-
(reason) => err(`failed to initialize offset-converter: ${reason}`)
879-
);
880-
},
850+
// When using the offset converter, we must interpose here. First,
851+
// the instantiation result must arrive (if it fails, the error
852+
// handling later down will handle it). Once it arrives, we can
853+
// initialize the offset converter. And only then is it valid to
854+
// call receiveInstantiationResult, as that function will use the
855+
// offset converter (in the case of pthreads, it will create the
856+
// pthreads and send them the offsets along with the wasm instance).
857+
var instantiationResult = await result;
858+
var arrayBufferResult = await clonedResponse.arrayBuffer();
859+
try {
860+
wasmOffsetConverter = new WasmOffsetConverter(new Uint8Array(arrayBufferResult), instantiationResult.module);
861+
} catch (reason) {
862+
err(`failed to initialize offset-converter: ${reason}`);
863+
}
864+
return instantiationResult;
881865
#else
882-
resolve,
883-
#endif
884-
(reason) => {
885-
// We expect the most common failure cause to be a bad MIME type for the binary,
886-
// in which case falling back to ArrayBuffer instantiation should work.
887-
err(`wasm streaming compile failed: ${reason}`);
888-
err('falling back to ArrayBuffer instantiation');
889-
return resolve(instantiateArrayBuffer(binaryFile, imports));
890-
}
891-
);
892-
});
893-
});
866+
return await result;
867+
#endif
868+
} catch (reason) {
869+
// We expect the most common failure cause to be a bad MIME type for the binary,
870+
// in which case falling back to ArrayBuffer instantiation should work.
871+
err(`wasm streaming compile failed: ${reason}`);
872+
err('falling back to ArrayBuffer instantiation');
873+
// fall back of instantiateArrayBuffer below
874+
};
894875
}
895876
#endif
896877
return instantiateArrayBuffer(binaryFile, imports);
@@ -936,7 +917,13 @@ function getWasmImports() {
936917

937918
// Create the wasm instance.
938919
// Receives the wasm imports, returns the exports.
920+
#if WASM_ASYNC_COMPILATION
921+
// Funnily enough in JS the `async` keyword has to be on the same line as the
922+
// function keyword.
923+
async function createWasm() {
924+
#else
939925
function createWasm() {
926+
#endif
940927
// Load the wasm module and create an instance of using native support in the JS engine.
941928
// handle a generated wasm instance, receiving its exports and
942929
// performing other necessary setup
@@ -1104,17 +1091,23 @@ function createWasm() {
11041091
#if RUNTIME_DEBUG
11051092
dbg('asynchronously preparing wasm');
11061093
#endif
1107-
instantiateAsync(wasmBinary, wasmBinaryFile, info).then(receiveInstantiationResult)
11081094
#if MODULARIZE
1109-
// If instantiation fails, reject the module ready promise.
1110-
.catch(readyPromiseReject)
1095+
try {
11111096
#endif
1112-
;
1097+
var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
1098+
receiveInstantiationResult(result);
11131099
#if LOAD_SOURCE_MAP
1114-
getSourceMapPromise().then(receiveSourceMapJSON);
1100+
receiveSourceMapJSON(await getSourceMapPromise());
11151101
#endif
1116-
return {}; // no exports yet; we'll fill them in later
1117-
#else
1102+
return result;
1103+
#if MODULARIZE
1104+
} catch (e) {
1105+
// If instantiation fails, reject the module ready promise.
1106+
readyPromiseReject(e);
1107+
throw e;
1108+
}
1109+
#endif
1110+
#else // WASM_ASYNC_COMPILATION
11181111
var result = instantiateSync(wasmBinaryFile, info);
11191112
#if PTHREADS || MAIN_MODULE
11201113
return receiveInstance(result[0], result[1]);
@@ -1124,7 +1117,7 @@ function createWasm() {
11241117
// When the regression is fixed, we can remove this if/else.
11251118
return receiveInstance(result[0]);
11261119
#endif
1127-
#endif
1120+
#endif // WASM_ASYNC_COMPILATION
11281121
}
11291122

11301123
#if !WASM_BIGINT
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8619
1+
8602
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21051
1+
21018
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8603
1+
8586
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21019
1+
20986
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9650
1+
9642
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24894
1+
24863
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8591
1+
8577
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20944
1+
20912
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8591
1+
8577
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20944
1+
20912
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8518
1+
8503
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20630
1+
20597
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9654
1+
9644
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24894
1+
24863
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8619
1+
8602
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21051
1+
21018
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3887
1+
3862
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8687
1+
8655
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7755
1+
7739
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18958
1+
18925
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2984
1+
2957
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6324
1+
6294
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8042
1+
8034
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21457
1+
21479
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2818
1+
2814
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7080
1+
7063
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2492
1+
2460
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5032
1+
5003
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2404
1+
2373
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4878
1+
4849
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2404
1+
2373
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4878
1+
4849
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2386
1+
2356
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4845
1+
4816
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6274
1+
6256
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13816
1+
13778
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1763
1+
1748
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3732
1+
3707
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2404
1+
2373
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4878
1+
4849
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1969
1+
1945
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4109
1+
4085
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2000
1+
1984
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4156
1+
4132

0 commit comments

Comments
 (0)