lua-users home
lua-l archive

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


> Christian Vogler wrote:
> >
> > I propose a small enhancement to the API that IMHO would result in a
> > huge improvement in usability: Let every function that pushes a result
> > on the stack return the stack index of the result.

Nice idea.  It happened a couple of time to me that I wrote a stack usage
diagram beside a function to get the indexes right ;-)  The most hairy
thing is passing negative indexes around.

Roberto Ierusalimschy wrote:
>
> We thought about that for Lua 4.0, but it brings a performance problem.
> In many machines (Linux, for instance), the size of a TObject is not
> a power of 2, and so the difference between two (TObject *) is not
> an efficient operation.

Hmm... right.  How about then exporting a lua_StkId?  Something like this:

lua_StkId lua_index(lua_State *L, int index);
lua_StkId lua_s_pushvalue(lua_State *L, lua_StkId ref);

plus some compatibility functions:

#define lua_pushvalue(L,i) lua_s_pushvalue((L), lua_index((L),(i)))

By using these new "s_" functions you could even become faster.  Only
downside I see is that for this to work the Lua stack has to be fixed
(at least while within a C function).  May limit future changes...

If the lua_StkId is fully defined you could even perform arithmetic
on it (i.e. lua_s_pushvalue(L, x+1)).  And one could pass the Cbase
to the C function as a second argument...

Ciao, ET.