Description
An FFI closure wrapper, for handling callbacks.
Example
closure = Class.new(Fiddle::Closure) {
def call
10
end
}.new(Fiddle::TYPE_INT, [])
#=> #<#<Class:0x0000000150d308>:0x0000000150d240>
func = Fiddle::Function.new(closure, [], Fiddle::TYPE_INT)
#=> #<Fiddle::Function:0x00000001516e58>
func.call
#=> 10
Attributes
| [R] | args | arguments of the FFI closure |
| [R] | ctype | the C type of the return of the FFI closure |
Class Public methods
create(*args) Link
Create a new closure. If a block is given, the created closure is automatically freed after the given block is executed.
The all given arguments are passed to Fiddle::Closure.new. So using this method without block equals to Fiddle::Closure.new.
Example
Fiddle::Closure.create(TYPE_INT, [TYPE_INT]) do |closure|
# closure is freed automatically when this block is finished.
end
new(ret, args, abi = Fiddle::DEFAULT) Link
Construct a new Closure object.
-
retis the C type to be returned -
argsis anArrayof arguments, passed to the callback function -
abiis the abi of the closure
If there is an error in preparing the ffi_cif or ffi_prep_closure, then a RuntimeError will be raised.
Source: show
static VALUE
initialize(int argc, VALUE *argv, VALUE self)
{
initialize_data data;
data.self = self;
data.argc = argc;
data.argv = argv;
return rb_rescue(initialize_body, (VALUE)&data,
initialize_rescue, (VALUE)&data);
}
Instance Public methods
free() Link
Free this closure explicitly. You can’t use this closure anymore.
If this closure is already freed, this does nothing.
Source: show
static VALUE
closure_free(VALUE self)
{
fiddle_closure *closure;
TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, closure);
if (closure) {
dealloc(closure);
RTYPEDDATA_DATA(self) = NULL;
}
return RUBY_Qnil;
}