[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua strings as "atoms"???
- From: Chris Marrin <chris@...>
- Date: Mon, 16 May 2005 21:00:38 -0700
Rici Lake wrote:
On 16-May-05, at 10:37 PM, Chris Marrin wrote:
...
The other question is still outstanding, though. If I give a string to
Lua, will it always give me back the same address in every call to
lua_tostring() for that same sequence of characters? Does this work
across GC cycles, for instance?
The short answer is yes, providing the string doesn't get garbage
collected (that is, as long as you keep a reference to it somewhere).
The longer answer is that the reference manual clearly states that the
string's address will remain constant provided the string is on the
stack. This presumably gives flexibility to language designers, but the
"reference implementation" of Lua does not currently take advantage of
that.
Ok, RTFM makes it a bit more clear. When it says "stack" it really means
as long as the string exists as a valid object inside the Lua VM, right.
It implies this where it talks about the registry (3.18). So that means
I should be able to do this:
lua_pushstring(L, "foo");
const char * myFoo = lua_tostring(L, -1);
lua_pushlightuserdata(L, s);
lua_insert(L, -2);
lua_rawset(L, L_REGISTRYINDEX);
Now I can put 's' in some global place and my C++ code can use this to
do equality comparisons against strings in the stack, right? That would
be very cool since I use strings a lot. If this is all true, then I can
also push a string onto the stack like this:
lua_pushlightuserdata(L, myFoo);
lua_rawget(L, L_REGISTRYINDEX);
Any idea about whether this is faster that just calling
lua_pushstring()? Has anyone tested the performance of this? If not, I
can do it and let you know...
--
chris marrin ,""$,
cmarrin@arch.sel.sony.com b` $ ,,.
(408) 955-3049 mP b' , 1$'
Sony ,.` ,b` ,` :$$'
,|` mP ,` ,mm
,b" b" ,` ,mm m$$ ,m ,`P$$
m$` ,b` .` ,mm ,'|$P ,|"1$` ,b$P ,` :$1
b$` ,$: :,`` |$$ ,` $$` ,|` ,$$,,`"$$ .` :$|
b$| _m$`,:` :$1 ,` ,$Pm|` ` :$$,..;"' |$:
P$b, _;b$$b$1" |$$ ,` ,$$" ``' $$
```"```'" `"` `""` ""` ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'