Skip to content

Commit b330159

Browse files
authored
Convert createWasm async/await where possible (#23157)
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 generate: ``` var wasmExport = await createWasm(); // returns actual exports ... 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. There are two minor observable effects of this change: 1. In `MODULARIZE` mode its no longer possible to call `new Foo()` with factory function. We already added a debug error for in #23210. 2. Because we now can `await` for createWasm to return, the `run` method can run on first call, which means it runs `main` on first call, which means main will now run before `postjs` code, just like it would with sync instantiation.
1 parent adb972c commit b330159

File tree

88 files changed

+132
-111
lines changed

Some content is hidden

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

88 files changed

+132
-111
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions

src/preamble.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,11 +1036,11 @@ function getWasmImports() {
10361036
trueModule = null;
10371037
#endif
10381038
#if SHARED_MEMORY || RELOCATABLE
1039-
receiveInstance(result['instance'], result['module']);
1039+
return receiveInstance(result['instance'], result['module']);
10401040
#else
10411041
// 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.
10421042
// When the regression is fixed, can restore the above PTHREADS-enabled path.
1043-
receiveInstance(result['instance']);
1043+
return receiveInstance(result['instance']);
10441044
#endif
10451045
}
10461046
#endif // WASM_ASYNC_COMPILATION
@@ -1076,8 +1076,7 @@ function getWasmImports() {
10761076
// Instantiate from the module posted from the main thread.
10771077
// We can just use sync instantiation in the worker.
10781078
var instance = new WebAssembly.Instance(module, getWasmImports());
1079-
receiveInstance(instance, module);
1080-
resolve();
1079+
resolve(receiveInstance(instance, module));
10811080
};
10821081
});
10831082
}
@@ -1095,16 +1094,16 @@ function getWasmImports() {
10951094
try {
10961095
#endif
10971096
var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
1098-
receiveInstantiationResult(result);
1097+
var exports = receiveInstantiationResult(result);
10991098
#if LOAD_SOURCE_MAP
11001099
receiveSourceMapJSON(await getSourceMapAsync());
11011100
#endif
1102-
return result;
1101+
return exports;
11031102
#if MODULARIZE
11041103
} catch (e) {
11051104
// If instantiation fails, reject the module ready promise.
11061105
readyPromiseReject(e);
1107-
return;
1106+
return Promise.reject(e);
11081107
}
11091108
#endif
11101109
#else // WASM_ASYNC_COMPILATION
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4380
1+
4374
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 454,
33
"a.html.gz": 328,
4-
"a.js": 4532,
5-
"a.js.gz": 2315,
4+
"a.js": 4538,
5+
"a.js.gz": 2320,
66
"a.wasm": 10402,
77
"a.wasm.gz": 6703,
8-
"total": 15388,
9-
"total_gz": 9346
8+
"total": 15394,
9+
"total_gz": 9351
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"a.html": 346,
33
"a.html.gz": 262,
4-
"a.js": 22200,
5-
"a.js.gz": 11583,
6-
"total": 22546,
7-
"total_gz": 11845
4+
"a.js": 22206,
5+
"a.js.gz": 11589,
6+
"total": 22552,
7+
"total_gz": 11851
88
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 454,
33
"a.html.gz": 328,
4-
"a.js": 4070,
5-
"a.js.gz": 2158,
4+
"a.js": 4076,
5+
"a.js.gz": 2163,
66
"a.wasm": 10402,
77
"a.wasm.gz": 6703,
8-
"total": 14926,
9-
"total_gz": 9189
8+
"total": 14932,
9+
"total_gz": 9194
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"a.html": 346,
33
"a.html.gz": 262,
4-
"a.js": 21726,
5-
"a.js.gz": 11413,
6-
"total": 22072,
7-
"total_gz": 11675
4+
"a.js": 21732,
5+
"a.js.gz": 11419,
6+
"total": 22078,
7+
"total_gz": 11681
88
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8352
1+
8351
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20347
1+
20343
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8336
1+
8334

0 commit comments

Comments
 (0)