Skip to content

Commit 6ffba40

Browse files
authored
Fix pointer class for closure arguments for FFI backend (#181)
Currently, pointer arguments for closures use the FFI pointer class instead of the Fiddle pointer class with the FFI backend.
1 parent 6d33e91 commit 6ffba40

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/fiddle/ffi_backend.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ def initialize(ret, args, abi = Function::DEFAULT)
188188
end
189189
return_type = Fiddle::FFIBackend.to_ffi_type(@ctype)
190190
raise "#{self.class} must implement #call" unless respond_to?(:call)
191-
callable = method(:call)
192-
@function = FFI::Function.new(return_type, ffi_args, callable, convention: abi)
191+
wrapper = lambda do |*args|
192+
call(*args.map { |v| v.is_a?(FFI::Pointer) ? Pointer.new(v) : v })
193+
end
194+
@function = FFI::Function.new(return_type, ffi_args, wrapper, convention: abi)
193195
@freed = false
194196
end
195197

test/fiddle/test_closure.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ def call(bool)
8787
end
8888
end
8989

90+
def test_pointer
91+
closure_class = Class.new(Closure) do
92+
def call(ptr)
93+
ptr.is_a?(Pointer)
94+
end
95+
end
96+
closure_class.create(:bool, [:voidp]) do |closure|
97+
func = Function.new(closure, [:voidp], :bool)
98+
assert do
99+
func.call(Pointer["hello"])
100+
end
101+
end
102+
end
103+
90104
def test_free
91105
closure_class = Class.new(Closure) do
92106
def call

0 commit comments

Comments
 (0)