Skip to content

Commit 7591f1c

Browse files
authored
[embind] Stricter function argument name checking (#23524)
Distinguish between unknown argument list and the empty argument list when parsing function names. Also use early return to make the code a little more compact.
1 parent a5c9510 commit 7591f1c

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

src/lib/libembind_gen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,13 @@ var LibraryEmbind = {
499499
}
500500
argStart = 2;
501501
}
502-
if (argsName.length && argsName.length != (argTypes.length - hasThis - 1)) {
502+
if (argsName && argsName.length != (argTypes.length - hasThis - 1)) {
503503
throw new Error('Argument names should match number of parameters.');
504504
}
505505

506506
const args = [];
507507
for (let i = argStart, x = 0; i < argTypes.length; i++) {
508-
if (x < argsName.length) {
508+
if (argsName) {
509509
args.push(new Argument(argsName[x++], argTypes[i]));
510510
} else {
511511
args.push(new Argument(`_${i - argStart}`, argTypes[i]));

src/lib/libembind_shared.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,21 @@ var LibraryEmbindShared = {
126126
$getFunctionName: (signature) => {
127127
signature = signature.trim();
128128
const argsIndex = signature.indexOf("(");
129-
if (argsIndex !== -1) {
129+
if (argsIndex === -1) return signature;
130130
#if ASSERTIONS
131-
assert(signature.endsWith(")"), "Parentheses for argument names should match.");
131+
assert(signature.endsWith(")"), "Parentheses for argument names should match.");
132132
#endif
133-
return signature.slice(0, argsIndex);
134-
} else {
135-
return signature;
136-
}
133+
return signature.slice(0, argsIndex);
137134
},
138135
$getFunctionArgsName__deps: [],
139136
$getFunctionArgsName: (signature) => {
140137
signature = signature.trim();
141-
const argsIndex = signature.indexOf("(") + 1;
142-
if (argsIndex !== 0) {
138+
const argsIndex = signature.indexOf("(");
139+
if (argsIndex == -1) return; // Return undefined to mean we don't have any argument names
143140
#if ASSERTIONS
144-
assert(signature.endsWith(")"), "Parentheses for argument names should match.");
141+
assert(signature.endsWith(")"), "Parentheses for argument names should match.");
145142
#endif
146-
return signature.slice(argsIndex, -1).replaceAll(" ", "").split(",").filter(n => n.length);
147-
} else {
148-
return [];
149-
}
143+
return signature.slice(argsIndex + 1, -1).replaceAll(" ", "").split(",").filter(n => n.length);
150144
},
151145
$heap32VectorToArray: (count, firstElement) => {
152146
var array = [];

test/code_size/embind_hello_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"a.html": 552,
33
"a.html.gz": 380,
44
"a.js": 9878,
5-
"a.js.gz": 4285,
5+
"a.js.gz": 4282,
66
"a.wasm": 7348,
77
"a.wasm.gz": 3375,
88
"total": 17778,
9-
"total_gz": 8040
9+
"total_gz": 8037
1010
}

0 commit comments

Comments
 (0)