Skip to content

Commit

Permalink
Fix Fiddle::Closure on TruffleRuby
Browse files Browse the repository at this point in the history
* TruffleRuby requires a Proc or Method as the callable.
  • Loading branch information
eregon committed Oct 8, 2024
1 parent fc3be9c commit 41c21f5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
14 changes: 8 additions & 6 deletions lib/fiddle/jruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,17 @@ def initialize(ret, args, abi = Function::DEFAULT)
if ffi_args.size == 1 && ffi_args[0] == FFI::Type::Builtin::VOID
ffi_args = []
end
@function = FFI::Function.new(
Fiddle::JRuby.__ffi_type__(@ctype),
ffi_args,
self,
:convention => abi
)
return_type = Fiddle::JRuby.__ffi_type__(@ctype)
raise "#{self.class} must implement #call" unless respond_to?(:call)
callable = method(:call)
@function = FFI::Function.new(return_type, ffi_args, callable, convention: abi)
@freed = false
end

def to_ptr
@function
end

def to_i
@function.to_i
end
Expand Down
15 changes: 7 additions & 8 deletions test/fiddle/test_closure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

module Fiddle
class TestClosure < Fiddle::TestCase
def setup
if RUBY_ENGINE == "truffleruby"
omit("FFI::Function don't accept #call-able object with TruffleRuby")
end
end

def teardown
super
# We can't use ObjectSpace with JRuby.
Expand Down Expand Up @@ -64,7 +58,7 @@ def call thing
end

def test_const_string
if RUBY_ENGINE == "jruby"
if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
omit("Closure with :const_string works but " +
"Function with :const_string doesn't work with JRuby")
end
Expand Down Expand Up @@ -125,9 +119,14 @@ def test_memsize_ruby_dev_42480
end

require 'objspace'
closure_class = Class.new(Closure) do
def call
10
end
end
n = 10000
n.times do
Closure.create(:int, [:void]) do |closure|
closure_class.create(:int, [:void]) do |closure|
ObjectSpace.memsize_of(closure)
end
end
Expand Down
4 changes: 0 additions & 4 deletions test/fiddle/test_func.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def test_strtod
end

def test_qsort1
if RUBY_ENGINE == "truffleruby"
omit("TruffleRuby's FFI::Function don't accept #call-able object")
end

closure_class = Class.new(Closure) do
def call(x, y)
Pointer.new(x)[0] <=> Pointer.new(y)[0]
Expand Down
4 changes: 0 additions & 4 deletions test/fiddle/test_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ def test_call
end

def test_argument_count
if RUBY_ENGINE == "truffleruby"
omit("TruffleRuby's FFI::Function don't accept #call-able object")
end

closure_class = Class.new(Closure) do
def call one
10 + one
Expand Down

0 comments on commit 41c21f5

Please sign in to comment.