Sha256: 385a8151358551045bfd1f8cbcf9b1de2529b01ccffa0b6e5e1db66e13480956
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
require 'ffi-gobject/closure' module GObject # This class encapsulates Ruby # blocks as GObject Closures. class RubyClosure < Closure BLOCK_STORE = {} # Extend the standard GClosure layout with a field block_id to store # the object_id of the associated block. class Struct < FFI::Struct layout :parent, Closure::Struct, 0, :block_id, :int64 end def block BLOCK_STORE[@struct[:block_id]] end def block= block id = block.object_id BLOCK_STORE[id] = block @struct[:block_id] = id end def invoke_block *args block.call(*args) end def self.new &block raise ArgumentError unless block_given? closure = wrap(new_simple(self::Struct.size, nil).to_ptr) closure.block = block closure.set_marshal proc { |*args| marshaller(*args) } closure end def self.marshaller closure, return_value, param_values, _invocation_hint, _marshal_data # TODO: Improve by registering RubyClosure as a GObject type rclosure = wrap(closure.to_ptr) param_values ||= [] args = param_values.map { |value| value.get_value } result = rclosure.invoke_block(*args) return_value.set_ruby_value(result) if return_value end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gir_ffi-0.7.6 | lib/ffi-gobject/ruby_closure.rb |