Skip to content

Commit 4549d9d

Browse files
brendandahlHunter Richards
andauthored
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 <hunter.a.richards@gmail.com>
1 parent b725fbf commit 4549d9d

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

system/include/emscripten/bind.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,12 @@ struct GetterPolicy<GetterReturnType (GetterThisType::*)() const> {
635635
}
636636
};
637637

638+
#ifdef __cpp_noexcept_function_type
639+
template<typename GetterReturnType, typename GetterThisType>
640+
struct GetterPolicy<GetterReturnType (GetterThisType::*)() const noexcept>
641+
: GetterPolicy<GetterReturnType (GetterThisType::*)() const> {};
642+
#endif
643+
638644
template<typename GetterReturnType, typename GetterThisType>
639645
struct GetterPolicy<GetterReturnType (*)(const GetterThisType&)> {
640646
typedef GetterReturnType ReturnType;
@@ -710,6 +716,12 @@ struct SetterPolicy<SetterReturnType (SetterThisType::*)(SetterArgumentType)> {
710716
}
711717
};
712718

719+
#ifdef __cpp_noexcept_function_type
720+
template<typename SetterReturnType, typename SetterThisType, typename SetterArgumentType>
721+
struct SetterPolicy<SetterReturnType (SetterThisType::*)(SetterArgumentType) noexcept>
722+
: SetterPolicy<SetterReturnType (SetterThisType::*)(SetterArgumentType)> {};
723+
#endif
724+
713725
template<typename SetterReturnType, typename SetterThisType, typename SetterArgumentType>
714726
struct SetterPolicy<SetterReturnType (*)(SetterThisType&, SetterArgumentType)> {
715727
typedef SetterArgumentType ArgumentType;
@@ -1326,6 +1338,12 @@ struct RegisterClassMethod<ReturnType (ClassType::*)(Args...)> {
13261338
}
13271339
};
13281340

1341+
#ifdef __cpp_noexcept_function_type
1342+
template<typename ClassType, typename ReturnType, typename... Args>
1343+
struct RegisterClassMethod<ReturnType (ClassType::*)(Args...) noexcept>
1344+
: RegisterClassMethod<ReturnType (ClassType::*)(Args...)> {};
1345+
#endif
1346+
13291347
template<typename ClassType, typename ReturnType, typename... Args>
13301348
struct RegisterClassMethod<ReturnType (ClassType::*)(Args...) const> {
13311349

@@ -1347,6 +1365,12 @@ struct RegisterClassMethod<ReturnType (ClassType::*)(Args...) const> {
13471365
}
13481366
};
13491367

1368+
#ifdef __cpp_noexcept_function_type
1369+
template<typename ClassType, typename ReturnType, typename... Args>
1370+
struct RegisterClassMethod<ReturnType (ClassType::*)(Args...) const noexcept>
1371+
: RegisterClassMethod<ReturnType (ClassType::*)(Args...) const> {};
1372+
#endif
1373+
13501374
template<typename ReturnType, typename ThisType, typename... Args>
13511375
struct RegisterClassMethod<ReturnType (*)(ThisType, Args...)> {
13521376

@@ -1367,6 +1391,12 @@ struct RegisterClassMethod<ReturnType (*)(ThisType, Args...)> {
13671391
}
13681392
};
13691393

1394+
#ifdef __cpp_noexcept_function_type
1395+
template<typename ReturnType, typename ThisType, typename... Args>
1396+
struct RegisterClassMethod<ReturnType (*)(ThisType, Args...) noexcept>
1397+
: RegisterClassMethod<ReturnType (*)(ThisType, Args...)> {};
1398+
#endif
1399+
13701400
template<typename ReturnType, typename ThisType, typename... Args>
13711401
struct RegisterClassMethod<std::function<ReturnType (ThisType, Args...)>> {
13721402

tests/embind/embind_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,22 @@ class BigClass {
386386
}
387387
};
388388

389+
class NoExceptClass {
390+
public:
391+
int getValue() noexcept {
392+
return 42;
393+
}
394+
int getValueConst() const noexcept {
395+
return 43;
396+
}
397+
int getX() const noexcept { return x; }
398+
void setX(int x_) noexcept { x = x_; }
399+
private:
400+
int x;
401+
};
402+
403+
void embind_test_no_except_function(NoExceptClass&) noexcept {}
404+
389405
class ParentClass {
390406
public:
391407
ParentClass(): bigClass() {};
@@ -1970,6 +1986,12 @@ EMSCRIPTEN_BINDINGS(tests) {
19701986
.function("getMember", &TemplateClass<int>::getMember)
19711987
;
19721988

1989+
class_<NoExceptClass>("NoExceptClass")
1990+
.function("embind_test_no_except_function", &embind_test_no_except_function)
1991+
.function("getValue", &NoExceptClass::getValue)
1992+
.function("getValueConst", &NoExceptClass::getValueConst)
1993+
.property("x", &NoExceptClass::getX, &NoExceptClass::setX);
1994+
19731995
class_<ContainsTemplatedMemberClass>("ContainsTemplatedMemberClass")
19741996
.constructor<>()
19751997
.function("getTestTemplate", &ContainsTemplatedMemberClass::getTestTemplate)

tests/test_other.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,8 @@ def test_embind_closure_no_dynamic_execution(self):
24992499
def test_embind(self, extra_args):
25002500
test_cases = [
25012501
(['-lembind']),
2502+
# Ensure embind compiles under C++17 where "noexcept" became part of the function signature.
2503+
(['-lembind', '-std=c++17']),
25022504
(['-lembind', '-O1']),
25032505
(['-lembind', '-O2']),
25042506
(['-lembind', '-O2', '-sALLOW_MEMORY_GROWTH', test_file('embind/isMemoryGrowthEnabled=true.cpp')]),

0 commit comments

Comments
 (0)