Search code examples
rubyruby-ffi

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


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.


Solution

  • 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.