From e25abe4e842625e1b39294792d2a450a0a5d6a57 Mon Sep 17 00:00:00 2001 From: black-sliver <59490463+black-sliver@users.noreply.github.com> Date: Mon, 2 Sep 2024 00:32:52 +0200 Subject: [PATCH] Update Lua to 5.4.7+2 --- lib/lua | 2 +- test/lib/test_lua.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 test/lib/test_lua.cpp diff --git a/lib/lua b/lib/lua index 6baee9ef..782ef85b 160000 --- a/lib/lua +++ b/lib/lua @@ -1 +1 @@ -Subproject commit 6baee9ef9d5657ab582c8a4b9f885ec58ed502d0 +Subproject commit 782ef85b22f89d1cd1ab083202f018668d26e4b0 diff --git a/test/lib/test_lua.cpp b/test/lib/test_lua.cpp new file mode 100644 index 00000000..5703c7a2 --- /dev/null +++ b/test/lib/test_lua.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include + + +static std::weak_ptr ptr; + +static int func(lua_State *L) { + auto mem = std::make_shared(42); + ptr = mem; + asm volatile("": : :"memory"); + luaL_error(L, "error"); + return 0; +} + +TEST(LuaTest, CppStackUnwrap) { + // Lua needs to be configured to use C++ exceptions instead of longjmp for this to succeed + auto initial = std::make_shared(0); + ptr = initial; + + lua_State* L = luaL_newstate(); + ASSERT_TRUE(L); + lua_pushcfunction(L, func); + lua_setglobal(L, "func"); + luaL_loadstring(L, "func()"); + auto res = lua_pcall(L, 0, 0, 0); + EXPECT_NE(res, 0) << "Lua func did not throw"; + lua_close(L); + + if (auto p = ptr.lock()) { + ASSERT_EQ(*p, 42) << "Lua func was never run"; + } + EXPECT_TRUE(ptr.expired()) << "Lua func did not properly unwrap the stack"; +} + +TEST(LuaTest, AssignToItorator) { + // This is a breaking change for many packs in the preview of Lua 5.5 + // not sure what to do once 5.4 reaches EOL + auto script = R""""( + local table = {a = "x", b = "y", c = "z"} + local n = 0 + for k, v in pairs(table) do + k = k .. ":" -- this assignment fails in Lua 5.5 preview + n = n + 1 + end + assert(n == 3, "Iteration did not succeed") + )""""; + + lua_State* L = luaL_newstate(); + ASSERT_TRUE(L); + luaL_requiref(L, LUA_GNAME, luaopen_base, 1); + lua_pop(L, 1); + luaL_loadstring(L, script); + auto res = lua_pcall(L, 0, 0, 0); + EXPECT_EQ(res, 0) << lua_tostring(L, -1); + lua_close(L); +}