Method: Fiddle::Closure#initialize

Defined in:
ext/fiddle/closure.c,
lib/fiddle/ffi_backend.rb

#initialize(*args) ⇒ Object

call-seq: new(ret, args, abi = Fiddle::DEFAULT)

Construct a new Closure object.

  • ret is the C type to be returned

  • args is an Array of arguments, passed to the callback function

  • abi is the abi of the closure

If there is an error in preparing the ffi_cif or ffi_prep_closure, then a RuntimeError will be raised.



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'ext/fiddle/closure.c', line 351

def initialize(ret, args, abi = Function::DEFAULT)
  raise TypeError.new "invalid argument types" unless args.is_a?(Array)

  @ctype, @args = ret, args
  ffi_args = @args.map { |t| Fiddle::FFIBackend.to_ffi_type(t) }
  if ffi_args.size == 1 && ffi_args[0] == FFI::Type::Builtin::VOID
    ffi_args = []
  end
  return_type = Fiddle::FFIBackend.to_ffi_type(@ctype)
  raise "#{self.class} must implement #call" unless respond_to?(:call)
  wrapper = lambda do |*args|
    call(*args.map { |v| v.is_a?(FFI::Pointer) ? Pointer.new(v) : v })
  end
  @function = FFI::Function.new(return_type, ffi_args, wrapper, convention: abi)
  @freed = false
end