I have a function that takes a variable number of arguments in C that I want to pass to another lua function. In pure Lua I could do something like this
function foo(...)
print(...)
end
How would I do the same thing if I were to implement the function in C?
Edit: I might have worded this poorly. What I want to do is write the function foo above, in C, using the Lua API, but I don’t know how to pass the arguments I get off the Lua stack into print, because I don’t know how many there are, and I can’t find any way to check.
The way to check the number of arguments on the stack is using the
lua_gettop()function.Try the following from the C function:
The
lua_call()will pop all the arguments from the stack so if you want to preserve them you would need to adjust it slightly to make a copy of all the arguments.