Skip to content

Commit

Permalink
Allow throwing handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Mar 17, 2024
1 parent acd7dac commit 56023d9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
23 changes: 23 additions & 0 deletions Source/LuaBridge/detail/Invoke.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ class LuaResult
return LuaRef(m_L);
}

#if LUABRIDGE_HAS_EXCEPTIONS
/**
* @brief
*/
void raiseException() const
{
if (wasOk())
return;

if (std::holds_alternative<std::string>(m_data))
{
const auto& message = std::get<std::string>(m_data);
lua_pushlstring(m_L, message.c_str(), message.size());
}
else
{
lua_pushlstring(m_L, m_ec.message().c_str(), m_ec.message().size());
}

throw LuaException::fromStack(m_L, m_ec);
}
#endif

private:
template <class... Args>
friend LuaResult call(const LuaRef&, Args&&...);
Expand Down
26 changes: 15 additions & 11 deletions Source/LuaBridge/detail/LuaException.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LuaException : public std::exception
LUABRIDGE_ASSERT(areExceptionsEnabled(L));

#if LUABRIDGE_HAS_EXCEPTIONS
throw LuaException(L, code, FromLua{});
throw LuaException::fromStack(L, code);
#else
unused(L, code);

Expand Down Expand Up @@ -103,6 +103,19 @@ class LuaException : public std::exception
#endif
}

//=============================================================================================
/**
* @brief Construct a LuaException from stack.
*
* @return A Lua exception from stack.
*/
static LuaException fromStack(lua_State* L, std::error_code code = makeErrorCode(ErrorCode::LuaFunctionCallFailed))
{
auto exception = LuaException(L, code);
exception.whatFromStack();
return exception;
}

//=============================================================================================
/**
* @brief Retrieve the lua_State associated with the exception.
Expand All @@ -112,15 +125,6 @@ class LuaException : public std::exception
lua_State* state() const { return m_L; }

private:
struct FromLua {};

LuaException(lua_State* L, std::error_code code, FromLua)
: m_L(L)
, m_code(code)
{
whatFromStack();
}

void whatFromStack()
{
std::stringstream ss;
Expand All @@ -141,7 +145,7 @@ class LuaException : public std::exception
static int panicHandlerCallback(lua_State* L)
{
#if LUABRIDGE_HAS_EXCEPTIONS
throw LuaException(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed), FromLua{});
throw LuaException::fromStack(L);
#else
unused(L);

Expand Down
19 changes: 19 additions & 0 deletions Tests/Source/LuaRefTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,25 @@ TEST_F(LuaRefTests, Callable)
EXPECT_EQ(200, obj["i"].unsafe_cast<int>());
}

#if LUABRIDGE_HAS_EXCEPTIONS
TEST_F(LuaRefTests, CallableWithThrowingHandler)
{
runLua("function f(x) error('we failed ' .. x) end");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

bool calledHandler = false;
auto handler = [&](lua_State*) -> int
{
calledHandler = true;
return 0;
};

EXPECT_ANY_THROW(f.callWithHandler(handler, "badly").raiseException());
EXPECT_TRUE(calledHandler);
}
#endif

TEST_F(LuaRefTests, CallableWithAndWithoutHandler)
{
runLua("function f(x) error('we failed ' .. x) end");
Expand Down

0 comments on commit 56023d9

Please sign in to comment.