lua-users home
lua-l archive

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


I wonder if there is an easy way to implement the following
without doing excessive stack moves.

There are many cases where calling Lua functions from C involves a lot
of stack shuffling. For example, when using C API to iterate over
sequence using Lua iterator protocol, or when lua_CFunction is forwarding
call to another Lua function. It would help to have a more generic way to call
Lua function from C. Something like:

lua_calli(L, int fi, int argi, int reti, int nret)
fi - index of function
argi - index of first functoin argument
reti - index where first return value should be located
nret - if not LUA_MULTRET, number of returned values

so lua_call(L, a, b) would be the same as lua_calli(L, -a-1, -a, -a-1, b),
and lua_calli(L, idx, idx + 1, idx + 2, 2) where idx is the index
of iterator function, would update stack with values from the next iteration.

It is easy to implement this by duplicating function + arguments on a stack. But I don't know how to do any better, since stack position of called function
value is tightly coupled with the call frame in Lua internals.
Any ideas.