// 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);
}
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)
{}
};
{
if (lua_isfunction(state, index) == 0 && lua_isuserdata(state, index) == 0)
return boost::none;
- return ReturnType(state);
+ return ReturnType(state, index);
}
};