|
Ando Sonenblick wrote:
Is the stack top on entry to a C routine I've registered **guaranteed** to always indicate the number of parameters passed?
For Lua 5, yes, definitely. In Lua 4, no, there may be upvalues on top of the stack (but my memory of Lua 4 is fading so don't trust me).
Or another way of asking: is the stack top prior to lua pushing any arguments **guaranteed** to always be at zero?
Yes, always, always.
I'm looking to use a single C routine as a dispatcher that takes a different number of arguments:From Lua, eg:Dispatch(100, self, x, y); -- 100 means SetLocation Dispatch(101, self, "foo"); -- 101 means SetName So, is it guaranteed that in my C routine the dispatch number (100, 101, etc) will always be at index 1? And the self parameter at 2?
Yes.
But what happens if I have a bug and accidentally let my stack grow?
If it's at the application level, then you have a memory gobbler. If it's just within one C function, then it's not so bad because Lua will return the number of elements on the top of the stack that you specify, and then pop all the rest off the stack so that the stack is back to what it was before the C function was called.
Will the first argument then be at index <prior stack top> + 1?
No, never. Good questions. - Peter