Skip to content

Commit

Permalink
Add some additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Feb 23, 2022
1 parent 6548d5a commit 4696349
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Source/LuaBridge/detail/Invoke.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ LuaResult call(const LuaRef& object, Args&&... args)

#if LUABRIDGE_HAS_EXCEPTIONS
if (LuaException::areExceptionsEnabled())
LuaException::raise(LuaException(L, ec));
LuaException::raise(L, ec);
#else
return LuaResult::errorFromStack(L, ec);
#endif
Expand All @@ -212,7 +212,7 @@ inline int pcall(lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0)

#if LUABRIDGE_HAS_EXCEPTIONS
if (code != LUABRIDGE_LUA_OK && LuaException::areExceptionsEnabled())
LuaException::raise(LuaException(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed)));
LuaException::raise(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed));
#endif

return code;
Expand Down
10 changes: 3 additions & 7 deletions Source/LuaBridge/detail/LuaException.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,15 @@ class LuaException : public std::exception
//=============================================================================================
/**
* @brief Throw an exception or raises a luaerror when exceptions are disabled.
*
* This centralizes all the exceptions thrown, so that we can set breakpoints before the stack is
* unwound, or otherwise customize the behavior.
*/
template <class Exception>
static void raise(const Exception& e)
static void raise(lua_State* L, std::error_code code)
{
assert(areExceptionsEnabled());

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

std::abort();
#endif
Expand Down
46 changes: 46 additions & 0 deletions Tests/Source/LuaRefTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,49 @@ TEST_F(LuaRefTests, Print)
ASSERT_EQ("\"abc\"", stream.str());
}
}

TEST_F(LuaRefTests, RegisterLambdaInTable)
{
luabridge::getGlobalNamespace(L)
.beginNamespace("Entities")
.addFunction("GetLocalHero", [&]() {
auto table = luabridge::newTable(L);
table.push(L);

luabridge::getNamespaceFromStack(L)
.addProperty("index", [] { return 150; })
.addFunction("Health", [&] { return 500; });

table.pop();
return table;
})
.endNamespace();

runLua("result = Entities.GetLocalHero().Health()");
ASSERT_EQ(500, result().cast<int>());
}


TEST_F(LuaRefTests, HookTesting)
{
std::map<std::string, luabridge::LuaRef> hooklist;

auto hook = [&](const std::string& name, luabridge::LuaRef cb) {
hooklist.emplace(name, std::move(cb));
};

luabridge::getGlobalNamespace(L)
.addFunction("Hook", hook);

runLua(R"(
function hook1(type, packet)
print("lol")
end
Hook("hook1", hook1)
)");

for (auto& func : hooklist) {
func.second(0, "x");
}
}

0 comments on commit 4696349

Please sign in to comment.