Class: FFI::Function

Inherits:
Pointer show all
Includes:
RegisterAttach
Defined in:
lib/ffi/function.rb,
ext/ffi_c/Function.c

Defined Under Namespace

Modules: RegisterAttach

Constant Summary

Constants inherited from Pointer

Pointer::NULL, Pointer::SIZE

Constants inherited from AbstractMemory

AbstractMemory::LONG_MAX

Instance Method Summary collapse

Methods inherited from Pointer

#+, #==, #address, #inspect, #null?, #order, #read, #read_array_of_type, #read_string, #read_string_length, #read_string_to_null, size, #slice, #to_ptr, #to_s, #type_size, #write, #write_array_of_type, #write_string, #write_string_length

Methods inherited from AbstractMemory

#[], #__copy_from__, #clear, #freeze, #get, #get_array_of_float32, #get_array_of_float64, #get_array_of_pointer, #get_array_of_string, #get_bytes, #get_float32, #get_float64, #get_pointer, #get_string, #put, #put_array_of_float32, #put_array_of_float64, #put_array_of_pointer, #put_bytes, #put_float32, #put_float64, #put_pointer, #put_string, #read_array_of_double, #read_array_of_float, #read_array_of_pointer, #read_bytes, #read_double, #read_float, #read_pointer, #size_limit?, #total, #type_size, #write_array_of_double, #write_array_of_float, #write_array_of_pointer, #write_bytes, #write_double, #write_float, #write_pointer

Constructor Details

#initialize(return_type, param_types, options = {}) {|i| ... } ⇒ self #initialize(return_type, param_types, proc, options = {}) ⇒ self

A new Function instance.

Define a function from a Proc or a block.

Overloads:

  • #initialize(return_type, param_types, options = {}) {|i| ... } ⇒ self

    Yield Parameters:

    • i

      parameters for the function

  • #initialize(return_type, param_types, proc, options = {}) ⇒ self

    Parameters:

    • proc (Proc)

Parameters:

  • return_type (Type, Symbol)

    return type for the function

  • param_types (Array<Type, Symbol>)

    array of parameters types

  • options (Hash)

    see FFI::FunctionType for available options



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/ffi_c/Function.c', line 298

static VALUE
function_initialize(int argc, VALUE* argv, VALUE self)
{

    VALUE rbReturnType = Qnil, rbParamTypes = Qnil, rbProc = Qnil, rbOptions = Qnil;
    VALUE rbFunctionInfo = Qnil;
    VALUE infoArgv[3];
    int nargs;

    nargs = rb_scan_args(argc, argv, "22", &rbReturnType, &rbParamTypes, &rbProc, &rbOptions);

    /*
     * Callback with block,
     * e.g. Function.new(:int, [ :int ]) { |i| blah }
     * or   Function.new(:int, [ :int ], { :convention => :stdcall }) { |i| blah }
     */
    if (rb_block_given_p()) {
        if (nargs > 3) {
            rb_raise(rb_eArgError, "cannot create function with both proc/address and block");
        }
        rbOptions = rbProc;
        rbProc = rb_block_proc();
    } else {
        /* Callback with proc, or Function with address
         * e.g. Function.new(:int, [ :int ], Proc.new { |i| })
         *      Function.new(:int, [ :int ], Proc.new { |i| }, { :convention => :stdcall })
         *      Function.new(:int, [ :int ], addr)
         *      Function.new(:int, [ :int ], addr, { :convention => :stdcall })
         */
    }

    infoArgv[0] = rbReturnType;
    infoArgv[1] = rbParamTypes;
    infoArgv[2] = rbOptions;
    rbFunctionInfo = rb_class_new_instance(rbOptions != Qnil ? 3 : 2, infoArgv, rbffi_FunctionTypeClass);

    function_init(self, rbFunctionInfo, rbProc);

    return self;
}

Instance Method Details

#attach(m, name) ⇒ self

Attach a Function to the Module m as name.

Parameters:

  • m (Module)
  • name (String)

Returns:

  • (self)


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'ext/ffi_c/Function.c', line 495

static VALUE
function_attach(VALUE self, VALUE module, VALUE name)
{
    Function* fn;

    StringValue(name);
    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    if (fn->info->parameterCount == -1) {
        rb_raise(rb_eRuntimeError, "cannot attach variadic functions");
        return Qnil;
    }

    if (!rb_obj_is_kind_of(module, rb_cModule)) {
        rb_raise(rb_eRuntimeError, "trying to attach function to non-module");
        return Qnil;
    }

    if (fn->methodHandle == NULL) {
        fn->methodHandle = rbffi_MethodHandle_Alloc(fn->info, fn->base.memory.address);
    }

    rb_define_singleton_method(module, StringValueCStr(name),
            rbffi_MethodHandle_CodeAddress(fn->methodHandle), -1);


    rb_define_method(module, StringValueCStr(name),
            rbffi_MethodHandle_CodeAddress(fn->methodHandle), -1);

    return self;
}

#autoreleaseBoolean

Get autorelease attribute. Synonymous for #autorelease?.

Returns:

  • (Boolean)


546
547
548
549
550
551
552
553
554
# File 'ext/ffi_c/Function.c', line 546

static VALUE
function_autorelease_p(VALUE self)
{
    Function* fn;

    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    return fn->autorelease ? Qtrue : Qfalse;
}

#autorelease=(autorelease) ⇒ self

Set autorelease attribute (See Pointer).

Parameters:

  • autorelease (Boolean)

Returns:

  • (self)


533
534
535
536
537
538
539
540
541
542
543
544
# File 'ext/ffi_c/Function.c', line 533

static VALUE
function_set_autorelease(VALUE self, VALUE autorelease)
{
    Function* fn;

    rb_check_frozen(self);
    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    fn->autorelease = RTEST(autorelease);

    return self;
}

#autorelease?Boolean

Get autorelease attribute.

Returns:

  • (Boolean)

    autorelease attribute



546
547
548
549
550
551
552
553
554
# File 'ext/ffi_c/Function.c', line 546

static VALUE
function_autorelease_p(VALUE self)
{
    Function* fn;

    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    return fn->autorelease ? Qtrue : Qfalse;
}

#call(*args) ⇒ FFI::Type

Call the function

Parameters:

  • args (Array)

    function arguments

Returns:



478
479
480
481
482
483
484
485
486
# File 'ext/ffi_c/Function.c', line 478

static VALUE
function_call(int argc, VALUE* argv, VALUE self)
{
    Function* fn;

    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    return (*fn->info->invoke)(argc, argv, fn->base.memory.address, fn->info);
}

#freeself

Free memory allocated by Function.

Returns:

  • (self)


571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'ext/ffi_c/Function.c', line 571

static VALUE
function_release(VALUE self)
{
    Function* fn;

    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    if (fn->closure == NULL) {
        rb_raise(rb_eRuntimeError, "cannot free function which was not allocated");
    }

    rbffi_Closure_Free(fn->closure);
    fn->closure = NULL;

    return self;
}

#initialize_copy(other) ⇒ nil

DO NOT CALL THIS METHOD

Returns:

  • (nil)


344
345
346
347
348
349
# File 'ext/ffi_c/Function.c', line 344

static VALUE
function_initialize_copy(VALUE self, VALUE other)
{
    rb_raise(rb_eRuntimeError, "cannot duplicate function instances");
    return Qnil;
}

#param_typesArray<FFI::Type>

Retrieve Array of parameter types

This method returns an Array of FFI types accepted as function parameters.

Returns:



49
50
51
# File 'lib/ffi/function.rb', line 49

def param_types
  type.param_types
end

#return_typeFFI::Type

Retrieve the return type of the function

This method returns FFI type returned by the function.

Returns:



40
41
42
# File 'lib/ffi/function.rb', line 40

def return_type
  type.return_type
end

#typeObject (private)



556
557
558
559
560
561
562
563
564
# File 'ext/ffi_c/Function.c', line 556

static VALUE
function_type(VALUE self)
{
    Function* fn;

    TypedData_Get_Struct(self, Function, &function_data_type, fn);

    return fn->rbFunctionInfo;
}