Guest User

Untitled

a guest
Jun 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. module FFI
  2. module Library
  3. TYPE_MAP = {
  4. :string => DL::TYPE_VOIDP,
  5. :pointer => DL::TYPE_VOIDP,
  6. }
  7.  
  8. DL.constants.each do |const|
  9. next unless const.to_s =~ /^TYPE_/
  10.  
  11. name = const.to_s.split('_', 2).last.downcase.to_sym
  12. TYPE_MAP[name] = DL.const_get(const)
  13. end
  14.  
  15. def ffi_lib(lib)
  16. @lib = DL::Handle.new lib
  17. end
  18.  
  19. def attach_function(name, args, ret)
  20. func = Fiddle::Function.new(
  21. @lib[name.to_s], args.map { |x| TYPE_MAP[x] }, TYPE_MAP[ret]
  22. )
  23.  
  24. define_singleton_method(name) { |*args| func.call(*args) }
  25. end
  26. end
  27. end
Add Comment
Please, Sign In to add comment