Skip to content

Commit 51b5fe9

Browse files
committed
Convert instantiateAsync form callbacks to promises. NFC
1 parent 88820b6 commit 51b5fe9

File tree

82 files changed

+162
-159
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

+162
-159
lines changed

src/library_async.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,7 @@ addToLibrary({
520520
var imports = {'primary': wasmExports};
521521
// Replace '.wasm' suffix with '.deferred.wasm'.
522522
var deferred = wasmBinaryFile.slice(0, -5) + '.deferred.wasm';
523-
await new Promise((resolve) => {
524-
instantiateAsync(null, deferred, imports, resolve);
525-
});
523+
await instantiateAsync(null, deferred, imports);
526524
},
527525

528526
$Fibers__deps: ['$Asyncify', 'emscripten_stack_set_limits', '$stackRestore'],

src/preamble.js

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
847850

848851
#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,
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(
862+
#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+
},
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
});
887892
}
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
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8432
1+
8433
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20676
1+
20718
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8416
1+
8417
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20644
1+
20686
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9453
1+
9455
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24519
1+
24561
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20570
1+
20612
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20570
1+
20612
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8329
1+
8325
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20254
1+
20296
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9460
1+
9458
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24519
1+
24561
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8432
1+
8433
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20676
1+
20718
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3895
1+
3898
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8691
1+
8733
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7569
1+
7575
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18585
1+
18626
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2989
1+
2992
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6327
1+
6369
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8040
1+
8056
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21408
1+
21506
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2818
1+
2756
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7025
1+
6971
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2498
1+
2434
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5036
1+
4957
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2418
1+
2349
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4882
1+
4803
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2418
1+
2349
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4882
1+
4803
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2397
1+
2329
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4849
1+
4770
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6298
1+
6294
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13867
1+
13894
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1762
1+
1693
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3733
1+
3643
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2418
1+
2349
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4882
1+
4803
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1971
1+
1911
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4110
1+
4031
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2006
1+
1947
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4157
1+
4078
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2434
1+
2435
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5024
1+
5049
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2578
1+
2517
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5306
1+
5227
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2276
1+
2217
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4713
1+
4634
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2242
1+
2184
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4643
1+
4564
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1990
1+
1930
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4161
1+
4082
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2006
1+
1947
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4157
1+
4078
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2006
1+
1947
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4157
1+
4078
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1524
1+
1462
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3179
1+
3102
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6570
1+
6585
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17544
1+
17642
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1597
1+
1535

0 commit comments

Comments
 (0)