[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: C API question
- From: Hakki Dogusan <dogusanh@...>
- Date: Fri, 07 Jan 2011 18:21:51 +0200
07/01/2011 17:59, Roberto Ierusalimschy wrote:
I would be happy to see refs go away because they seem like a sharp,
tempting feature that actually doesn't add value.
If your userdata object needs to reference other Lua data, you
really, really, really want to use the environment mechanism because
this works with the GC whereas refs are very prone to creating
If your C code needs to reference a Lua value, I tend to recommend
using light userdata keys in the registry bearing in mind that this
creates anchors for the GC.
Exactly. The archetypal use of references were a full userdata keeping
references to other Lua values by keeping in its C structure the
integers returned by luaL_ref. The finalizer for this userdata should be
responsible to release its references when the userdata was collected.
This kind of use became obsolete with environments.
Does anyone still use references in real applications? For what?
( You may say to do my homework, I won't angry :) )
I'm using luna(r) -with Ignacio Burgueño's additions- for some of my
I found these usages in my code:
// create and push singleton
pimpl_ = new pxProxyLuaImpl(L);
/*mtix =*/ Luna<pxProxyLuaImpl>::push(L, pimpl_);
refimpl_ = luaL_ref(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, LUA_REGISTRYINDEX, refimpl_);
luaL_unref(L, LUA_REGISTRYINDEX, refimpl_);
lua_gc(L, LUA_GCCOLLECT, 0);
// this is for an event handler imlemented in Lua:
// proxy.on_paint = function(evt) pxManager:on_paint(evt) end
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_pushvalue(L, 1); //dup
ref_on_paint_ = luaL_ref(L, LUA_REGISTRYINDEX);
// calling handler from host:
lua_rawgeti(L, LUA_REGISTRYINDEX, ref_on_paint_);
lua_rawgeti(L, LUA_REGISTRYINDEX, ref_event_);
int rc = lua_pcall(L, 1, 0, 0); // 1-in,0-out
I should note that I'm doing -mostly- copy/paste programming here, ie.
above usage is not my invention :)
I'm open to suggestions...