Skip to content

Commit 7ff7855

Browse files
authored
Fix convertJsFunctionToWasm when the signature has more than 122 arguments (#16658)
With this patch, convertJsFunctionToWasm works correctly with up to 1000 arguments. Called with 1001 arguments, Chrome, Firefox, and Node all agree on this error: WebAssembly.Module(): param count of 1001 exceeds internal limit of 1000
1 parent a856e88 commit 7ff7855

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/runtime_functions.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7+
// This gives correct answers for everything less than 2^{14} = 16384
8+
// I hope nobody is contemplating functions with 16384 arguments...
9+
function uleb128Encode(n) {
10+
#if ASSERTIONS
11+
assert(n < 16384);
12+
#endif
13+
if (n < 128) {
14+
return [n];
15+
}
16+
return [(n % 128) | 128, n >> 7];
17+
}
18+
19+
720
// Wraps a JS function as a wasm function with a given signature.
821
function convertJsFunctionToWasm(func, sig) {
922
#if WASM2JS
@@ -34,8 +47,6 @@ function convertJsFunctionToWasm(func, sig) {
3447
// The module is static, with the exception of the type section, which is
3548
// generated based on the signature passed in.
3649
var typeSection = [
37-
0x01, // id: section,
38-
0x00, // length: 0 (placeholder)
3950
0x01, // count: 1
4051
0x60, // form: func
4152
];
@@ -49,7 +60,7 @@ function convertJsFunctionToWasm(func, sig) {
4960
};
5061

5162
// Parameters, length + signatures
52-
typeSection.push(sigParam.length);
63+
typeSection = typeSection.concat(uleb128Encode(sigParam.length));
5364
for (var i = 0; i < sigParam.length; ++i) {
5465
typeSection.push(typeCodes[sigParam[i]]);
5566
}
@@ -62,9 +73,12 @@ function convertJsFunctionToWasm(func, sig) {
6273
typeSection = typeSection.concat([0x01, typeCodes[sigRet]]);
6374
}
6475

65-
// Write the overall length of the type section back into the section header
66-
// (excepting the 2 bytes for the section id and length)
67-
typeSection[1] = typeSection.length - 2;
76+
// Write the section code and overall length of the type section into the
77+
// section header
78+
typeSection = [0x01 /* Type section code */].concat(
79+
uleb128Encode(typeSection.length),
80+
typeSection
81+
);
6882

6983
// Rest of the module is static
7084
var bytes = new Uint8Array([

0 commit comments

Comments
 (0)