]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_lua.cpp
Modify PUC Lua to wrap C++ exceptions (#12445)
[minetest.git] / src / unittest / test_lua.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
5
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.
10
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.
15
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.
19 */
20
21 #include "test.h"
22 #include "config.h"
23
24 #include <stdexcept>
25
26 extern "C" {
27 #if USE_LUAJIT
28         #include <luajit.h>
29 #else
30         #include <lua.h>
31 #endif
32 #include <lauxlib.h>
33 }
34
35 /*
36  * This class tests for two common issues that prevent correct error handling
37  * between Lua and C++.
38  * Further reading:
39  * - https://luajit.org/extensions.html#exceptions
40  * - http://lua-users.org/wiki/ErrorHandlingBetweenLuaAndCplusplus
41  */
42
43 class TestLua : public TestBase
44 {
45 public:
46         TestLua() { TestManager::registerTestModule(this); }
47         const char *getName() { return "TestLua"; }
48
49         void runTests(IGameDef *gamedef);
50
51         void testLuaDestructors();
52         void testCxxExceptions();
53 };
54
55 static TestLua g_test_instance;
56
57 void TestLua::runTests(IGameDef *gamedef)
58 {
59         TEST(testLuaDestructors);
60         TEST(testCxxExceptions);
61 }
62
63 ////////////////////////////////////////////////////////////////////////////////
64
65 /*
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++.)
68 */
69
70 namespace
71 {
72
73         class DestructorDetector {
74                 bool *did_destruct;
75         public:
76                 DestructorDetector(bool *did_destruct) : did_destruct(did_destruct)
77                 {
78                         *did_destruct = false;
79                 }
80                 ~DestructorDetector()
81                 {
82                         *did_destruct = true;
83                 }
84         };
85
86 }
87
88 void TestLua::testLuaDestructors()
89 {
90         bool did_destruct = false;
91
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");
96                 return 0;
97         }, &did_destruct);
98         lua_close(L);
99
100         UASSERT(did_destruct);
101 }
102
103 namespace {
104
105         int wrapper(lua_State *L, lua_CFunction inner)
106         {
107                 try {
108                         return inner(L);
109                 } catch (std::exception &e) {
110                         lua_pushstring(L, e.what());
111                         return lua_error(L);
112                 }
113         }
114
115 }
116
117 /*
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)
121 */
122
123 void TestLua::testCxxExceptions()
124 {
125         lua_State *L = luaL_newstate();
126
127 #if USE_LUAJIT
128         lua_pushlightuserdata(L, reinterpret_cast<void*>(wrapper));
129         luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
130         lua_pop(L, 1);
131 #else
132         lua_atccall(L, wrapper);
133 #endif
134
135         lua_pushcfunction(L, [](lua_State *L) -> int {
136                 throw std::runtime_error("example");
137         });
138
139         int caught = 0;
140         std::string errmsg;
141         try {
142                 if (lua_pcall(L, 0, 0, 0) != 0) {
143                         caught = 2;
144                         errmsg = lua_isstring(L, -1) ? lua_tostring(L, -1) : "";
145                 }
146         } catch (std::exception &e) {
147                 caught = 1;
148         }
149
150         if (caught != 1)
151                 lua_close(L);
152
153         UASSERTEQ(int, caught, 2);
154         UASSERT(errmsg.find("example") != std::string::npos);
155 }