Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created November 19, 2010 23:16
Show Gist options
  • Save tenderlove/707385 to your computer and use it in GitHub Desktop.
Save tenderlove/707385 to your computer and use it in GitHub Desktop.
module FFI
module Library
TYPE_MAP = {
:string => DL::TYPE_VOIDP,
:pointer => DL::TYPE_VOIDP,
}
DL.constants.each do |const|
next unless const.to_s =~ /^TYPE_/
name = const.to_s.split('_', 2).last.downcase.to_sym
TYPE_MAP[name] = DL.const_get(const)
end
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| func.call(*args) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment