lua-users home
lua-l archive

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


On Sat, 4 Aug 2001, Edgar Toernig wrote:

> Ehem... another note: this property of string pointers is afaik not
> documented.
>
> > Essentially what I want is something like:
> >
> > int     get_symbol_id( const char* name );
> > const char*     get_symbol_name( int id );
> > void    lua_getglobal_using_id( lua_State* L, int id );
> > etc.
>
> This can be even done without knowing about the above mentioned
> property of string pointers.  Use lua_ref/getref to map strings
> to integers.

OK, I think I get this pretty well.  Although if I want to keep that char*
valid, it doesn't appear that Lua makes any guarantees, other than that
lua_getref() on the locked reference will still push a valid string.  It
seems as though the garbage collector is (and should be) still free to
move the actual data around.  So really I would need to copy the string
data out of Lua if I need it to stay around.  Not a huge problem, just
something to be aware of.

>  If you call get_symbol_id more then once for a given
> name you have to manage a lookup table though to not create
> multiple references for the same name.

This part I'm not clear on... if I do this:

	lua_pushstring( L, "test" );
	int	ref1 = lua_ref( L, 1 );
	lua_getref( L, ref1 );
	int	ref2 = lua_ref( L, 1 );
	lua_pushstring( L, "test" );
	int	ref3 = lua_ref( L, 1 );

Are ref1, ref2 and ref3 guaranteed to be equal?  Seems like they should
be, but it's not spelled out in the manual.  That's what I need for the
symbol-table idea to be useful.

-Thatcher