From 4bada8b864e445b6eebe1a341e30cad94dbcaf84 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Thu, 6 May 2010 06:59:24 +0000 Subject: * ext/fiddle/*: Adding fiddle library to wrap libffi * test/fiddle/*: testing fiddle extension * ext/dl/lib/dl.rb: Requiring fiddle if it is available * ext/dl/lib/dl/callback.rb: using Fiddle if it is available * ext/dl/lib/dl/func.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/dl/lib/dl/func.rb | 114 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 39 deletions(-) (limited to 'ext/dl/lib/dl/func.rb') diff --git a/ext/dl/lib/dl/func.rb b/ext/dl/lib/dl/func.rb index 7a8b62e325..4d0db4ee7a 100644 --- a/ext/dl/lib/dl/func.rb +++ b/ext/dl/lib/dl/func.rb @@ -5,21 +5,37 @@ require 'dl/value' require 'thread' module DL - class Function + parent = DL.fiddle? ? Fiddle::Function : Object + + class Function < parent include DL include ValueUtil - def initialize(cfunc, argtypes, &proc) - @cfunc = cfunc - @stack = Stack.new(argtypes.collect{|ty| ty.abs}) - if( @cfunc.ctype < 0 ) - @cfunc.ctype = @cfunc.ctype.abs - @unsigned = true + def initialize cfunc, argtypes, abi = nil, &block + if DL.fiddle? + abi ||= Fiddle::Function::DEFAULT + if block_given? + @cfunc = Class.new(Fiddle::Closure) { + define_method(:call, block) + }.new(cfunc.ctype, argtypes) + else + @cfunc = cfunc + end + + @args = argtypes + super(@cfunc, @args.reject { |x| x == TYPE_VOID }, cfunc.ctype, abi) else - @unsigned = false - end - if( proc ) - bind(&proc) + @cfunc = cfunc + @stack = Stack.new(argtypes.collect{|ty| ty.abs}) + if( @cfunc.ctype < 0 ) + @cfunc.ctype = @cfunc.ctype.abs + @unsigned = true + else + @unsigned = false + end + if block_given? + bind(&block) + end end end @@ -32,11 +48,18 @@ module DL end def call(*args, &block) - funcs = [] - args = wrap_args(args, @stack.types, funcs, &block) - r = @cfunc.call(@stack.pack(args)) - funcs.each{|f| f.unbind_at_call()} - return wrap_result(r) + if DL.fiddle? + if block_given? + args.find { |a| DL::Function === a }.bind_at_call(&block) + end + super + else + funcs = [] + args = wrap_args(args, @stack.types, funcs, &block) + r = @cfunc.call(@stack.pack(args)) + funcs.each{|f| f.unbind_at_call()} + return wrap_result(r) + end end def wrap_result(r) @@ -52,31 +75,44 @@ module DL end def bind(&block) - if( !block ) - raise(RuntimeError, "block must be given.") - end - if( @cfunc.ptr == 0 ) - cb = Proc.new{|*args| - ary = @stack.unpack(args) - @stack.types.each_with_index{|ty, idx| - case ty - when TYPE_VOIDP - ary[idx] = CPtr.new(ary[idx]) - end - } - r = block.call(*ary) - wrap_arg(r, @cfunc.ctype, []) - } - case @cfunc.calltype - when :cdecl - @cfunc.ptr = set_cdecl_callback(@cfunc.ctype, @stack.size, &cb) - when :stdcall - @cfunc.ptr = set_stdcall_callback(@cfunc.ctype, @stack.size, &cb) - else - raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}") + if DL.fiddle? + @cfunc = Class.new(Fiddle::Closure) { + def initialize ctype, args, block + super(ctype, args) + @block = block + end + + def call *args + @block.call(*args) + end + }.new(@cfunc.ctype, @args, block) + else + if( !block ) + raise(RuntimeError, "block must be given.") end if( @cfunc.ptr == 0 ) - raise(RuntimeException, "can't bind C function.") + cb = Proc.new{|*args| + ary = @stack.unpack(args) + @stack.types.each_with_index{|ty, idx| + case ty + when TYPE_VOIDP + ary[idx] = CPtr.new(ary[idx]) + end + } + r = block.call(*ary) + wrap_arg(r, @cfunc.ctype, []) + } + case @cfunc.calltype + when :cdecl + @cfunc.ptr = set_cdecl_callback(@cfunc.ctype, @stack.size, &cb) + when :stdcall + @cfunc.ptr = set_stdcall_callback(@cfunc.ctype, @stack.size, &cb) + else + raise(RuntimeError, "unsupported calltype: #{@cfunc.calltype}") + end + if( @cfunc.ptr == 0 ) + raise(RuntimeException, "can't bind C function.") + end end end end -- cgit v1.2.3