[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: "Daisy-Chaining" of C functions with lua_insert stack data: necessary to clean up stack before return?
- From: Viacheslav Usov <via.usov@...>
- Date: Tue, 15 Nov 2022 09:07:01 +0100
On Sun, Nov 13, 2022 at 9:36 PM bil til <biltil52@gmail.com> wrote:
> ... just after thinking a bit about this (and as this is quite a
> cumbersome and tedious procedure, if I have to do this at several
> points of my C code), I thought that from my understanding of
> "invoking a function with stack", this should be NOT necessary, as on
> return the internal Lua machine anyway should strip the stack to
> exactly the same point where luaB_print was invoked.
The catch here is that this is only correct when your function is
called as a "C function" from Lua. If it was called by another
function of yours, then the Lua stack will be preserved and the caller
will have to deal with it. It may be (badly) surprised by the extra
data on the stack.
Another thing you should pay attention to is that Lua guarantees, when
calling a C function, that the stack will have LUA_MINSTACK slots. The
C function (and whatever it calls) is responsible for calling
lua_checkstack() if more stack space is required.
Cheers,
V.