Skip to content

Commit 4b73e86

Browse files
committed
More checks
1 parent 3a4dc46 commit 4b73e86

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎Source/LuaBridge/detail/Invoke.h‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ namespace luabridge {
2020
//=================================================================================================
2121
namespace detail {
2222

23+
template <class F>
24+
bool is_handler_valid(const F& f)
25+
{
26+
if constexpr (std::is_pointer_v<remove_cvref_t<F>>)
27+
return f != nullptr;
28+
else if constexpr (std::is_constructible_v<bool, remove_cvref_t<F>>)
29+
return static_cast<bool>(f);
30+
else
31+
return true;
32+
}
33+
2334
template <class T>
2435
struct IsTuple : std::false_type
2536
{
@@ -105,11 +116,7 @@ TypeResult<R> callWithHandler(const Ref& object, F&& errorHandler, Args&&... arg
105116
bool hasHandler = false;
106117
if constexpr (isValidHandler)
107118
{
108-
if constexpr (std::is_constructible_v<bool, detail::remove_cvref_t<F>>)
109-
hasHandler = static_cast<bool>(errorHandler);
110-
else
111-
hasHandler = true;
112-
119+
hasHandler = detail::is_handler_valid(errorHandler);
113120
if (hasHandler)
114121
detail::push_function(L, std::forward<F>(errorHandler), "");
115122
}
@@ -129,7 +136,7 @@ TypeResult<R> callWithHandler(const Ref& object, F&& errorHandler, Args&&... arg
129136
auto ec = makeErrorCode(ErrorCode::LuaFunctionCallFailed);
130137

131138
#if LUABRIDGE_HAS_EXCEPTIONS
132-
if (! hasHandler)
139+
if constexpr (! isValidHandler)
133140
{
134141
if (LuaException::areExceptionsEnabled(L))
135142
LuaException::raise(L, ec);

‎Tests/Source/LuaRefTests.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ TEST_F(LuaRefTests, CallableWithNullifiedStdFunction)
590590

591591
std::function<int(lua_State*)> pHandler = nullptr;
592592
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
593+
594+
#if LUABRIDGE_HAS_EXCEPTIONS
595+
EXPECT_ANY_THROW(f.callWithHandler(pHandler, "badly").throw_on_error());
596+
#endif
593597
}
594598

595599
TEST_F(LuaRefTests, CallableWithCFunction)
@@ -609,11 +613,10 @@ TEST_F(LuaRefTests, CallableWithNullCFunction)
609613
EXPECT_TRUE(f.isCallable());
610614

611615
lua_CFunction pHandler = nullptr;
616+
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
612617

613618
#if LUABRIDGE_HAS_EXCEPTIONS
614-
EXPECT_ANY_THROW(f.callWithHandler(pHandler, "badly"));
615-
#else
616-
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
619+
EXPECT_ANY_THROW(f.callWithHandler(pHandler, "badly").throw_on_error());
617620
#endif
618621
}
619622

0 commit comments

Comments
 (0)