[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: A couple of Ruby API questions

e

2/9/2005 7:01:00 AM

> Lähettäjä: Jon McClintock <jammer@weak.org>
> Aihe: A couple of Ruby API questions
>
> Hi Rubyists,
>
> I'm working on wrapping a C library, and I've got a couple of things I
> can't quite figure out how to manage:
>
> 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.

> 2) Wrapping structures with flexible arrays. How do I realloc the
> structure held by a VALUE? Or do I have to create an entirely new
> Ruby object each time I need to resize the structure?

Just wrap a pointer to the struct instead if you already have the C
to manage the arrays. Whenever Ruby needs those values, have a C
function extract them. Alternatively, you could use a RArray.

> Thanks,
>
> -Jon

E



2 Answers

Jon McClintock

2/9/2005 3:01:00 PM

0

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


Lyle Johnson

2/9/2005 3:30:00 PM

0

On Thu, 10 Feb 2005 00:00:33 +0900, Jon McClintock <jammer@weak.org> wrote:

> 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? ...
>
> Unless you build some ungodly switch statement on the number of
> arguments. Surely I'm not the first person to run into this?

You're not the first person to run into this, but most people punt on
trying to find some general purpose solution to the problem (which
seems to be what you're asking for) and instead look for a way to wrap
the *specific* varargs function that makes sense for the targeted
scripting language.

There's a pretty good discussion of this issue in the SWIG documentation, here:

http://www.swig.org/Doc1.3/Va...

It includes some strategies (including the use of libffi) for dealing
with these kinds of functions.

Hope this helps,

Lyle