It was thus said that the Great Jairo A. del Rio once stated:
> Hi, list. I want to ask you about this:
>
> https://stackoverflow.com/questions/50563785/how-to-wrap-a-c-function-with-variable-arguments-using-swig/50673219
>
> Concretely, I want to know if nowadays (2022) there's a simpler or safer
> way to pass variable length arguments from Lua to C/C++.
Short answer: No.
Long answer: No, but ...
> My question
> concerns libraries using variadic functions, so setting a fixed number of
> arguments is not an option to me. Thank you in advance.
What type of function are you trying to call? If it's in the printf()
style family (format string, extra parameters), then, as stated on the Stack
Overflow page you linked, calling string.format() is your best bet. You can
even do this at the C level:
static int foo(lua_State *L)
{
luaL_checktype(L,1,LUA_TSTRING);
lua_getfield(L,1,"format");
lua_insert(L,1);
lua_call(L,lua_gettop(L) - 1,1);
printf("%s",lua_tostring(L,-1));
return 0;
}
If it's some other type of function, say execlp() [1] then finding an
alternative that doesn't use variable arguments, like execve() (which uses
arrays) is your best bet.
Sadly, such functions are not under my control.
Now, it's not impossible to do what you are asking for, but it can't be
done in C. No, you'll need to drop down to assembly language [2] if you
really want to go this route. Assembly will give you the control needed to
set up the stack (and/or registers) needed to make the call. But this
approach is, by its very nature, very CPU and OS dependent.
So... no portable solutions out there? I don't know assembly at all, but I guess I'll have to learn about it.
-spc
[1] From a link on the State Overflow page:
https://www.swig.org/Doc3.0/Varargs.html
[2] Yes, I have written a Lua module in assembly. It's not hard (if you
know assembly), just tedious.
I'd like to see that. Thank you very much.
Cordially,
Jairo Araujo