Subject:
Re: [ruby-ffi] FFI from C?
From:
Mark Wotton
Date:
4/5/10 3:42 AM
To:
ruby-ffi@googlegroups.com

On Mon, Apr 5, 2010 at 6:10 PM, Wayne Meissner <wmeissner@gmail.com> wrote:
> Short answer is 'no' - there is no C api for Ruby-FFI.
>
> However, can you drive the integration from the ruby side, ala the lua
> bindings (http://github.com/jmettraux/rufus-lua)?
>
> I had a quick search for calling Haskell from C, and it looks like it
> might be doable - if its callable from straight C, then you can call
> it via FFI.
>
> Basically how it would go is:
>
> 1) create ruby wrapper method
> 2) massage ruby data into basic C types (int, string, struct, raw
> chunk of memory, etc)
> 3) call C method exported by Haskell with massaged parameters
> 4) convert Haskell result back into ruby type.
>
> Calling back into ruby from Haskell via a C function pointer works
> similarly, but in reverse.
>
> There was an example on stackoverflow using DL to do this - FFI would
> work much the same.
>
> If you're doing it the other way round, calling ruby from Haskell,
> you're doomed.  There is no common C api, and no C api at all on JRuby
> or IronRuby.

The use case is mostly to have Ruby calling Haskell - Haskell does
call back in to the MRI runtime currently, but only to query data
(accessing hashes/arrays etc). The tricky bit is that it then needs to
have libruby (or something similar) to build against, so you can then
dlopen the library in ruby. I could get around this by not including
ruby-specific code on the Haskell side, but part of the reason for
doing it on the Haskell side is that I can scan a Haskell module and
automatically export anything that would be callable from Ruby, which
is a rather nice feature. What I was hoping for was an
interpreter-agnostic C api to link against, but I guess that if that's
not available, I'll write my own C API for MRI 1.8 and 1.9 and add
JRuby support when I get a request for it.

Anyway, thank you for your considered response. The FFI is a really
good step forward for ruby interop, it's just a pity it's orthogonal
to my needs.

Cheers
Mark

-- A UNIX signature isn't a return address, it's the ASCII equivalent of a black velvet clown painting. It's a rectangle of carets surrounding a quote from a literary giant of weeniedom like Heinlein or Dr. Who. -- Chris Maeda
-- To unsubscribe, reply using "remove me" as the subject.