Subject:
Re: [ruby-ffi] DataConverter for callback type
From:
Matijs van Zuijlen
Date:
10/5/13 1:18 AM
To:
ruby-ffi@googlegroups.com

Hi Wayne,

Thanks for your response. Based on your example I got things working. I was
indeed trying to use Type::Function as the native type, and experiencing crashes
because of it (although that actually worked on JRuby!).

Regards,
Matijs

On 01/10/13 06:20, Wayne Meissner wrote:
> Hmm.  There are a couple of ways of doing it:
> - making a DataConverter that has a native_type of a Type::Function actually
> *work* instead of crashing the VM (or failing with an error on JRuby)
> - allowing FFI::Function.new() to take a call context and a function
> pointer/proc/block instead of a separate rtype and param_type array for efficiency.
> 
> In the meantime, you can probably achieve what you want.
> e.g.
> 
> module QSort
>   extend FFI::Library
>   ffi_lib 'c'
> 
>   module Comparator
>     RTYPE = FFI::Type::SINT
>     PARAM_TYPES =  [ FFI::Type::POINTER, FFI::Type::POINTER ].freeze
>     extend FFI::DataConverter
> 
>     def self.native_type
>       FFI::Type::POINTER
>     end
> 
>     def self.to_native(value, ctx)
>       FFI::Function.new(RTYPE, PARAM_TYPES, value)
>     end
>   end
>   attach_function :qsort, [ :pointer, :size_t, :size_t, Comparator ], :void
> end
> 
> ptr = FFI::MemoryPointer.new(:int, 2)
> ptr[0].write_int(2)
> ptr[1].write_int(1)
> puts "unsorted=#{ptr.get_array_of_int(0, 2)}"
> QSort.qsort(ptr, 2, 4, ->(p1, p2) { i1, i2 = p1.read_int, p2.read_int;  i1 < i2
> ? -1 : i1 > i2 ? 1 : 0 })
> puts "sorted=#{ptr.get_array_of_int(0, 2)}"
> 
> 
> On Monday, 23 September 2013 05:37:23 UTC+10, Matijs van Zuijlen wrote:
> 
>     I've been digging a little further, and it seems that the
>     rbffi_Function_ForProc
>     function defined in Function.c does exactly what I want, since I have a
>     FFI::FunctionType (i.e., self::Callback), and a Proc (the value argument of
>     #to_native), and I would like to turn this into a FFI::Function.
> 
>     Would it be possible to expose this function to Ruby?
> 
>     On 22/09/13 13:05, Matijs van Zuijlen wrote:
>     > Hello,
>     >
>     > I'm trying to create a DataConverter that represents a callback function. The
>     > code basically boils down to this:
>     >
>     > module Lib
>     >   include FFI::Library
>     >   ffi_lib 'some-library'
>     > end
>     >
>     > class Foo < Proc
>     >   extend FFI::DataConverter
>     >
>     >   Callback = Lib.callback ... # usual call to FFI::Library.callback
>     >
>     >   def self.native_type
>     >     self::Callback
>     >   end
>     > end
>     >
>     > [The reason I'm doing this is that I want to wrap procs passed from ruby
>     in some
>     > argument conversion code using a method defined on, in this case, Foo]
>     >
>     > Now, I'm unable to figure out what the definition of Foo.to_native should be.
>     > Foo::Callback is of type FFI::FunctionType, which doesn't have a to_native
>     method.
>     >
>     > Any ideas?
>     >
>     > Thanks and regards,
>     >
> 
> 
>     -- 
>     Matijs
> 
> -- 
>  
> ---
> You received this message because you are subscribed to the Google Groups
> "ruby-ffi" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to ruby-ffi+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.


-- Matijs