Skip to content

Commit f2b5976

Browse files
committed
Allow the createWasm function to use async/await where possible
The advantage if using `await` in the cases where we can is that it avoids the generation or wrapper function for each export. So instead of ``` var wasmExport = createWasm(); // returns empty object ... var malloc = ((..) => (malloc = wasmExports['malloc'])() ``` We can just generate: ``` var wasmExport = createWasm(); // returns empty object ... var malloc = wasmExports['malloc']; ``` This only currently works in MODULARIZE mode where the code is running inside a factory function. Otherwise it would end up using top-level-await. One wrinkle here is that this is not currently supported when closure is enabled because we run closure only on the contents of the factory function so closure ends up seeing this as a top level await when its not.
1 parent dfe3356 commit f2b5976

File tree

85 files changed

+105
-166
lines changed

Some content is hidden

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

85 files changed

+105
-166
lines changed

src/parseTools.mjs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -971,34 +971,6 @@ function to64(x) {
971971
return `BigInt(${x})`;
972972
}
973973

974-
// Add assertions to catch common errors when using the Promise object we
975-
// return from MODULARIZE Module() invocations.
976-
function addReadyPromiseAssertions() {
977-
// Warn on someone doing
978-
//
979-
// var instance = Module();
980-
// ...
981-
// instance._main();
982-
const properties = Array.from(EXPORTED_FUNCTIONS.values());
983-
// Also warn on onRuntimeInitialized which might be a common pattern with
984-
// older MODULARIZE-using codebases.
985-
properties.push('onRuntimeInitialized');
986-
const warningEnding =
987-
' on the Promise object, instead of the instance. Use .then() to get called back with the instance, see the MODULARIZE docs in src/settings.js';
988-
const res = JSON.stringify(properties);
989-
return (
990-
res +
991-
`.forEach((prop) => {
992-
if (!Object.getOwnPropertyDescriptor(readyPromise, prop)) {
993-
Object.defineProperty(readyPromise, prop, {
994-
get: () => abort('You are getting ' + prop + '${warningEnding}'),
995-
set: () => abort('You are setting ' + prop + '${warningEnding}'),
996-
});
997-
}
998-
});`
999-
);
1000-
}
1001-
1002974
function asyncIf(condition) {
1003975
return condition ? 'async' : '';
1004976
}
@@ -1118,7 +1090,6 @@ addToCompileTimeContext({
11181090
ENVIRONMENT_IS_WORKER_THREAD,
11191091
addAtExit,
11201092
addAtInit,
1121-
addReadyPromiseAssertions,
11221093
asyncIf,
11231094
awaitIf,
11241095
buildStringArray,

src/preamble.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,11 +1028,11 @@ function getWasmImports() {
10281028
trueModule = null;
10291029
#endif
10301030
#if SHARED_MEMORY || RELOCATABLE
1031-
receiveInstance(result['instance'], result['module']);
1031+
return receiveInstance(result['instance'], result['module']);
10321032
#else
10331033
// TODO: Due to Closure regression https://github.com/google/closure-compiler/issues/3193, the above line no longer optimizes out down to the following line.
10341034
// When the regression is fixed, can restore the above PTHREADS-enabled path.
1035-
receiveInstance(result['instance']);
1035+
return receiveInstance(result['instance']);
10361036
#endif
10371037
}
10381038
#endif // WASM_ASYNC_COMPILATION
@@ -1068,8 +1068,7 @@ function getWasmImports() {
10681068
// Instantiate from the module posted from the main thread.
10691069
// We can just use sync instantiation in the worker.
10701070
var instance = new WebAssembly.Instance(module, getWasmImports());
1071-
receiveInstance(instance, module);
1072-
resolve();
1071+
resolve(receiveInstance(instance, module));
10731072
};
10741073
});
10751074
}
@@ -1085,11 +1084,11 @@ function getWasmImports() {
10851084
try {
10861085
#endif
10871086
var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
1088-
receiveInstantiationResult(result);
1087+
var exports = receiveInstantiationResult(result);
10891088
#if LOAD_SOURCE_MAP
10901089
receiveSourceMapJSON(await getSourceMapPromise());
10911090
#endif
1092-
return result;
1091+
return exports;
10931092
#if MODULARIZE
10941093
} catch (e) {
10951094
// If instantiation fails, reject the module ready promise.

src/shell.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ var readyPromise = new Promise((resolve, reject) => {
4747
readyPromiseResolve = resolve;
4848
readyPromiseReject = reject;
4949
});
50-
#if ASSERTIONS
51-
{{{ addReadyPromiseAssertions() }}}
52-
#endif
5350
#endif
5451

5552
// Determine the runtime environment we are in. You can customize this by

src/shell_minimal.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var readyPromise = new Promise((resolve, reject) => {
4141
readyPromiseResolve = resolve;
4242
readyPromiseReject = reject;
4343
});
44-
#if ASSERTIONS
45-
{{{ addReadyPromiseAssertions() }}}
46-
#endif
4744
#endif
4845

4946
#if ENVIRONMENT_MAY_BE_NODE
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8545
1+
8542
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20951
1+
20947
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8530
1+
8528
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20919
1+
20915
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9579
1+
9578
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24796
1+
24792

0 commit comments

Comments
 (0)