From 4549d9da3e85c96a71a7cf56de5e203b22801bfb Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Fri, 10 Jun 2022 09:02:08 -0700 Subject: [PATCH] Add support for noexcept keyword to embind. (#17140) * Add support for noexcept keyword in C++ class methods * Remove code duplication after adding support for noexcept * Add support for noexcept keyword to embind. This continues the work from #15273 and #10613 which supports the noexcept keyword by ignoring it on c++17 compilers. I've also added a missing template for class methods and tests for each possible use of noexcept. Co-authored-by: Hunter Richards --- system/include/emscripten/bind.h | 30 ++++++++++++++++++++++++++++++ tests/embind/embind_test.cpp | 22 ++++++++++++++++++++++ tests/test_other.py | 2 ++ 3 files changed, 54 insertions(+) diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index 633652a7e3a4d..755e2645aa79e 100644 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -635,6 +635,12 @@ struct GetterPolicy { } }; +#ifdef __cpp_noexcept_function_type +template +struct GetterPolicy + : GetterPolicy {}; +#endif + template struct GetterPolicy { typedef GetterReturnType ReturnType; @@ -710,6 +716,12 @@ struct SetterPolicy { } }; +#ifdef __cpp_noexcept_function_type +template +struct SetterPolicy + : SetterPolicy {}; +#endif + template struct SetterPolicy { typedef SetterArgumentType ArgumentType; @@ -1326,6 +1338,12 @@ struct RegisterClassMethod { } }; +#ifdef __cpp_noexcept_function_type +template +struct RegisterClassMethod + : RegisterClassMethod {}; +#endif + template struct RegisterClassMethod { @@ -1347,6 +1365,12 @@ struct RegisterClassMethod { } }; +#ifdef __cpp_noexcept_function_type +template +struct RegisterClassMethod + : RegisterClassMethod {}; +#endif + template struct RegisterClassMethod { @@ -1367,6 +1391,12 @@ struct RegisterClassMethod { } }; +#ifdef __cpp_noexcept_function_type +template +struct RegisterClassMethod + : RegisterClassMethod {}; +#endif + template struct RegisterClassMethod> { diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index a20d596666bc2..6b21ac7c2a2dc 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -386,6 +386,22 @@ class BigClass { } }; +class NoExceptClass { +public: + int getValue() noexcept { + return 42; + } + int getValueConst() const noexcept { + return 43; + } + int getX() const noexcept { return x; } + void setX(int x_) noexcept { x = x_; } +private: + int x; +}; + +void embind_test_no_except_function(NoExceptClass&) noexcept {} + class ParentClass { public: ParentClass(): bigClass() {}; @@ -1970,6 +1986,12 @@ EMSCRIPTEN_BINDINGS(tests) { .function("getMember", &TemplateClass::getMember) ; + class_("NoExceptClass") + .function("embind_test_no_except_function", &embind_test_no_except_function) + .function("getValue", &NoExceptClass::getValue) + .function("getValueConst", &NoExceptClass::getValueConst) + .property("x", &NoExceptClass::getX, &NoExceptClass::setX); + class_("ContainsTemplatedMemberClass") .constructor<>() .function("getTestTemplate", &ContainsTemplatedMemberClass::getTestTemplate) diff --git a/tests/test_other.py b/tests/test_other.py index d97981f424747..049dc2e4c8d4d 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -2499,6 +2499,8 @@ def test_embind_closure_no_dynamic_execution(self): def test_embind(self, extra_args): test_cases = [ (['-lembind']), + # Ensure embind compiles under C++17 where "noexcept" became part of the function signature. + (['-lembind', '-std=c++17']), (['-lembind', '-O1']), (['-lembind', '-O2']), (['-lembind', '-O2', '-sALLOW_MEMORY_GROWTH', test_file('embind/isMemoryGrowthEnabled=true.cpp')]),