lua-users home
lua-l archive

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


Hey

Thanks for the reply Roberto.

One other question occurred to me - what about situations where C is doing something in a situation where it hasn’t been called from Lua? Eg a C callback is triggered and calls a Lua function?
I’m thinking in that situation it probably has no guarantee about stack slots and should grow the stack for everything?

Cheers,
--
Chris Jones

On 5 Apr 2018, at 16:37, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:

>> 1) What do people think is the best practice for using these calls? Be safe
>> and call them every time you push something onto the stack from C? Follow
>> the (afaict) convention in Lua itself and only checkstack if you're going
>> to push more than 5 objects?
> 
> Lua ensures 20 free slots when it calls a C function, exactly to avoid
> the need to check the stack every time. Mostly, the code in the Lua
> libraries does not check the stack when it pushes more than 5 elements,
> but when it pushes an unknown number of elements (tipically inside a
> loop).
> 
> 
>> 2) Given that the stack has a maximum size, how come Lua doesn't grow the
>> stack automatically when it gets full?
> 
> First, it has a (small) cost. Second, and more important, several
> functions that cannot raise errors (e.g., lua_push*) would not be safe
> anymore.
> 
> 
>> 3) Just to be sure that I'm understanding the docs correctly, when pushing
>> a table, no matter how big it is, I only need two stack slots - one for
>> lua_newtable() and one that's used for each value before lua_setfield()
>> pops it off the stack. Right?
> 
> Yes.
> 
> -- Roberto
>