Created
November 6, 2013 22:53
-
-
Save lian/7345679 to your computer and use it in GitHub Desktop.
ffi-fiddle-wrapper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.slideshare.net/tenderlove/hidden-gems-of-ruby-19 on slide 134 | |
module FFI | |
module Library | |
TYPE_MAP = { | |
string: DL::TYPE_VOIDP, | |
pointer: DL::TYPE_VOIDP, | |
} | |
DL.constants.each{|const| | |
next unless const.to_s.match(/^TYPE_/) | |
name = const.to_s.spit("_",2).last.downcase.to_sym | |
TYPE_MAP[name] = DL.const_get(const) | |
} | |
def ffi_lib(lib) | |
@lib = DL::Handle.new(lib) | |
end | |
def attach_function(name, args, ret) | |
func = Fiddle::Function.new( @lib[name.to_s], args.map{|x| TYPE_MAP[x] }, TYPE_MAP[ret] ) | |
define_singleton_method(name){|*args| fund.call(*args) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment