Skip to content

Commit e763c3d

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 1ddef04 commit e763c3d

File tree

86 files changed

+203
-205
lines changed

Some content is hidden

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

86 files changed

+203
-205
lines changed

src/library.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -2181,16 +2181,13 @@ addToLibrary({
21812181
return x.startsWith('dynCall_') ? x : '_' + x;
21822182
},
21832183

2184-
$asyncLoad: (url) => {
2185-
return new Promise((resolve, reject) => {
2186-
readAsync(url).then(
2187-
(arrayBuffer) => {
2184+
$asyncLoad__docs: '/** @param {boolean=} noRunDep */',
2185+
$asyncLoad: async (url, noRunDep) => {
2186+
var arrayBuffer = await readAsync(url);
21882187
#if ASSERTIONS
2189-
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2188+
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
21902189
#endif
2191-
resolve(new Uint8Array(arrayBuffer));
2192-
}, reject);
2193-
});
2190+
return new Uint8Array(arrayBuffer);
21942191
},
21952192

21962193
$alignMemory: (size, alignment) => {

src/preamble.js

+93-100
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ function getBinarySync(file) {
645645
#endif
646646
}
647647

648-
function getBinaryPromise(binaryFile) {
648+
async function getWasmBinary(binaryFile) {
649649
#if !SINGLE_FILE
650650
// If we don't have the binary yet, load it asynchronously using readAsync.
651651
if (!wasmBinary
@@ -654,16 +654,18 @@ function getBinaryPromise(binaryFile) {
654654
#endif
655655
) {
656656
// 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+
}
662664
}
663665
#endif
664666

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

669671
#if LOAD_SOURCE_MAP
@@ -771,56 +773,47 @@ function resetPrototype(constructor, attrs) {
771773
#endif
772774

773775
#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);
775780
#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);
777784
#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}`);
794788
#if WASM == 2
795789
#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;
808800
}
801+
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
802+
}
809803
#endif
810804
#endif // WASM == 2
811805

812806
#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+
}
817811
#endif
818-
abort(reason);
819-
});
820-
});
812+
abort(reason);
813+
}
821814
}
822815

823-
function instantiateAsync(binary, binaryFile, imports) {
816+
async function instantiateAsync(binary, binaryFile, imports) {
824817
#if !SINGLE_FILE
825818
if (!binary &&
826819
typeof WebAssembly.instantiateStreaming == 'function' &&
@@ -839,56 +832,44 @@ function instantiateAsync(binary, binaryFile, imports) {
839832
!ENVIRONMENT_IS_NODE &&
840833
#endif
841834
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' }") }}});
851837
#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);
862847
#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;
879863
#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+
};
892873
}
893874
#endif
894875
return instantiateArrayBuffer(binaryFile, imports);
@@ -934,7 +915,13 @@ function getWasmImports() {
934915

935916
// Create the wasm instance.
936917
// 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
937923
function createWasm() {
924+
#endif
938925
// Load the wasm module and create an instance of using native support in the JS engine.
939926
// handle a generated wasm instance, receiving its exports and
940927
// performing other necessary setup
@@ -1102,17 +1089,23 @@ function createWasm() {
11021089
#if RUNTIME_DEBUG
11031090
dbg('asynchronously preparing wasm');
11041091
#endif
1105-
instantiateAsync(wasmBinary, wasmBinaryFile, info).then(receiveInstantiationResult)
11061092
#if MODULARIZE
1107-
// If instantiation fails, reject the module ready promise.
1108-
.catch(readyPromiseReject)
1093+
try {
11091094
#endif
1110-
;
1095+
var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
1096+
receiveInstantiationResult(result);
11111097
#if LOAD_SOURCE_MAP
1112-
getSourceMapPromise().then(receiveSourceMapJSON);
1098+
receiveSourceMapJSON(await getSourceMapPromise());
11131099
#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
11161109
var result = instantiateSync(wasmBinaryFile, info);
11171110
#if PTHREADS || MAIN_MODULE
11181111
return receiveInstance(result[0], result[1]);
@@ -1122,7 +1115,7 @@ function createWasm() {
11221115
// When the regression is fixed, we can remove this if/else.
11231116
return receiveInstance(result[0]);
11241117
#endif
1125-
#endif
1118+
#endif // WASM_ASYNC_COMPILATION
11261119
}
11271120

11281121
#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-
6276
1+
6256
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13831
1+
13778
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1763
1+
1748

0 commit comments

Comments
 (0)