Le Mercredi 3 juin 2015 14h19, Roberto Ierusalimschy <roberto@inf.puc-rio.br> a écrit :
> Another way is to store a reference in the Lua Registry:
>
> int myfuncref;
>
> static int foo(lua_State *L)
> {
>
> luaL_checktype(L,1,LUA_TFUNCTION);
> myfuncref = luaL_ref(L,LUA_REGISTRYINDEX);
> lua_pushinteger(L,myfuncref);
> lua_pushvalue(L,1);
> lua_settable(L,LUA_REGISTRYINDEX);
> return 0;
> }
luaL_ref does all the work (see the manual). This function should be
like this:
static int foo(lua_State *L) {
luaL_checktype(L,1,LUA_TFUNCTION);
myfuncref = luaL_ref(L,LUA_REGISTRYINDEX);
return 0;
}
> And to call that:
>
> static int bar(lua_State *L)
> {
> lua_pushinteger(L,myfuncref);
> lua_gettable(L,LUA_REGISTRYINDEX);
> lua_call(L,0,0);
> return 0;
> }
(It is simpler to use 'lua_rawgeti'...)
-- Roberto