Skip to content

Commit 47ca47c

Browse files
authored
Don't use new WebAssembly.Function by default (#24581)
The js-types proposal has been, unfortunately, stuck for a couple of years now, and is only available under a flag in certain browsers. It makes sense to include it only under an explicit opt-in flag for now to save a bit of code size.
1 parent e1692b4 commit 47ca47c

14 files changed

+39
-15
lines changed

site/source/docs/tools_reference/settings_reference.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3342,3 +3342,14 @@ or chrome.
33423342
.. note:: This is an experimental setting
33433343

33443344
Default value: false
3345+
3346+
.. _wasm_js_types:
3347+
3348+
WASM_JS_TYPES
3349+
=============
3350+
3351+
Experimental support for WebAssembly js-types proposal.
3352+
It's currently only available under a flag in certain browsers,
3353+
so we disable it by default to save on code size.
3354+
3355+
Default value: false

src/lib/libaddfunction.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ addToLibrary({
1717
target.push((n % 128) | 128, n >> 7);
1818
}
1919
},
20-
20+
#if WASM_JS_TYPES
2121
// Converts a signature like 'vii' into a description of the wasm types, like
2222
// { parameters: ['i32', 'i32'], results: [] }.
2323
$sigToWasmTypes: (sig) => {
@@ -48,6 +48,7 @@ addToLibrary({
4848
}
4949
return type;
5050
},
51+
#endif
5152
$generateFuncType__deps: ['$uleb128Encode'],
5253
$generateFuncType: (sig, target) => {
5354
var sigRet = sig.slice(0, 1);
@@ -85,7 +86,13 @@ addToLibrary({
8586
},
8687
// Wraps a JS function as a wasm function with a given signature.
8788
#if !WASM2JS
88-
$convertJsFunctionToWasm__deps: ['$uleb128Encode', '$sigToWasmTypes', '$generateFuncType'],
89+
$convertJsFunctionToWasm__deps: [
90+
'$uleb128Encode',
91+
#if WASM_JS_TYPES
92+
'$sigToWasmTypes',
93+
#endif
94+
'$generateFuncType'
95+
],
8996
#endif
9097
$convertJsFunctionToWasm: (func, sig) => {
9198
#if WASM2JS
@@ -95,15 +102,15 @@ addToLibrary({
95102
#if ASSERTIONS && !WASM_BIGINT
96103
assert(!sig.includes('j'), 'i64 not permitted in function signatures when WASM_BIGINT is disabled');
97104
#endif
98-
105+
#if WASM_JS_TYPES
99106
// If the type reflection proposal is available, use the new
100107
// "WebAssembly.Function" constructor.
101108
// Otherwise, construct a minimal wasm module importing the JS function and
102109
// re-exporting it.
103110
if (typeof WebAssembly.Function == "function") {
104111
return new WebAssembly.Function(sigToWasmTypes(sig), func);
105112
}
106-
113+
#endif
107114
// The module is static, with the exception of the type section, which is
108115
// generated based on the signature passed in.
109116
var typeSectionBody = [
@@ -252,4 +259,3 @@ addToLibrary({
252259
freeTableIndexes.push(index);
253260
},
254261
});
255-

src/lib/libasync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ addToLibrary({
2020
},
2121

2222
#if ASYNCIFY
23-
$Asyncify__deps: ['$runAndAbortIfError', '$callUserCallback', '$sigToWasmTypes',
23+
$Asyncify__deps: ['$runAndAbortIfError', '$callUserCallback',
2424
#if !MINIMAL_RUNTIME
2525
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
2626
#endif

src/settings.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,11 @@ var WASM_ESM_INTEGRATION = false;
21842184
// [link]
21852185
var JS_BASE64_API = false;
21862186

2187+
// Experimental support for WebAssembly js-types proposal.
2188+
// It's currently only available under a flag in certain browsers,
2189+
// so we disable it by default to save on code size.
2190+
var WASM_JS_TYPES = false;
2191+
21872192
// For renamed settings the format is:
21882193
// [OLD_NAME, NEW_NAME]
21892194
// For removed settings (which now effectively have a fixed value and can no
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8345
1+
8337
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
22435
1+
22420
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11659
1+
11562
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27545
1+
27310

test/other/codesize/test_codesize_minimal_O0.expected.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,6 @@ Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
926926
'ccall',
927927
'cwrap',
928928
'uleb128Encode',
929-
'sigToWasmTypes',
930929
'generateFuncType',
931930
'convertJsFunctionToWasm',
932931
'getEmptyTableSlot',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6639
1+
6632
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17698
1+
17683
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
54182
1+
54162
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
52232
1+
52212

tools/link.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
821821
if settings.ABORT_ON_WASM_EXCEPTIONS:
822822
exit_with_error('WASM_ESM_INTEGRATION is not compatible with ABORT_ON_WASM_EXCEPTIONS')
823823

824+
if settings.WASM_JS_TYPES:
825+
diagnostics.warning('experimental', '-sWASM_JS_TYPES is only supported under a flag in certain browsers')
826+
824827
if settings.MODULARIZE and settings.MODULARIZE not in [1, 'instance']:
825828
exit_with_error(f'Invalid setting "{settings.MODULARIZE}" for MODULARIZE.')
826829

0 commit comments

Comments
 (0)