Missing function in lua API: move an element on the top of the stack ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Missing function in lua API: move an element on the top of the stack ?

Mildred
Hi

I found in the lua manual a function: lua_insert which put the element
on the top of the stack at a given index.
But is there a solution to put an element at a given index on the top
of the stack (and not a copy of the element like lua_pushfield do) ?

Thanks.
-- 
Mildred
Website	<http://louve.dyndns.org> ou <http://mildred632.free.fr>
GnuPG	<http://mildred632.free.fr/gpg_key> ou hkp://pgp.mit.edu
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 <9A7D_2E2B> (ID)
Ce courrier E-mail doit Ãtre considÃrà comme une correspondance privÃe.

Attachment: signature.asc
Description: PGP signature

Reply | Threaded
Open this post in threaded view
|

Re: Missing function in lua API: move an element on the top of the stack ?

Ashwin Hirschi-2

I found in the lua manual a function: lua_insert which put the element
on the top of the stack at a given index.
But is there a solution to put an element at a given index on the top
of the stack (and not a copy of the element like lua_pushfield do) ?

I don't know about lua_pushfield, but how about using lua_pushvalue and lua_remove?

Ashwin.
--
no signature is a signature.

Reply | Threaded
Open this post in threaded view
|

Re: Missing function in lua API: move an element on the top of the stack ?

Mildred
On Mon, 07 Nov 2005 18:35:49 +0100
"Ashwin Hirschi" <[hidden email]> wrote:

> 
> > I found in the lua manual a function: lua_insert which put the
> > element on the top of the stack at a given index.
> > But is there a solution to put an element at a given index on the
> > top of the stack (and not a copy of the element like lua_pushfield
> > do) ?
> 
> I don't know about lua_pushfield, but how about using lua_pushvalue
> and lua_remove?
> 
> Ashwin.

Just a mistake. Replace lua_pushfield by lua_pushvalue ...
The problem of that function is that a copy is made. And I want the
same object

For example, If I have a table obj and a subtable obj.child. I want
obj.child.parent to be obj itseld.
Then If I modify obj.child.parend, obj is also modified (because it is
the same object)

Am I clear ?
Mildred
-- 
Mildred
Website	<http://louve.dyndns.org> ou <http://mildred632.free.fr>
GnuPG	<http://mildred632.free.fr/gpg_key> ou hkp://pgp.mit.edu
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 <9A7D_2E2B> (ID)
Ce courrier E-mail doit Ãtre considÃrà comme une correspondance privÃe.

Attachment: signature.asc
Description: PGP signature

Reply | Threaded
Open this post in threaded view
|

Re: Missing function in lua API: move an element on the top of the stack ?

Ben Sunshine-Hill
No, lua_pushvalue does not copy tables, or other objects. This will
work (tho it's untested):

// assuming the "parent" table is the only value on the stack
lua_pushstring(L, "child");
lua_gettable(L, 1);
lua_pushstring(L, "parent");
lua_pushvalue(L, 1); // copies a reference to the table, not the table itself
lua_settable(L, 2);
lua_pop(L, 1);

Ben

On 11/7/05, Mildred <[hidden email]> wrote:
> On Mon, 07 Nov 2005 18:35:49 +0100
> "Ashwin Hirschi" <[hidden email]> wrote:
>
> >
> > > I found in the lua manual a function: lua_insert which put the
> > > element on the top of the stack at a given index.
> > > But is there a solution to put an element at a given index on the
> > > top of the stack (and not a copy of the element like lua_pushfield
> > > do) ?
> >
> > I don't know about lua_pushfield, but how about using lua_pushvalue
> > and lua_remove?
> >
> > Ashwin.
>
> Just a mistake. Replace lua_pushfield by lua_pushvalue ...
> The problem of that function is that a copy is made. And I want the
> same object
>
> For example, If I have a table obj and a subtable obj.child. I want
> obj.child.parent to be obj itseld.
> Then If I modify obj.child.parend, obj is also modified (because it is
> the same object)
>
> Am I clear ?
> Mildred
> --
> Mildred
> Website <http://louve.dyndns.org> ou <http://mildred632.free.fr>
> GnuPG   <http://mildred632.free.fr/gpg_key> ou hkp://pgp.mit.edu
> Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 <9A7D_2E2B> (ID)
> Ce courrier E-mail doit être considéré comme une correspondance privée.
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Missing function in lua API: move an element on the top of the stack ?

Remo Dentato
In reply to this post by Mildred
For example, If I have a table obj and a subtable obj.child. I want
obj.child.parent to be obj itseld.
Then If I modify obj.child.parend, obj is also modified (because it is
the same object)


My understanding is that what gets pushed on the stack is the pointer to the table, not a copy of the table. If you use lua_pushvalue() and lua_remove() (as Ashwin suggested) I think you'll get exactly what you want (but I might be wrong.)

When in doubt about the stack status I use the StackDump() function that Roberto describes in his book, very helpful to trace operations on stack!

Remo D.