Can I pass a ruby object pointer to a ruby-ffi callback?

767 views Asked by At

I could really use a push in the right direction on this.

Given this C code:

typedef void cbfunc(void *data);
void set_callback(cbfunc* cb);
//do_stuff calls the callback multiple times with data as argument
void do_stuff(void *data);

This Ruby code:

module Lib
    extend FFI::Library
    # ...
    callback :cbfunc, [:pointer], :void
    attach_function :set_callback, [:cbfunc], :void
    attach_function :do_stuff, [:pointer], :void
end

Is there any way that I can pass a ruby array as the callback data, something like:

proc = Proc.new do |data|
    # somehow cast FFI::Pointer to be a ruby array here?
    data.push(5)
end
Lib::set_callback(proc)
Lib::do_stuff(ptr_to_a_ruby_obj_here)

The problem is that the callback will be called multiple times and I need a way of easily constructing an array of various ruby objects.

Maybe I'm just a bit tired but it feels like there's a simple way to get this done and I'm just not seeing it.

2

There are 2 answers

0
Daniel Wyatt On BEST ANSWER

I realized after posting this that I can curry the Proc and use that as the callback.

So something like:

proc = Proc.new do |results, data|
    results.push(5)
end
results = []
callback = proc[results]
Lib::set_callback(callback)
Lib::do_stuff(nil) # Not concerned with this pointer anymore

I just switched to ignoring the void* data parameter (which is a requirement on the C side) here. There must be a few other ways and I'm interested in hearing about them if anyone wants to share.

0
Just Jake On

You can create a FFI::Pointer from a Ruby object using that object's object_id:

# @param obj [any] Any value
# @return [::FFI::Pointer] a pointer to the given value
def ruby_to_pointer(obj)
  require 'ffi'
  address = obj.object_id << 1
  ffi_pointer = ::FFI::Pointer.new(:pointer, address)
end

The problem is getting a usable Ruby value out of this kind of pointer, when it comes back to Ruby in a callback. FFI doesn't provide a natural interface for this, but the Ruby standardlib module fiddle does, with it's Pointer type:

# @param ffi_pointer [FFI::Pointer, #to_i]
# @return [any] Ruby object
def pointer_to_ruby(ffi_pointer)
  require 'fiddle'
  address = ffi_pointer.to_i
  fiddle = ::Fiddle::Pointer.new(address)
  obj = fiddle.to_value
end

This is an unsafe operation! You should take extreme care that your original Ruby object isn't garbage collected before you convert its FFI::Pointer representation back to Ruby.