Subject: [ruby-ffi] Re: cannot define function pointers that take function pointer arguments in jruby struct layout |
From: Wayne Meissner |
Date: 9/17/09 10:13 PM |
To: ruby-ffi@googlegroups.com |
2009/9/14 Aman Gupta <themastermind1@gmail.com>:
I'm using ffi-swig-generator on: typedef void (*MyCallback) (void *data); typedef struct { void (*member) (MyCallback cb); } MyStruct; which generates: callback(:MyCallback, [ :pointer ], :void) class MyStruct < FFI::Struct layout( :member, callback([ :MyCallback ], :void) ) end
The root problem here is the resolution of :MyCallback - it will fail
on both JRuby and 1.9 (or at least it does for me).
The best way to ensure this works is to define a constant in enclosing
module, and use that on the layout line.
e.g.
module Foo
extend FFI::Library
MyCallback = callback(:MyCallback, [ :pointer ], :void
class MyStruct < FFI::Struct
layout(:member, callback(MyCallback, :void))
end
end
Alternatively you can look it up using Foo.find_type(:MyCallback)
module Foo
extend FFI::Library
callback(:MyCallback, [ :pointer ], :void
class MyStruct < FFI::Struct
layout(:member, callback(Foo.find_type(:MyCallback), :void))
end
end
ffi-swig-generator should be altered to do one of the above.
Struct#find_type tries to emulate this behaviour, and fails - ruby
does not appear to have anyway of getting the lexical enclosing module
of a subclass, when accessed from a method of the superclass. If
anyone knows a way, I'd be interested.