Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment