lua-users home
lua-l archive

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


Am 04.02.2010 22:10, schrieb Oliver Schmidt:
> Hello list!

Hi!

> 
> One possible way for integrating Lua with C++ code is to build a C++ 
> class that represent a Lua variable and manages its corresponding 
> stack slot. 
> 
> C++ objects of this class type are holding their stack index and are 
> responsible for releasing the corresponding stack slot on destruction
> (e.g. using the function lua_remove).

Why are you doing that? You don't get deterministic cleanup of Lua
values anyway, and IIRC, the stack slots are removed when the function
returns.

> 
> This works well as long as objects are destructed in reverse order
> of their constructions, which is normally the case, e.g.
> 
>     class C
>     {...};
>     
>     void f(C c1)
>     {
>         C c2;
>         C c3;
>     } // <- destruction order: c3, c2, c1
> 
> 
> But in some cases the destruction order may change, especially for 
> temporary objects, e.g.
> 
>     C g()
>     {
>         C c1;
>         return C(); // c1 is destructed before this temporary object
>     }               // is destroyed (at least with g++)
> 
> The problem here is that removing a stack index that is not on the top
> shifts all stack slots above down.
> 
> To solve this problem it would be fine if Lua supported "unused" stack 
> indices.

It does (sort of): Use lua_pushnil + lua_replace instead of lua_remove.


> 
> [...]
> 
> Best regards,
> Oliver
> 

Philipp