lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Jun 19, 2004 at 12:17:47PM +0200 Tassilo von Parseval wrote:

> On Sat, Jun 19, 2004 at 05:40:43AM -0400 Dan East wrote:
> 
> [ safely storing a LUA_TFUNCTION value somewhere for later use ]
> 
> >   I am pretty new with Lua, but from what I've been able to tell, the
> > LUA_TFUNCTION type represents both functions defined within Lua, as well
> > as C Functions pushed in from the API side.  So it is ambiguous as to
> > which type the variable actually has.
> > 
> >   I have found that I can get a reference to either type by first
> > calling lua_tocfunction.  If that returns NULL then the function has
> > been defined within Lua, and I use lua_ref to get a reference to it.
> > 
> >   To push it back on the stack, lua_pushcfunction is used (if
> > lua_tocfunction returned a non-NULL value), otherwise lua_getref is used
> > to push a Lua defined function.
> > 
> >   I would think using references is the proper way to handle what you're
> > trying to do.
> 
> References seem to be the keyword indeed. Scanning this list's archive,
> I found some notes on using luaL_ref() for that which I will try now.
> When I am done, I'll follow-up with a brief summary of what I've done
> and how far it got me.

Wonderful! That was painless indeed. I now have it working. What I did
was quite simply:

    int func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
    /* now deal with func_ref accordingly */

    ...

    /* much later, when calling this function: */
    lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref);
    lua_pushnumber(L, 1);
    ... /* other function arguments */
    
    lua_pcall(L, nargs, LUA_MULTRET, 0);

One thing worthy mention (for me anyway): It looks as though I must not
call lua_pop() after doing luaL_ref() because luaL_ref seems to have
popped off the value for me already.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval