Skip to content

Commit b85a441

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 c17479b commit b85a441

File tree

88 files changed

+137
-146
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

+137
-146
lines changed

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

test/code_size/hello_webgl2_wasm.json

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
}

test/code_size/hello_webgl_wasm.json

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-
8539
1+
8534
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20918
1+
20914
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8523
1+
8520
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20886
1+
20882
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9568
1+
9566
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24764
1+
24760
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8500
1+
8498
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20813
1+
20809
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8500
1+
8498
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20813
1+
20809
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8437
1+
8435
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20498
1+
20494
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9571
1+
9569
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24764
1+
24760
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8539
1+
8534
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20918
1+
20914
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3802
1+
3798
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8562
1+
8558
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7679
1+
7677
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18826
1+
18822
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2897
1+
2895
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6203
1+
6199
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8017
1+
8015
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
21521
1+
21512
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2753
1+
2750
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6998
1+
6986
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2402
1+
2400
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4911
1+
4907
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2312
1+
2311
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4758
1+
4754
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2312
1+
2311
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4758
1+
4754
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2295
1+
2294
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4725
1+
4721
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6191
1+
6196
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13684
1+
13701
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1687
1+
1683
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3618
1+
3614
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2312
1+
2311
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4758
1+
4754
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1889
1+
1887
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3995
1+
3991
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1928
1+
1925
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4043
1+
4039
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2349
1+
2348
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4899
1+
4895
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2497
1+
2496
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5184
1+
5180
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2192
1+
2191
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4592
1+
4588
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2158
1+
2155
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4522
1+
4518
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1914
1+
1910
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4042
1+
4038
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1928
1+
1925
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4043
1+
4039
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1928
1+
1925
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4043
1+
4039
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1458
1+
1461
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3098
1+
3092
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1536
1+
1539
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3663
1+
3652
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1389
1+
1390
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2803
1+
2797
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1354
1+
1356
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2753
1+
2747
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1354
1+
1356
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2753
1+
2747
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1345
1+
1346
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2738
1+
2732
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1354
1+
1356
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2753
1+
2747
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4155
1+
4162
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8612
1+
8628
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1354
1+
1356
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2753
1+
2747
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
53979
1+
54001
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
29399
1+
29421
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
52775
1+
52797

test/test_browser.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4795,28 +4795,19 @@ def test_browser_run_from_different_directory(self):
47954795

47964796
# Similar to `test_browser_run_from_different_directory`, but asynchronous because of `-sMODULARIZE`
47974797
def test_browser_run_from_different_directory_async(self):
4798-
for args, creations in [
4799-
(['-sMODULARIZE'], [
4800-
'Module();', # documented way for using modularize
4801-
'new Module();' # not documented as working, but we support it
4802-
]),
4803-
]:
4804-
print(args)
4805-
# compile the code with the modularize feature and the preload-file option enabled
4806-
self.compile_btest('browser_test_hello_world.c', ['-o', 'test.js', '-O3'] + args)
4807-
ensure_dir('subdir')
4808-
shutil.move('test.js', Path('subdir/test.js'))
4809-
shutil.move('test.wasm', Path('subdir/test.wasm'))
4810-
for creation in creations:
4811-
print(creation)
4812-
# Make sure JS is loaded from subdirectory
4813-
create_file('test-subdir.html', '''
4814-
<script src="subdir/test.js"></script>
4815-
<script>
4816-
%s
4817-
</script>
4818-
''' % creation)
4819-
self.run_browser('test-subdir.html', '/report_result?0')
4798+
# compile the code with the modularize feature and the preload-file option enabled
4799+
self.compile_btest('browser_test_hello_world.c', ['-o', 'test.js', '-O3', '-sMODULARIZE'])
4800+
ensure_dir('subdir')
4801+
shutil.move('test.js', Path('subdir/test.js'))
4802+
shutil.move('test.wasm', Path('subdir/test.wasm'))
4803+
# Make sure JS is loaded from subdirectory
4804+
create_file('test-subdir.html', '''
4805+
<script src="subdir/test.js"></script>
4806+
<script>
4807+
Module();
4808+
</script>
4809+
''')
4810+
self.run_browser('test-subdir.html', '/report_result?0')
48204811

48214812
# Similar to `test_browser_run_from_different_directory`, but
48224813
# also also we eval the initial code, so currentScript is not present. That prevents us

test/test_unicode_js_library.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Unicode postjs: àČšñéáúÍųåêâăščếệüçλληνικάбългарскиPусскийСрпскиУкраїнська한국어中文普通话(中国大陆)普通话(香港)中文(台灣)粵語(香港)日本語हिन्दीภาษาไทย
21
Unicode snowman ☃ says hello! àČšñéáúÍųåêâăščếệüçλληνικάбългарскиPусскийСрпскиУкраїнська한국어中文普通话(中国大陆)普通话(香港)中文(台灣)粵語(香港)日本語हिन्दीภาษาไทย
2+
Unicode postjs: àČšñéáúÍųåêâăščếệüçλληνικάбългарскиPусскийСрпскиУкраїнська한국어中文普通话(中国大陆)普通话(香港)中文(台灣)粵語(香港)日本語हिन्दीภาษาไทย

0 commit comments

Comments
 (0)