lua-users home
lua-l archive

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

On Sunday 19 January 2003 11:35 am, "jimmyp_gr wrote:
> Since noone else answers to my question I will.I think(after a few
> tests) that the answer to the first question is yes.That is I do get
> the pointer to the actual string with lua_tostring.So the pointer I
> assign to the C var in my metamethod should be valid after the
> metamethod exits.

It's not really guaranteed, especially if a relocating GC is ever 
implemented.... references are a better solution; see below.

> On the second question:I've noticed something about a reference
> mechanism in the manual.I checked the lauxlib.c file but could not
> find out much about it.My guess is that luaL_ref creates a reference
> to some value and unref destroys it.Is that like putting that value in
> the registry?That could be usefull...

Yes, that's exactly what lua_ref does; it sticks the reference in the registry 
under a unique numeric key, and returns the key. Accessing a table member by 
numeric subscript is very fast, so instead of storing the pointer, you may 
want to store the integer returned by lua_ref. Then, when you actually want 
to get the thing, lua_getref will put it on the stack for you, and 
lua_tostring will get you the pointer. Call lua_unref when you don't need to 
ever access the string again.