Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved LuaRef call with handler #180

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion 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 Expand Up @@ -253,7 +276,17 @@ template <class Impl, class LuaRef>
template <class F, class... Args>
LuaResult LuaRefBase<Impl, LuaRef>::callWithHandler(F&& errorHandler, Args&&... args) const
{
return luabridge::callWithHandler(*this, std::forward<F>(errorHandler), std::forward<Args>(args)...);
if constexpr (! std::is_convertible_v<F, bool>)
{
return luabridge::callWithHandler(*this, std::forward<F>(errorHandler), std::forward<Args>(args)...);
}
else
{
if (errorHandler)
return luabridge::callWithHandler(*this, std::forward<F>(errorHandler), std::forward<Args>(args)...);
}

return luabridge::call(*this, std::forward<Args>(args)...);
}

} // namespace luabridge
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
83 changes: 82 additions & 1 deletion Tests/Source/LuaRefTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,18 +510,39 @@ TEST_F(LuaRefTests, Callable)
EXPECT_EQ(200, obj["i"].unsafe_cast<int>());
}

TEST_F(LuaRefTests, CallableWithHandler)
#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");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

// Call without
#if LUABRIDGE_HAS_EXCEPTIONS
EXPECT_ANY_THROW(f.call("badly"));
#else
EXPECT_FALSE(f.call("badly"));
#endif

// Call with
bool calledHandler = false;
std::string errorMessage;
auto handler = [&](lua_State*) -> int
Expand All @@ -540,6 +561,66 @@ TEST_F(LuaRefTests, CallableWithHandler)
EXPECT_TRUE(errorMessage.find("we failed badly") != std::string::npos);
}

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

bool calledHandler = false;
std::string errorMessage;
auto handler = [&](lua_State*) -> int
{
calledHandler = true;

if (auto msg = lua_tostring(L, 1))
errorMessage = msg;

return 0;
};

std::function<int(lua_State*)> pHandler = handler;

EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
EXPECT_TRUE(calledHandler);
EXPECT_TRUE(errorMessage.find("we failed badly") != std::string::npos);
}

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

std::function<int(lua_State*)> pHandler = nullptr;
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
}

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

lua_CFunction pHandler = +[](lua_State* L) { return 0; };
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
}

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

lua_CFunction pHandler = nullptr;

#if LUABRIDGE_HAS_EXCEPTIONS
EXPECT_ANY_THROW(f.callWithHandler(pHandler, "badly"));
#else
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
#endif
}

TEST_F(LuaRefTests, Pop)
{
lua_pushstring(L, "hello");
Expand Down
Loading