Skip to content

Commit 4696349

Browse files
committed
Add some additional tests
1 parent 6548d5a commit 4696349

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

Source/LuaBridge/detail/Invoke.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ LuaResult call(const LuaRef& object, Args&&... args)
193193

194194
#if LUABRIDGE_HAS_EXCEPTIONS
195195
if (LuaException::areExceptionsEnabled())
196-
LuaException::raise(LuaException(L, ec));
196+
LuaException::raise(L, ec);
197197
#else
198198
return LuaResult::errorFromStack(L, ec);
199199
#endif
@@ -212,7 +212,7 @@ inline int pcall(lua_State* L, int nargs = 0, int nresults = 0, int msgh = 0)
212212

213213
#if LUABRIDGE_HAS_EXCEPTIONS
214214
if (code != LUABRIDGE_LUA_OK && LuaException::areExceptionsEnabled())
215-
LuaException::raise(LuaException(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed)));
215+
LuaException::raise(L, makeErrorCode(ErrorCode::LuaFunctionCallFailed));
216216
#endif
217217

218218
return code;

Source/LuaBridge/detail/LuaException.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,15 @@ class LuaException : public std::exception
4949
//=============================================================================================
5050
/**
5151
* @brief Throw an exception or raises a luaerror when exceptions are disabled.
52-
*
53-
* This centralizes all the exceptions thrown, so that we can set breakpoints before the stack is
54-
* unwound, or otherwise customize the behavior.
5552
*/
56-
template <class Exception>
57-
static void raise(const Exception& e)
53+
static void raise(lua_State* L, std::error_code code)
5854
{
5955
assert(areExceptionsEnabled());
6056

6157
#if LUABRIDGE_HAS_EXCEPTIONS
62-
throw e;
58+
throw LuaException(L, code, FromLua{});
6359
#else
64-
unused(e);
60+
unused(L, code);
6561

6662
std::abort();
6763
#endif

Tests/Source/LuaRefTests.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,49 @@ TEST_F(LuaRefTests, Print)
419419
ASSERT_EQ("\"abc\"", stream.str());
420420
}
421421
}
422+
423+
TEST_F(LuaRefTests, RegisterLambdaInTable)
424+
{
425+
luabridge::getGlobalNamespace(L)
426+
.beginNamespace("Entities")
427+
.addFunction("GetLocalHero", [&]() {
428+
auto table = luabridge::newTable(L);
429+
table.push(L);
430+
431+
luabridge::getNamespaceFromStack(L)
432+
.addProperty("index", [] { return 150; })
433+
.addFunction("Health", [&] { return 500; });
434+
435+
table.pop();
436+
return table;
437+
})
438+
.endNamespace();
439+
440+
runLua("result = Entities.GetLocalHero().Health()");
441+
ASSERT_EQ(500, result().cast<int>());
442+
}
443+
444+
445+
TEST_F(LuaRefTests, HookTesting)
446+
{
447+
std::map<std::string, luabridge::LuaRef> hooklist;
448+
449+
auto hook = [&](const std::string& name, luabridge::LuaRef cb) {
450+
hooklist.emplace(name, std::move(cb));
451+
};
452+
453+
luabridge::getGlobalNamespace(L)
454+
.addFunction("Hook", hook);
455+
456+
runLua(R"(
457+
function hook1(type, packet)
458+
print("lol")
459+
end
460+
461+
Hook("hook1", hook1)
462+
)");
463+
464+
for (auto& func : hooklist) {
465+
func.second(0, "x");
466+
}
467+
}

0 commit comments

Comments
 (0)