[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: What to do with values of type LUA_TFUNCTION?
- From: Tassilo von Parseval <tassilo.von.parseval@...>
- Date: Sat, 19 Jun 2004 13:09:21 +0200
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);
... /* 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.