Class: FFI::Function
- Inherits:
-
Pointer
- Object
- AbstractMemory
- Pointer
- FFI::Function
- Defined in:
- ext/ffi_c/Function.c
Constant Summary
Constants inherited from Pointer
Instance Method Summary collapse
-
#attach(m, name) ⇒ self
Attach a Function to the Module
m
asname
. -
#autorelease ⇒ Boolean
Get
autorelease
attribute. -
#autorelease=(autorelease) ⇒ self
Set
autorelease
attribute (See Pointer). -
#autorelease? ⇒ Boolean
Get
autorelease
attribute. -
#call(*args) ⇒ FFI::Type
Call the function.
-
#free ⇒ self
Free memory allocated by Function.
-
#initialize(*args) ⇒ self
constructor
A new Function instance.
-
#initialize_copy(other) ⇒ nil
DO NOT CALL THIS METHOD.
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, #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, #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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 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 244 |
# File 'ext/ffi_c/Function.c', line 205
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
.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'ext/ffi_c/Function.c', line 377
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");
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);
}
/*
* 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_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;
}
|
#autorelease ⇒ Boolean
Get autorelease
attribute. Synonymous for #autorelease?.
433 434 435 436 437 438 439 440 441 |
# File 'ext/ffi_c/Function.c', line 433
static VALUE
function_autorelease_p(VALUE self)
{
Function* fn;
Data_Get_Struct(self, Function, fn);
return fn->autorelease ? Qtrue : Qfalse;
}
|
#autorelease=(autorelease) ⇒ self
Set autorelease
attribute (See Pointer).
421 422 423 424 425 426 427 428 429 430 431 |
# File 'ext/ffi_c/Function.c', line 421
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
Get autorelease
attribute.
433 434 435 436 437 438 439 440 441 |
# File 'ext/ffi_c/Function.c', line 433
static VALUE
function_autorelease_p(VALUE self)
{
Function* fn;
Data_Get_Struct(self, Function, fn);
return fn->autorelease ? Qtrue : Qfalse;
}
|
#call(*args) ⇒ FFI::Type
Call the function
360 361 362 363 364 365 366 367 368 |
# File 'ext/ffi_c/Function.c', line 360
static VALUE
function_call(int argc, VALUE* argv, VALUE self)
{
Function* fn;
Data_Get_Struct(self, Function, fn);
return (*fn->info->invoke)(argc, argv, fn->base.memory.address, fn->info);
}
|
#free ⇒ self
Free memory allocated by Function.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'ext/ffi_c/Function.c', line 448
static VALUE
function_release(VALUE self)
{
Function* fn;
Data_Get_Struct(self, Function, 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
251 252 253 254 255 256 |
# File 'ext/ffi_c/Function.c', line 251
static VALUE
function_initialize_copy(VALUE self, VALUE other)
{
rb_raise(rb_eRuntimeError, "cannot duplicate function instances");
return Qnil;
}
|