[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: C coding practice for top of stack
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Thu, 21 Feb 2013 17:46:23 -0300
> My question is whether it might be better, in cases like this, to set the
> top of the stack before adding more stuff. So we have just value, then
When a function starts, Lua ensures that it has a reasonable number of
free slots in the stack (20 in the current implementation). So you don't
usually need to worry about these details.
However, and this has bitten me, if you want to return the first argument
(for supporting method chaining for instance) then you want to make sure
that there are no other values in the stack, even if your function normally
just took one argument, because the user might have called it with more.
Here is a concrete example, from my lgdbm:
static int Lsync(lua_State *L) /** sync(file) */
The call to lua_settop could be replaced by lua_pushvalue(L,-1), which would
probably be clearer, though.