mail[Wesnoth-commits] r34047 - /trunk/src/scripting/lua.cpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by guillaume . melquiond on March 22, 2009 - 19:59:
Author: silene
Date: Sun Mar 22 19:59:55 2009
New Revision: 34047

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34047&view=rev
Log:
Made the Lua kernel reentrant by storing the error handler in the registry.

Modified:
    trunk/src/scripting/lua.cpp

Modified: trunk/src/scripting/lua.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/scripting/lua.cpp?rev=34047&r1=34046&r2=34047&view=diff
==============================================================================
--- trunk/src/scripting/lua.cpp (original)
+++ trunk/src/scripting/lua.cpp Sun Mar 22 19:59:55 2009
@@ -45,6 +45,7 @@
 };
 
 /* Dummy pointer for getting unique keys for Lua's registry. */
+static char const executeKey = 0;
 static char const gettextKey = 0;
 static char const getunitKey = 0;
 static char const handlerKey = 0;
@@ -581,13 +582,16 @@
        lua_pushnil(L);
        lua_setglobal(L, "loadfile");
 
-       // Push the error handler, then close debug.
-       lua_settop(L, 0);
+       // Store the error handler, then close debug.
+       lua_pushlightuserdata(L, (void *)&executeKey);
        lua_getglobal(L, "debug");
        lua_getfield(L, -1, "traceback");
        lua_remove(L, -2);
+       lua_settable(L, LUA_REGISTRYINDEX);
        lua_pushnil(L);
        lua_setglobal(L, "debug");
+
+       lua_settop(L, 0);
 }
 
 LuaKernel::~LuaKernel()
@@ -649,13 +653,19 @@
 {
        lua_State *L = mState;
 
+       // Load the error handler before the function arguments.
+       lua_pushlightuserdata(L, (void *)&executeKey);
+       lua_gettable(L, LUA_REGISTRYINDEX);
+       if (nArgs)
+               lua_insert(L, -1 - nArgs);
+
        // Compile script into a variadic function.
        int res = luaL_loadstring(L, prog);
        if (res)
        {
                std::cerr << "Failure while loading Lua script: "
                          << lua_tostring(L, -1) << '\n';
-               lua_pop(L, 1);
+               lua_pop(L, 2);
                return;
        }
 
@@ -663,12 +673,14 @@
        if (nArgs)
                lua_insert(L, -1 - nArgs);
 
-       res = lua_pcall(L, nArgs, nRets, 1);
+       res = lua_pcall(L, nArgs, nRets, -2 - nArgs);
        if (res)
        {
                std::cerr << "Failure while running Lua script: "
                          << lua_tostring(L, -1) << '\n';
-               lua_pop(L, 1);
+               lua_pop(L, 2);
                return;
        }
-}
+
+       lua_remove(L, -1 - nRets);
+}




Related Messages


Powered by MHonArc, Updated Sun Mar 22 20:00:30 2009