lib/fiddle/ffi_backend.rb in fiddle-1.1.5 vs lib/fiddle/ffi_backend.rb in fiddle-1.1.6

- old
+ new

@@ -157,19 +157,20 @@ args[i + 1] = String.try_convert(args[i + 1]) || args[i + 1] end args[i] = Fiddle::FFIBackend.to_ffi_type(args[i]) end else - args.map! do |arg| - if arg.respond_to?(:to_ptr) - begin - arg = arg.to_ptr - end until arg.is_a?(FFI::Pointer) || !arg.respond_to?(:to_ptr) - arg - else - arg - end + @args.each_with_index do |arg_type, i| + next unless arg_type == Types::VOIDP + + src = args[i] + next if src.nil? + next if src.is_a?(String) + next if src.is_a?(FFI::AbstractMemory) + next if src.is_a?(FFI::Struct) + + args[i] = Pointer[src] end end result = @function.call(*args, &block) result = Pointer.new(result) if result.is_a?(FFI::Pointer) result @@ -259,10 +260,18 @@ else Pointer.new(value) end end + def self.write(addr, bytes) + FFI::Pointer.new(addr).write_bytes(bytes) + end + + def self.read(addr, len) + FFI::Pointer.new(addr).read_bytes(len) + end + class << self alias [] to_ptr end def []=(*args, value) @@ -306,9 +315,11 @@ else raise DLError.new("to_ptr should return a Fiddle::Pointer object, was #{fiddle_ptr.class}") end elsif addr.is_a?(IO) raise NotImplementedError, "IO ptr isn't supported" + else + FFI::Pointer.new(Integer(addr)) end @size = size ? size : ptr.size @free = free @ffi_ptr = ptr