]> granicus.if.org Git - pdns/commitdiff
LuaWrapper: Use the correct index when storing a function
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 16 Dec 2016 13:39:46 +0000 (14:39 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 16 Dec 2016 13:41:22 +0000 (14:41 +0100)
The LuaWrapper used to assume that the function was at the
top of the stack, making it effectively impossible to have
a callback function parameter anywhere else than as the last
parameter.

ext/luawrapper/include/LuaContext.hpp

index e9627d689c715c626a10f50eda5d6ea9b09ec9ea..b31e9cd399caaad34e92103f8f8d332768edc877 100644 (file)
@@ -1642,10 +1642,10 @@ private:
     // structure that will ensure that a certain is stored somewhere in the registry
     struct ValueInRegistry {
         // this constructor will clone and hold the value at the top of the stack in the registry
-        ValueInRegistry(lua_State* lua) : lua{lua}
+        ValueInRegistry(lua_State* lua, int index=-1) : lua{lua}
         {
             lua_pushlightuserdata(lua, this);
-            lua_pushvalue(lua, -2);
+            lua_pushvalue(lua, -1 + index);
             lua_settable(lua, LUA_REGISTRYINDEX);
         }
         
@@ -1821,8 +1821,8 @@ private:
 
 private:
     friend LuaContext;
-    explicit LuaFunctionCaller(lua_State* state) :
-        valueHolder(std::make_shared<ValueInRegistry>(state)),
+    explicit LuaFunctionCaller(lua_State* state, int index) :
+        valueHolder(std::make_shared<ValueInRegistry>(state, index)),
         state(state)
     {}
 };
@@ -2521,7 +2521,7 @@ struct LuaContext::Reader<LuaContext::LuaFunctionCaller<TRetValue (TParameters..
     {
         if (lua_isfunction(state, index) == 0 && lua_isuserdata(state, index) == 0)
             return boost::none;
-        return ReturnType(state);
+        return ReturnType(state, index);
     }
 };