lua-users home
lua-l archive

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


You're right, if your function returns the value 'n' the top 'n' stack entries are returned, any excess ones are discarded.

On Sun, Nov 13, 2022 at 12:36 PM bil til <biltil52@gmail.com> wrote:
Sorry for a further maybe a bit "greedy" qeustion concerning my C code.

I have some parts of my code, where I want to "daisy-chain" a Lua
function to some other C "sub-function" of me, and for this I need to
add some additional parameters on the Lua stack from time to time, e.
g. I use my own version of luab_print like this:

int luaB_print (lua_State *L) {
  lua_pushnil( L);
  lua_insert( L, 1);
  lua_pushnil( L);
  lua_insert( L, 2);
  lua_pushnil( L);
  lua_insert( L, 3);
  return com_print_i( L, WRITEMODE_print );
}

(my internal function "com_print_i" needs three additional parameter
as first function parameters... to keep it easy I just inserted NIL
here 3 times at the start of the the stack).

In a first approach, I cleaned up the stack again tediously in
com_print_i, before I return 0 (luaB_print should not return
parameters, so I just return 0):

    for( int i= 0; i< 3; i++)
      lua_remove( L, 1);
    return 0;
}

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

So I skipped this "lua_remove" for loop, and I do not see anything bad
happening so far... .

Am I correct here? Or do I overlook any possibly nerving side effects?
(I use yield / resume inside com_print_i)?

(if a person with deeper understanding of the "Lua internals" could
look into this, I would be very happy...).


--