Skip to content

Commit 41c21f5

Browse files
committed
Fix Fiddle::Closure on TruffleRuby
* TruffleRuby requires a Proc or Method as the callable.
1 parent fc3be9c commit 41c21f5

4 files changed

Lines changed: 15 additions & 22 deletions

File tree

lib/fiddle/jruby.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,17 @@ def initialize(ret, args, abi = Function::DEFAULT)
185185
if ffi_args.size == 1 && ffi_args[0] == FFI::Type::Builtin::VOID
186186
ffi_args = []
187187
end
188-
@function = FFI::Function.new(
189-
Fiddle::JRuby.__ffi_type__(@ctype),
190-
ffi_args,
191-
self,
192-
:convention => abi
193-
)
188+
return_type = Fiddle::JRuby.__ffi_type__(@ctype)
189+
raise "#{self.class} must implement #call" unless respond_to?(:call)
190+
callable = method(:call)
191+
@function = FFI::Function.new(return_type, ffi_args, callable, convention: abi)
194192
@freed = false
195193
end
196194

195+
def to_ptr
196+
@function
197+
end
198+
197199
def to_i
198200
@function.to_i
199201
end

test/fiddle/test_closure.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66

77
module Fiddle
88
class TestClosure < Fiddle::TestCase
9-
def setup
10-
if RUBY_ENGINE == "truffleruby"
11-
omit("FFI::Function don't accept #call-able object with TruffleRuby")
12-
end
13-
end
14-
159
def teardown
1610
super
1711
# We can't use ObjectSpace with JRuby.
@@ -64,7 +58,7 @@ def call thing
6458
end
6559

6660
def test_const_string
67-
if RUBY_ENGINE == "jruby"
61+
if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
6862
omit("Closure with :const_string works but " +
6963
"Function with :const_string doesn't work with JRuby")
7064
end
@@ -125,9 +119,14 @@ def test_memsize_ruby_dev_42480
125119
end
126120

127121
require 'objspace'
122+
closure_class = Class.new(Closure) do
123+
def call
124+
10
125+
end
126+
end
128127
n = 10000
129128
n.times do
130-
Closure.create(:int, [:void]) do |closure|
129+
closure_class.create(:int, [:void]) do |closure|
131130
ObjectSpace.memsize_of(closure)
132131
end
133132
end

test/fiddle/test_func.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ def test_strtod
6464
end
6565

6666
def test_qsort1
67-
if RUBY_ENGINE == "truffleruby"
68-
omit("TruffleRuby's FFI::Function don't accept #call-able object")
69-
end
70-
7167
closure_class = Class.new(Closure) do
7268
def call(x, y)
7369
Pointer.new(x)[0] <=> Pointer.new(y)[0]

test/fiddle/test_function.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ def test_call
9494
end
9595

9696
def test_argument_count
97-
if RUBY_ENGINE == "truffleruby"
98-
omit("TruffleRuby's FFI::Function don't accept #call-able object")
99-
end
100-
10197
closure_class = Class.new(Closure) do
10298
def call one
10399
10 + one

0 commit comments

Comments
 (0)