[MacRuby-devel] Interfacing with C (not objective C)

Laurent Sansonetti lsansonetti at apple.com
Wed Mar 18 11:33:51 PDT 2009


Hi Martin & Brian,

On Mar 17, 2009, at 9:25 PM, Brian Chapados wrote:

> I think that if you need to make use extensive use of C
> functions/libraries, then the least painful route is to wrap this
> functionality in Objective-C classes.  Writing Objective-C classes
> that call your C code is easier than writing C ruby extensions.  At
> least in my opinion, this is why MacRuby makes it easier to interface
> with C. Just create a dylib or framework, load it in MacRuby and use
> it.
>
> Note that there is currently no way to bridge arbitrary C functions
> anyway, so if you want something that isn't covered by BridgeSupport,
> your only option right now is to wrap it in Objective-C.

It is possible to cover using BridgeSupport any arbitrary API, though.  
See the gen_bridge_metadata(1) man page, or BridgeSupport(5) if you  
want to write it by hand.

MacRuby trunk uses libffi under the covers to call C / ObjC  
implementations and uses the BridgeSupport annotations to provide the  
correct signatures. We could expose an API (like the Ruby "FFI" gem)  
that can feed the runtime with signatures, that should be very trivial.

> There really isn't a way to get a pointer to a MacRuby function and
> pass that to a C function.  So callback-type situations would have to
> be wrapped in Objective-C.

FYI, in RubyCocoa it is possible to pass Proc objects as function  
pointers. We allocate an FFI closure that calls the proc and pass a  
pointer to the closure to the underlying C API.

This has not been ported to MacRuby, yet :-)

Laurent


More information about the MacRuby-devel mailing list