Sha256: 67d364fb1439249736b19ffe1cf18eb113e3d413e2059474452cdd962358c611

Contents?: true

Size: 1.98 KB

Versions: 31

Compression:

Stored size: 1.98 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))

describe FFI::Struct, ' with inline callback functions' do
  it 'should be able to define inline callback field' do
    module CallbackMember
      extend FFI::Library
      ffi_lib TestLibrary::PATH
      DUMMY_CB = callback :dummy_cb, [ :int ], :int
      class TestStruct < FFI::Struct
        layout \
          :add, callback([ :int, :int ], :int),
          :sub, callback([ :int, :int ], :int),
          :cb_with_cb_parameter, callback([ DUMMY_CB, :int ], :int)
      end
      attach_function :struct_call_add_cb, [TestStruct, :int, :int], :int
      attach_function :struct_call_sub_cb, [TestStruct, :int, :int], :int
    end
  end
  it 'should take methods as callbacks' do
    module CallbackMember
      extend FFI::Library
      ffi_lib TestLibrary::PATH
      class TestStruct < FFI::Struct
        layout \
          :add, callback([ :int, :int ], :int),
          :sub, callback([ :int, :int ], :int)
      end
      attach_function :struct_call_add_cb, [TestStruct, :int, :int], :int
      attach_function :struct_call_sub_cb, [TestStruct, :int, :int], :int
    end
    module StructCallbacks
      def self.add a, b
        a+b
      end
    end

    ts = CallbackMember::TestStruct.new
    ts[:add] = StructCallbacks.method(:add)

    CallbackMember.struct_call_add_cb(ts, 1, 2).should == 3
  end

  it 'should return callable object from []' do
    module CallbackMember
      extend FFI::Library
      ffi_lib TestLibrary::PATH
      class TestStruct < FFI::Struct
        layout \
          :add, callback([ :int, :int ], :int),
          :sub, callback([ :int, :int ], :int)
      end
      attach_function :struct_call_add_cb, [TestStruct, :int, :int], :int
      attach_function :struct_call_sub_cb, [TestStruct, :int, :int], :int
    end

    s = CallbackMember::TestStruct.new
    add = Proc.new { |a,b| a+b}
    s[:add] = add
    fn = s[:add]
    fn.respond_to?(:call).should be_true
    fn.call(1, 2).should == 3
  end
end

Version data entries

31 entries across 31 versions & 3 rubygems

Version Path
ffi-0.6.4 spec/ffi/struct_callback_spec.rb
resque-pool-0.3.0 vendor/bundle/ruby/1.8/gems/ffi-0.6.3/spec/ffi/struct_callback_spec.rb
resque-pool-0.3.0.beta.2 vendor/bundle/ruby/1.8/gems/ffi-0.6.3/spec/ffi/struct_callback_spec.rb
ffi-1.0.4-x86-mingw32 spec/ffi/struct_callback_spec.rb
ffi-1.0.4 spec/ffi/struct_callback_spec.rb
ffi-1.0.3-x86-mingw32 spec/ffi/struct_callback_spec.rb
ffi-1.0.3 spec/ffi/struct_callback_spec.rb
ffi-1.0.2-x86-mingw32 spec/ffi/struct_callback_spec.rb
ffi-1.0.2 spec/ffi/struct_callback_spec.rb
ffi-1.0.1-x86-mingw32 spec/ffi/struct_callback_spec.rb
ffi-1.0.1 spec/ffi/struct_callback_spec.rb
ffi-1.0.0 spec/ffi/struct_callback_spec.rb
ffi-aitrus-0.9.0.beta spec/ffi/struct_callback_spec.rb
ffi-0.6.3-x86-mingw32 spec/ffi/struct_callback_spec.rb
ffi-0.6.3-x86-mswin32 spec/ffi/struct_callback_spec.rb
ffi-0.6.3 spec/ffi/struct_callback_spec.rb
ffi-0.6.2 spec/ffi/struct_callback_spec.rb
ffi-0.6.1 spec/ffi/struct_callback_spec.rb
ffi-0.6.0 spec/ffi/struct_callback_spec.rb
ffi-0.5.4-x86-mswin32 spec/ffi/struct_callback_spec.rb