3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 * This class tests for two common issues that prevent correct error handling
37 * between Lua and C++.
39 * - https://luajit.org/extensions.html#exceptions
40 * - http://lua-users.org/wiki/ErrorHandlingBetweenLuaAndCplusplus
43 class TestLua : public TestBase
46 TestLua() { TestManager::registerTestModule(this); }
47 const char *getName() { return "TestLua"; }
49 void runTests(IGameDef *gamedef);
51 void testLuaDestructors();
52 void testCxxExceptions();
55 static TestLua g_test_instance;
57 void TestLua::runTests(IGameDef *gamedef)
59 TEST(testLuaDestructors);
60 TEST(testCxxExceptions);
63 ////////////////////////////////////////////////////////////////////////////////
66 Check that Lua unwinds the stack correctly when it throws errors internally.
67 (This is not the case with PUC Lua unless it was compiled as C++.)
73 class DestructorDetector {
76 DestructorDetector(bool *did_destruct) : did_destruct(did_destruct)
78 *did_destruct = false;
88 void TestLua::testLuaDestructors()
90 bool did_destruct = false;
92 lua_State *L = luaL_newstate();
93 lua_cpcall(L, [](lua_State *L) -> int {
94 DestructorDetector d(reinterpret_cast<bool*>(lua_touserdata(L, 1)));
95 luaL_error(L, "error");
100 UASSERT(did_destruct);
105 int wrapper(lua_State *L, lua_CFunction inner)
109 } catch (std::exception &e) {
110 lua_pushstring(L, e.what());
118 Check that C++ exceptions are caught and re-thrown as Lua errors.
119 This is handled by a wrapper we define ourselves.
120 (PUC Lua does not support use of such a wrapper, we have a patched version)
123 void TestLua::testCxxExceptions()
125 lua_State *L = luaL_newstate();
128 lua_pushlightuserdata(L, reinterpret_cast<void*>(wrapper));
129 luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
132 lua_atccall(L, wrapper);
135 lua_pushcfunction(L, [](lua_State *L) -> int {
136 throw std::runtime_error("example");
142 if (lua_pcall(L, 0, 0, 0) != 0) {
144 errmsg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "";
146 } catch (std::exception &e) {
153 UASSERTEQ(int, caught, 2);
154 UASSERT(errmsg.find("example") != std::string::npos);