[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Do strings move around?
- From: Francisco Olarte <folarte@...>
- Date: Tue, 25 Aug 2020 16:13:32 +0200
Javier:
On Tue, Aug 25, 2020 at 3:58 PM Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> On Tue, 25 Aug 2020 at 08:42, Francisco Olarte <folarte@peoplecall.com> wrote:
> > Must it be kept in the stack or is it enoguh if I keep the original
> > value referenced. I.e., I'm inside a method of one of my classes ( so
> > I have a this pointer ) and I've arrived there from a C function which
> > has the relevant userdata as first argument, index 1, and I have a
> > string at the TOS, if I do:
> The GC doesn't follow C pointers, only Lua value references. The Lua
> stack is a good temporary place. for more "durable" references some
> options are:
> - in the registry
> - as an upvalue to your C function
> - in the userdata's metatable.
This I know, and I do. But my problem is I want to cache the
lua_tostring reference.
If you read my original message, it's explained there. I do not have a
problem keeping the string alive, but the manual does not say if the
string contents, the internal representation ( "lua_tolstring returns
a pointer to a string inside the Lua state. This string always has a
zero ('\0') after its last character (as in C), but can contain other
zeros in its body. " ) can move around if I pop it from the stack (
i.e., if i set it as uservalue I know I can get it into the stack
again with getuservalue and do to_string again to get the pointer, and
this is what I currently do, but a part of my dessign would be
easier/faster if I can just keep the original lua_tostring result ).
I.e., if currently I do this on a function where 1 is one of my userdata:
lua_pushstring(L,"hola");
lua_setuservalue(L, 1,-1);
and then in another similar function I do
lua_getuservalue(L,1);
const char * s = lua_tostring(L,-1);
AND what I want to know is if this could be changed to:
lua_pushstring(L,"hola");
that = (myclass *)lua_touserdata(L,1);
that->s = lua_tostring(L,-1);
lua_setuservalue(L, 1,-1);
And in the second function
that = (myclass *)lua_touserdata(L,1);
const char * s = that->s;
I know the uservalue is alive because I've gotten at that from it, I
can insure nobody touches uservalues.
Francisco Olarte.