lua-users home
lua-l archive

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


Edgar Toernig wrote:

> Why that?  Only negative indexes (relative to top) change.  Positive
> indexes stay.  Just convert all negative to positive ones using lua_gettop
> in your constructor (ie: if (idx<0) idx+=lua_gettop()+1;).

It is not necessarily true that positive indexes don't change because the
API allows any array elements to be inserted or deleted, changing the index
of other values.  But as Roberto pointed out, by following some usage
conventions this issue can be avoided.

> PS:
> > 4.0 API
> >     // construction - given "stack" index
> >     lua_ref
> >     // tonumber method call
> >     lua_getref
> >     lua_tonumber
> >     lua_pop
> >     // destruction
>       lua_unref
> >     //    no API calls - trivial
>
> Or you'll have a big memleak *g*

The assumption I stated was that the object does not leave the scope of the
Lua C function.  The lua_ref is not locked in that case, so the unref is not
strictly needed although it makes things easier for the garbage collector.
By the way one call I did forget was lua_pushvalue in the constructor,
because lua_ref only works on the stack top.  I haven't figured out any
pattern in the API as far as why some functions take indexes and others only
work with the stack top :(


-John