Subject:
[ruby-ffi] Inline Callbacks
From:
Charles Strahan
Date:
10/11/11 10:37 PM
To:
ruby-ffi@googlegroups.com

Hello,

Could someone elaborate on how inline callbacks work? I took a look at callback_spec.rb, and found the following (I've removed unrelated code):

  module LibTest
    extend FFI::Library
    ffi_lib TestLibrary::PATH

    callback :cbS8rV, [ :char ], :void
    attach_function :testCallbackCrV, :testClosureBrV, [ :cbS8rV, :char ], :void
   end

  it ":char (0) argument" do
    v = 0xdeadbeef
    LibTest.testCallbackCrV(0) { |i| v = i }
    v.should == 0
  end

So, apparently inline callbacks (via blocks) work... at least when the first type in the argument list (:cbS8rV, in this case) is a callback, and any other args can be specified as usual ((0) ,in this case). Is that the only case where an inline callback will work (first arg is callback) - or are there others?

Could someone please layout the rules in detail? For instance, if I were to use two callbacks like so:

  # [ char1, callback1, char2, callback2]
   attach_function :testSomeOtherClosure, :char, :cbS8rV:char, :cbS8rV ], :void

... would the following work "as expected"?

    char1 = 1
    char2 = 2
    callback2 = method(:someCallback)
    LibTest.testSomeOtherClosure(char1, char2, callback2) do
# The first defined callback . . .
2
end


Any clarification would be appreciated. I'm writing IronRuby's FFI gem, and I'm not sure where to look in the C code (ffi_c) to determine how this works (it doesn't help that C isn't my forte :) ).

-Charles