Jon McClintock
2/9/2005 3:01:00 PM
On Wed, Feb 09, 2005 at 04:00:57PM +0900, E S wrote:
> > 1) Wrapping functions that take variable-length arguments (varargs). Is
> > there are standard way to do this? The best I've found is libffi,
> > which lets you construct the stack frames you need to do this, but it
> > seems like this would be a problem someone would have solved a while
> > ago.
>
> Wrapping how? Should these be callable from Ruby? Ruby can define a
> method to take variable arguments either as a RArray or as a C array,
> so you'll pick one, construct a wrapper function based your selected
> way and have that function extract the values and feed them to your
> 'actual' function.
It's not quite that easy. Ruby lets you define a method that takes a
variable number of arguments, but then how do you actually pass them to
a varargs function? Think about printf(), for example. How can I call
printf() using the contents of an arbitrary length array (without
resorting to using vprintf())?
You can't just do:
char* format;
char* args[];
...
printf(format, args);
Because that'll just pass the address of the array to the function.
You can't do:
printf(format, args[0], args[1], args[2], ...);
Unless you build some ungodly switch statement on the number of
arguments. Surely I'm not the first person to run into this?
-Jon