Skip to content

Commit 97700f1

Browse files
authored
[embind] Fix typo in overloadTable error checking. NFC (#22715)
1 parent 49d2bd7 commit 97700f1

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

src/embind/embind.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,14 @@ var LibraryEmbind = {
124124
// We are exposing a function with the same name as an existing function. Create an overload table and a function selector
125125
// that routes between the two.
126126
ensureOverloadTable(Module, name, name);
127-
if (Module.hasOwnProperty(numArguments)) {
127+
if (Module[name].overloadTable.hasOwnProperty(numArguments)) {
128128
throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`);
129129
}
130130
// Add the new function into the overload table.
131131
Module[name].overloadTable[numArguments] = value;
132-
}
133-
else {
132+
} else {
134133
Module[name] = value;
135-
if (undefined !== numArguments) {
136-
Module[name].numArguments = numArguments;
137-
}
134+
Module[name].argCount = numArguments;
138135
}
139136
},
140137

@@ -147,8 +144,7 @@ var LibraryEmbind = {
147144
// If there's an overload table for this symbol, replace the symbol in the overload table instead.
148145
if (undefined !== Module[name].overloadTable && undefined !== numArguments) {
149146
Module[name].overloadTable[numArguments] = value;
150-
}
151-
else {
147+
} else {
152148
Module[name] = value;
153149
Module[name].argCount = numArguments;
154150
}

test/code_size/embind_hello_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 552,
33
"a.html.gz": 380,
4-
"a.js": 9727,
5-
"a.js.gz": 4295,
4+
"a.js": 9718,
5+
"a.js.gz": 4291,
66
"a.wasm": 7730,
77
"a.wasm.gz": 3507,
8-
"total": 18009,
9-
"total_gz": 8182
8+
"total": 18000,
9+
"total_gz": 8178
1010
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <functional>
2+
#include <emscripten/bind.h>
3+
4+
using namespace emscripten;
5+
6+
int main() {
7+
}
8+
9+
int foo(int a) {
10+
return 0;
11+
}
12+
13+
int foo(float a) {
14+
return 0;
15+
}
16+
17+
EMSCRIPTEN_BINDINGS(bindings) {
18+
// Overloads in embind all need to have a unique number of arguments.
19+
// This is invalid since both overloads take just one argument.
20+
function("foo", select_overload<int(int)>(&foo));
21+
function("foo", select_overload<int(float)>(&foo));
22+
}

test/test_other.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,10 @@ def test_embind_fail(self):
31273127
out = self.expect_fail([EMXX, test_file('embind/test_unsigned.cpp')])
31283128
self.assertContained("undefined symbol: _embind_register_function", out)
31293129

3130+
def test_embind_invalid_overload(self):
3131+
expected = 'BindingError: Cannot register multiple overloads of a function with the same number of arguments'
3132+
self.do_runf(test_file('embind/test_embind_invalid_overload.cpp'), expected, emcc_args=['-lembind'], assert_returncode=NON_ZERO)
3133+
31303134
def test_embind_asyncify(self):
31313135
create_file('post.js', '''
31323136
addOnPostRun(() => {

0 commit comments

Comments
 (0)