Skip to content

Instantly share code, notes, and snippets.

@lian
Created November 6, 2013 22:53
Show Gist options
  • Save lian/7345679 to your computer and use it in GitHub Desktop.
Save lian/7345679 to your computer and use it in GitHub Desktop.
ffi-fiddle-wrapper.rb
# 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