Method: Fiddle::Function#call

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

#call(argv[], self) ⇒ Object

Calls the constructed Function, with args. Caller must ensure the underlying function is called in a thread-safe manner if running in a multi-threaded process.

Note that it is not thread-safe to use this method to directly or indirectly call many Ruby C-extension APIs unless you don’t pass need_gvl: true to Fiddle::Function#new.

For an example see Fiddle::Function



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'ext/fiddle/function.c', line 219

def call(*args, &block)
  if @function.is_a?(FFI::VariadicInvoker)
    n_fixed_args = @args.size - 1
    n_fixed_args.step(args.size - 1, 2) do |i|
      if args[i] == :const_string || args[i] == Types::CONST_STRING
        args[i + 1] = String.try_convert(args[i + 1]) || args[i + 1]
      end
      args[i] = Fiddle::FFIBackend.to_ffi_type(args[i])
    end
  else
    args.map! do |arg|
      if arg.respond_to?(:to_ptr)
        begin
          arg = arg.to_ptr
        end until arg.is_a?(FFI::Pointer) || !arg.respond_to?(:to_ptr)
        arg
      else
        arg
      end
    end
  end
  result = @function.call(*args, &block)
  result = Pointer.new(result) if result.is_a?(FFI::Pointer)
  result
end