[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Need some help to use TFUNCTION
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 3 Jun 2015 09:19:15 -0300
> 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