lua-users home
lua-l archive

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


Hi Patrick

thanks, but it's really the pointers I'm looking for. Firstly, I need to be able to store UD pointers in a weak-keyed table so that I can look it up based on a pointer coming into a C callback, and I need to be sure that Lua is going to return the same pointer for the given UD each time.

Secondly, I wanted to know whether storing a pointer to a function in a given table could be used later to determine whether the function had been updated in that table (since __nexindex only notifies of new items in a table, it's quite awkward to figure out if a function has been updated, and using UDs as proxies is real slow...). Obviously, if the function pointer might be updated by Lua, this wouldn't work, but as it is I think I'm going to try using it :-)

Finally, not too sure if refs work in weak-referenced tables....

Thanks for the pointer (no pun intended) anyway,

Lawrie

On 11/08/09 12:08, Patrick Donnelly wrote:
Hi Lawrie,

On Tue, Aug 11, 2009 at 2:56 AM, Lawrie Nichols<lawrien@gmail.com>  wrote:
I'm looking at using the pointer returned from lua_topointer() as an index
into a weak-keyed table, but it's not obvious whether Lua might ever change
these pointers during the execution of my scripts (ie whether a gc cycle
might moving things around internally).

I'm guessing that for userdata, ghe pointer cannot be changed (since Lua has
no way of knowing the relevance of the data pointed to in the userdata), but
I'm not so sure whether this would hold true for functions/tables.

I've had a look at the PiL on lua_topointer(), and all that says is that the
pointer cannot be used to to retrieve what was being pointed to.

To be honest, from trying it out, there doesn't seem to be any issues, but
I'd like to be sure.
I have not encountered an instance where lua_topointer is necessary.
You can instead use luaL_ref and luaL_unref for an integer reference
into the lua registry. Instead of pushing your light userdata with the
void pointer you stored, you can use this:

lua_rawgeti(L, LUA_REGISTRYINDEX, index);