[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: What to do with values of type LUA_TFUNCTION?
- From: Jamie Webb <j@...>
- Date: Sat, 19 Jun 2004 11:36:09 +0100
On Saturday 19 June 2004 10:40, Dan East wrote:
> 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)
Don't do that. If the function is a C closure, you'll lose the upvalues.
> I would think using references is the proper way to handle what you're
> trying to do.
Yes. For both Lua and C functions (although beware that this means you're
doing reference counting, so it could lead to uncollectable reference
-- Jamie Webb