diff --git a/src/embind/embind.js b/src/embind/embind.js index 8e4626cb2b094..41e89a9197e4f 100644 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -124,17 +124,14 @@ var LibraryEmbind = { // We are exposing a function with the same name as an existing function. Create an overload table and a function selector // that routes between the two. ensureOverloadTable(Module, name, name); - if (Module.hasOwnProperty(numArguments)) { + if (Module[name].overloadTable.hasOwnProperty(numArguments)) { throwBindingError(`Cannot register multiple overloads of a function with the same number of arguments (${numArguments})!`); } // Add the new function into the overload table. Module[name].overloadTable[numArguments] = value; - } - else { + } else { Module[name] = value; - if (undefined !== numArguments) { - Module[name].numArguments = numArguments; - } + Module[name].argCount = numArguments; } }, @@ -147,8 +144,7 @@ var LibraryEmbind = { // If there's an overload table for this symbol, replace the symbol in the overload table instead. if (undefined !== Module[name].overloadTable && undefined !== numArguments) { Module[name].overloadTable[numArguments] = value; - } - else { + } else { Module[name] = value; Module[name].argCount = numArguments; } diff --git a/test/code_size/embind_hello_wasm.json b/test/code_size/embind_hello_wasm.json index 96e053fb65387..506bbbab2a7e7 100644 --- a/test/code_size/embind_hello_wasm.json +++ b/test/code_size/embind_hello_wasm.json @@ -1,10 +1,10 @@ { "a.html": 552, "a.html.gz": 380, - "a.js": 9727, - "a.js.gz": 4295, + "a.js": 9718, + "a.js.gz": 4291, "a.wasm": 7730, "a.wasm.gz": 3507, - "total": 18009, - "total_gz": 8182 + "total": 18000, + "total_gz": 8178 } diff --git a/test/embind/test_embind_invalid_overload.cpp b/test/embind/test_embind_invalid_overload.cpp new file mode 100644 index 0000000000000..c1c5c776eda11 --- /dev/null +++ b/test/embind/test_embind_invalid_overload.cpp @@ -0,0 +1,22 @@ +#include +#include + +using namespace emscripten; + +int main() { +} + +int foo(int a) { + return 0; +} + +int foo(float a) { + return 0; +} + +EMSCRIPTEN_BINDINGS(bindings) { + // Overloads in embind all need to have a unique number of arguments. + // This is invalid since both overloads take just one argument. + function("foo", select_overload(&foo)); + function("foo", select_overload(&foo)); +} diff --git a/test/test_other.py b/test/test_other.py index 5e96b205f9562..8fbd99a940658 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -3127,6 +3127,10 @@ def test_embind_fail(self): out = self.expect_fail([EMXX, test_file('embind/test_unsigned.cpp')]) self.assertContained("undefined symbol: _embind_register_function", out) + def test_embind_invalid_overload(self): + expected = 'BindingError: Cannot register multiple overloads of a function with the same number of arguments' + self.do_runf(test_file('embind/test_embind_invalid_overload.cpp'), expected, emcc_args=['-lembind'], assert_returncode=NON_ZERO) + def test_embind_asyncify(self): create_file('post.js', ''' addOnPostRun(() => {