Class: FFI::Function

Inherits:
Pointer
  • Object
show all
Defined in:
ext/ffi_c/Function.c

Constant Summary

Constants inherited from Pointer

Pointer::NULL, Pointer::SIZE

Instance Method Summary collapse

Methods inherited from Pointer

#+, #==, #address, #inspect, #null?, #read_array_of_int, #read_array_of_long, #read_array_of_pointer, #read_array_of_type, #read_float, #read_int, #read_long, #read_long_long, #read_pointer, #read_string, #read_string_length, #read_string_to_null, size, #write_array_of_int, #write_array_of_long, #write_array_of_pointer, #write_array_of_type, #write_float, #write_int, #write_long, #write_long_long, #write_pointer, #write_string, #write_string_length

Constructor Details

#initialize(*args) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/ffi_c/Function.c', line 120

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(module, name) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'ext/ffi_c/Function.c', line 240

static VALUE
function_attach(VALUE self, VALUE module, VALUE name)
{
    Function* fn;
    char var[1024];

    Data_Get_Struct(self, Function, fn);

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

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

    //
    // Stash the Function in a module variable so it does not get garbage collected
    //
    snprintf(var, sizeof(var), "@@%s", StringValueCStr(name));
    rb_cv_set(module, var, self);

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

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

    return self;
}

#autoreleaseObject



284
285
286
287
288
289
290
291
292
# File 'ext/ffi_c/Function.c', line 284

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

    Data_Get_Struct(self, Function, fn);

    return fn->autorelease ? Qtrue : Qfalse;
}

#autorelease=(autorelease) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
# File 'ext/ffi_c/Function.c', line 272

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

    Data_Get_Struct(self, Function, fn);

    fn->autorelease = RTEST(autorelease);

    return self;
}

#autorelease?Boolean

Returns:

  • (Boolean)


284
285
286
287
288
289
290
291
292
# File 'ext/ffi_c/Function.c', line 284

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

    Data_Get_Struct(self, Function, fn);

    return fn->autorelease ? Qtrue : Qfalse;
}

#call(*args) ⇒ Object



230
231
232
233
234
235
236
237
238
# File 'ext/ffi_c/Function.c', line 230

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

    Data_Get_Struct(self, Function, fn);

    return rbffi_CallFunction(argc, argv, fn->memory.address, fn->info);
}

#freeObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'ext/ffi_c/Function.c', line 294

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

    Data_Get_Struct(self, Function, fn);

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