diff options
author | Jude Melton-Houghton <jwmhjwmh@gmail.com> | 2022-09-26 07:23:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-26 07:23:48 -0400 |
commit | 03428d9825cfdf2cfaed6ac9410dafccac0d4f3a (patch) | |
tree | 3f6bacfa9e074e6e523698a10b0195349612eaa8 /src/unittest/test_lua.cpp | |
parent | f916398a541dbd09cbf14409f358556bc42f5535 (diff) | |
download | minetest-03428d9825cfdf2cfaed6ac9410dafccac0d4f3a.tar.xz |
Modify PUC Lua to wrap C++ exceptions (#12445)
Diffstat (limited to 'src/unittest/test_lua.cpp')
-rw-r--r-- | src/unittest/test_lua.cpp | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/src/unittest/test_lua.cpp b/src/unittest/test_lua.cpp index fc8f895af..724da1080 100644 --- a/src/unittest/test_lua.cpp +++ b/src/unittest/test_lua.cpp @@ -19,12 +19,27 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "test.h" +#include "config.h" + +#include <stdexcept> extern "C" { -#include <lua.h> +#if USE_LUAJIT + #include <luajit.h> +#else + #include <lua.h> +#endif #include <lauxlib.h> } +/* + * This class tests for two common issues that prevent correct error handling + * between Lua and C++. + * Further reading: + * - https://luajit.org/extensions.html#exceptions + * - http://lua-users.org/wiki/ErrorHandlingBetweenLuaAndCplusplus + */ + class TestLua : public TestBase { public: @@ -34,6 +49,7 @@ public: void runTests(IGameDef *gamedef); void testLuaDestructors(); + void testCxxExceptions(); }; static TestLua g_test_instance; @@ -41,10 +57,16 @@ static TestLua g_test_instance; void TestLua::runTests(IGameDef *gamedef) { TEST(testLuaDestructors); + TEST(testCxxExceptions); } //////////////////////////////////////////////////////////////////////////////// +/* + Check that Lua unwinds the stack correctly when it throws errors internally. + (This is not the case with PUC Lua unless it was compiled as C++.) +*/ + namespace { @@ -77,3 +99,57 @@ void TestLua::testLuaDestructors() UASSERT(did_destruct); } + +namespace { + + int wrapper(lua_State *L, lua_CFunction inner) + { + try { + return inner(L); + } catch (std::exception &e) { + lua_pushstring(L, e.what()); + return lua_error(L); + } + } + +} + +/* + Check that C++ exceptions are caught and re-thrown as Lua errors. + This is handled by a wrapper we define ourselves. + (PUC Lua does not support use of such a wrapper, we have a patched version) +*/ + +void TestLua::testCxxExceptions() +{ + lua_State *L = luaL_newstate(); + +#if USE_LUAJIT + lua_pushlightuserdata(L, reinterpret_cast<void*>(wrapper)); + luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON); + lua_pop(L, 1); +#else + lua_atccall(L, wrapper); +#endif + + lua_pushcfunction(L, [](lua_State *L) -> int { + throw std::runtime_error("example"); + }); + + int caught = 0; + std::string errmsg; + try { + if (lua_pcall(L, 0, 0, 0) != 0) { + caught = 2; + errmsg = lua_isstring(L, -1) ? lua_tostring(L, -1) : ""; + } + } catch (std::exception &e) { + caught = 1; + } + + if (caught != 1) + lua_close(L); + + UASSERTEQ(int, caught, 2); + UASSERT(errmsg.find("example") != std::string::npos); +} |