Skip to content

Commit 9cb0879

Browse files
committed
Convert instantiateAsync form callbacks to promises. NFC
1 parent 2f672ea commit 9cb0879

File tree

84 files changed

+164
-160
lines changed

Some content is hidden

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

84 files changed

+164
-160
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+
abort(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-
8430
1+
8435
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20665
1+
20703
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8413
1+
8417
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20633
1+
20671
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9449
1+
9456
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24508
1+
24546
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8395
1+
8400
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20559
1+
20597
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8395
1+
8400
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20559
1+
20597
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20243
1+
20281
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9457
1+
9459
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24508
1+
24546
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8430
1+
8435
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20665
1+
20703
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3893
1+
3900
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8680
1+
8718
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7565
1+
7572
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18574
1+
18612
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2985
1+
2992
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6316
1+
6354
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8037
1+
8054
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21395
1+
21488
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2814
1+
2825
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7012
1+
7111
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2497
1+
2503
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5025
1+
5063
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2414
1+
2416
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4871
1+
4909
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2414
1+
2416
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4871
1+
4909
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2394
1+
2398
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4838
1+
4876
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6297
1+
6304
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13856
1+
13894
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1760
1+
1772
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3722
1+
3760
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2414
1+
2416
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4871
1+
4909
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1970
1+
1977
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4099
1+
4137
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2003
1+
2013
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4146
1+
4184
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2431
1+
2438
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5013
1+
5051
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2578
1+
2582
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5295
1+
5333
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2273
1+
2279
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4702
1+
4740
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2239
1+
2248
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4632
1+
4670
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1987
1+
1996
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4150
1+
4188
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2003
1+
2013
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4146
1+
4184
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2003
1+
2013
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4146
1+
4184
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1524
1+
1535
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3179
1+
3216
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6570
1+
6584
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17544
1+
17637

0 commit comments

Comments
 (0)