Method: FFI::Library#callback

Defined in:
lib/ffi/library.rb

#callback(name, params, ret) ⇒ FFI::CallbackInfo #callback(params, ret) ⇒ FFI::CallbackInfo

Overloads:

Returns:

Raises:

  • (ArgumentError)


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/ffi/library.rb', line 330

def callback(*args)
  raise ArgumentError, "wrong number of arguments" if args.length < 2 || args.length > 3
  name, params, ret = if args.length == 3
    args
  else
    [ nil, args[0], args[1] ]
  end

  native_params = params.map { |e| find_type(e) }
  raise ArgumentError, "callbacks cannot have variadic parameters" if native_params.include?(FFI::Type::VARARGS)
  options = Hash.new
  options[:convention] = ffi_convention
  options[:enums] = @ffi_enums if defined?(@ffi_enums)
  ret_type = find_type(ret)
  if ret_type == Type::STRING
    raise TypeError, ":string is not allowed as return type of callbacks"
  end
  cb = FFI::CallbackInfo.new(ret_type, native_params, options)

  # Add to the symbol -> type map (unless there was no name)
  unless name.nil?
    typedef cb, name
  end

  cb
end