]> git.lizzy.rs Git - minetest.git/commitdiff
Modify PUC Lua to wrap C++ exceptions (#12445)
authorJude Melton-Houghton <jwmhjwmh@gmail.com>
Mon, 26 Sep 2022 11:23:48 +0000 (07:23 -0400)
committerGitHub <noreply@github.com>
Mon, 26 Sep 2022 11:23:48 +0000 (07:23 -0400)
lib/lua/src/lapi.c
lib/lua/src/ldo.c
lib/lua/src/lstate.c
lib/lua/src/lstate.h
lib/lua/src/lua.h
src/script/cpp_api/s_base.cpp
src/unittest/test_lua.cpp

index 5d5145d2ebad96ee5f14b5d06cfa9bad7dcef58c..383e65d60bec90438df02a3d659dae284807b037 100644 (file)
@@ -137,6 +137,18 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
 }
 
 
+/* MINETEST-SPECIFIC CHANGE */
+LUA_API lua_CFunctionwrapper lua_atccall (lua_State *L,
+                                          lua_CFunctionwrapper wrapf) {
+  lua_CFunctionwrapper old;
+  lua_lock(L);
+  old = G(L)->wrapcf;
+  G(L)->wrapcf = wrapf;
+  lua_unlock(L);
+  return old;
+}
+
+
 LUA_API lua_State *lua_newthread (lua_State *L) {
   lua_State *L1;
   lua_lock(L);
index d1bf786cb7202368755eeba13e3d43b49053b2df..57d2ac7c2c403283bf6587ab2f6fdbfaf8518851 100644 (file)
@@ -317,7 +317,11 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
     if (L->hookmask & LUA_MASKCALL)
       luaD_callhook(L, LUA_HOOKCALL, -1);
     lua_unlock(L);
-    n = (*curr_func(L)->c.f)(L);  /* do the actual call */
+    /* MINETEST-SPECIFIC CHANGE: Let custom code wrap C function calls. */
+    if (G(L)->wrapcf)
+      n = G(L)->wrapcf(L, *curr_func(L)->c.f);
+    else
+      n = (*curr_func(L)->c.f)(L);
     lua_lock(L);
     if (n < 0)  /* yielding? */
       return PCRYIELD;
index 4313b83a0c7cbdcdacbe57f0f2bfe30122b241d6..eced4a585094bd8b515251629953e4c3caddc521 100644 (file)
@@ -166,6 +166,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   setnilvalue(registry(L));
   luaZ_initbuffer(L, &g->buff);
   g->panic = NULL;
+  g->wrapcf = NULL; /* MINETEST-SPECIFIC CHANGE */
   g->gcstate = GCSpause;
   g->rootgc = obj2gco(L);
   g->sweepstrgc = 0;
index 3bc575b6bc8cdef8286ea4d8d539a4ee4fe5a180..c4364ea3fbd0b2fa6b2d2f814da535bf9951f13a 100644 (file)
@@ -86,6 +86,7 @@ typedef struct global_State {
   int gcpause;  /* size of pause between successive GCs */
   int gcstepmul;  /* GC `granularity' */
   lua_CFunction panic;  /* to be called in unprotected errors */
+  lua_CFunctionwrapper wrapcf; /* MINETEST-SPECIFIC CHANGE */
   TValue l_registry;
   struct lua_State *mainthread;
   UpVal uvhead;  /* head of double-linked list of all open upvalues */
index a4b73e743edeba8cbceb35b593f8993fd56e0558..1d7fe927fa30f59fc2384b39af95403115ec2e7d 100644 (file)
@@ -113,6 +113,11 @@ LUA_API lua_State *(lua_newthread) (lua_State *L);
 
 LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
 
+/* MINETEST-SPECIFIC CHANGE: Let custom code wrap C function calls. */
+typedef int (*lua_CFunctionwrapper)(lua_State *L, lua_CFunction f);
+LUA_API lua_CFunctionwrapper (lua_atccall) (lua_State *L,
+                                            lua_CFunctionwrapper wrapf);
+
 
 /*
 ** basic stack manipulation
index 659b327772c82aee9b5d9a973b39b2c3263d6a2e..5569a536e4f0f1e944c92ce469f6ced71f06ad1c 100644 (file)
@@ -109,12 +109,14 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
        lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE);
        lua_pop(m_luastack, 1); // pop debug
 
-       // If we are using LuaJIT add a C++ wrapper function to catch
-       // exceptions thrown in Lua -> C++ calls
+       // Add a C++ wrapper function to catch exceptions thrown in Lua -> C++ calls
 #if USE_LUAJIT
        lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
        luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
        lua_pop(m_luastack, 1);
+#else
+       // (This is a custom API from the bundled Lua.)
+       lua_atccall(m_luastack, script_exception_wrapper);
 #endif
 
        // Add basic globals
index fc8f895af89f52717aa7a0b996c614ddf3279973..724da108056b2f827b1439f6eae9636cff1a0123 100644 (file)
@@ -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 @@ class TestLua : public TestBase
        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);
+}