lua-users home
lua-l archive

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


> 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
> morestuff.

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) */
{
 GDBM_FILE dbf=Pget(L,1);
 gdbm_sync(dbf);
 lua_settop(L,1);
 return 1;
}

The call to lua_settop could be replaced by lua_pushvalue(L,-1), which would
probably be clearer, though.