Skip to content

Commit 418bd89

Browse files
committed
Use JRuby implementation for TruffleRuby
Fix GH-145 lib/fiddle/truffleruby.rb uses lib/fiddle/jruby.rb. lib/fiddle/jruby.rb uses FFI API. So we can use it for TruffleRuby too.
1 parent d76c87b commit 418bd89

File tree

12 files changed

+83
-1
lines changed

12 files changed

+83
-1
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ jobs:
2626
- '3.2'
2727
- debug
2828
- jruby
29+
- truffleruby
2930
include:
3031
- { os: windows-latest , ruby: mingw }
3132
- { os: windows-latest , ruby: mswin }
3233
exclude:
3334
- { os: macos-14 , ruby: '2.5' }
3435
- { os: windows-latest , ruby: '3.0' }
3536
- { os: windows-latest , ruby: debug }
37+
- { os: windows-latest , ruby: truffleruby }
3638

3739
steps:
3840
- uses: actions/checkout@v4

ext/fiddle/extconf.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
require 'mkmf'
33

4-
if RUBY_ENGINE == "jruby"
4+
unless RUBY_ENGINE == "ruby"
55
File.write('Makefile', dummy_makefile("").join)
66
return
77
end

fiddle.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Gem::Specification.new do |spec|
4444
"lib/fiddle/pack.rb",
4545
"lib/fiddle/ruby.rb",
4646
"lib/fiddle/struct.rb",
47+
"lib/fiddle/truffleruby.rb",
4748
"lib/fiddle/types.rb",
4849
"lib/fiddle/value.rb",
4950
"lib/fiddle/version.rb",

lib/fiddle/jruby.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ def call(*args, &block);
158158
end
159159
args[i] = Fiddle::JRuby.__ffi_type__(args[i])
160160
end
161+
else
162+
args.size.times do |i|
163+
arg = args[i]
164+
next unless arg.respond_to?(:to_ptr)
165+
loop do
166+
arg = arg.to_ptr
167+
break if arg.is_a?(FFI::Pointer)
168+
break unless arg.respond_to?(:to_ptr)
169+
end
170+
args[i] = arg
171+
end
161172
end
162173
result = @function.call(*args, &block)
163174
result = Pointer.new(result) if result.is_a?(FFI::Pointer)

lib/fiddle/truffleruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require_relative 'jruby'

test/fiddle/test_closure.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
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+
915
def teardown
1016
super
1117
# We can't use ObjectSpace with JRuby.

test/fiddle/test_fiddle.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def test_nil_true_etc
99
if RUBY_ENGINE == "jruby"
1010
omit("Fiddle::Q* aren't supported with JRuby")
1111
end
12+
if RUBY_ENGINE == "truffleruby"
13+
omit("Fiddle::Q* aren't supported with TruffleRuby")
14+
end
1215

1316
assert_equal Fiddle::Qtrue, Fiddle.dlwrap(true)
1417
assert_equal Fiddle::Qfalse, Fiddle.dlwrap(false)
@@ -30,6 +33,10 @@ def test_dlopen_linker_script_input_linux
3033
if Dir.glob("/usr/lib/*/libncurses.so").empty?
3134
omit("libncurses.so is needed")
3235
end
36+
if RUBY_ENGINE == "truffleruby"
37+
omit("Fiddle::Handle#file_name doesn't exist in TruffleRuby")
38+
end
39+
3340
# libncurses.so uses INPUT() on Debian GNU/Linux
3441
# $ cat /usr/lib/x86_64-linux-gnu/libncurses.so
3542
# INPUT(libncurses.so.6 -ltinfo)
@@ -44,6 +51,10 @@ def test_dlopen_linker_script_input_linux
4451

4552
def test_dlopen_linker_script_group_linux
4653
omit("This is only for Linux") unless RUBY_PLATFORM.match?("linux")
54+
if RUBY_ENGINE == "truffleruby"
55+
omit("Fiddle::Handle#file_name doesn't exist in TruffleRuby")
56+
end
57+
4758
# libc.so uses GROUP() on Debian GNU/Linux
4859
# $ cat /usr/lib/x86_64-linux-gnu/libc.so
4960
# /* GNU ld script

test/fiddle/test_func.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ 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+
6771
closure_class = Class.new(Closure) do
6872
def call(x, y)
6973
Pointer.new(x)[0] <=> Pointer.new(y)[0]

test/fiddle/test_function.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def test_need_gvl?
4141
if RUBY_ENGINE == "jruby"
4242
omit("rb_str_dup() doesn't exist in JRuby")
4343
end
44+
if RUBY_ENGINE == "truffleruby"
45+
omit("rb_str_dup() doesn't work with TruffleRuby")
46+
end
4447

4548
libruby = Fiddle.dlopen(nil)
4649
rb_str_dup = Function.new(libruby['rb_str_dup'],
@@ -91,6 +94,10 @@ def test_call
9194
end
9295

9396
def test_argument_count
97+
if RUBY_ENGINE == "truffleruby"
98+
omit("TruffleRuby's FFI::Function don't accept #call-able object")
99+
end
100+
94101
closure_class = Class.new(Closure) do
95102
def call one
96103
10 + one
@@ -112,6 +119,9 @@ def test_last_error
112119
if RUBY_ENGINE == "jruby"
113120
omit("Fiddle.last_error doesn't work with JRuby")
114121
end
122+
if RUBY_ENGINE == "truffleruby"
123+
omit("Fiddle.last_error doesn't work with TruffleRuby")
124+
end
115125

116126
func = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
117127

@@ -219,6 +229,9 @@ def test_no_memory_leak
219229
if RUBY_ENGINE == "jruby"
220230
omit("rb_obj_frozen_p() doesn't exist in JRuby")
221231
end
232+
if RUBY_ENGINE == "truffleruby"
233+
omit("memory leak detection is fragile with TruffleRuby")
234+
end
222235

223236
if respond_to?(:assert_nothing_leaked_memory)
224237
rb_obj_frozen_p_symbol = Fiddle.dlopen(nil)["rb_obj_frozen_p"]

test/fiddle/test_handle.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def test_to_i
1212
if RUBY_ENGINE == "jruby"
1313
omit("Fiddle::Handle#to_i is unavailable with JRuby")
1414
end
15+
if RUBY_ENGINE == "truffleruby"
16+
omit("Fiddle::Handle#to_i is unavailable with TruffleRuby")
17+
end
1518

1619
handle = Fiddle::Handle.new(LIBC_SO)
1720
assert_kind_of Integer, handle.to_i
@@ -21,6 +24,9 @@ def test_to_ptr
2124
if RUBY_ENGINE == "jruby"
2225
omit("Fiddle::Handle#to_i is unavailable with JRuby")
2326
end
27+
if RUBY_ENGINE == "truffleruby"
28+
omit("Fiddle::Handle#to_i is unavailable with TruffleRuby")
29+
end
2430

2531
handle = Fiddle::Handle.new(LIBC_SO)
2632
ptr = handle.to_ptr
@@ -37,6 +43,9 @@ def test_static_sym
3743
if RUBY_ENGINE == "jruby"
3844
omit("We can't assume static symbols with JRuby")
3945
end
46+
if RUBY_ENGINE == "truffleruby"
47+
omit("We can't assume static symbols with TruffleRuby")
48+
end
4049

4150
begin
4251
# Linux / Darwin / FreeBSD
@@ -136,6 +145,9 @@ def test_file_name
136145
if RUBY_ENGINE == "jruby"
137146
omit("Fiddle::Handle#file_name doesn't exist in JRuby")
138147
end
148+
if RUBY_ENGINE == "truffleruby"
149+
omit("Fiddle::Handle#file_name doesn't exist in TruffleRuby")
150+
end
139151

140152
file_name = Handle.new(LIBC_SO).file_name
141153
if file_name
@@ -158,6 +170,9 @@ def test_NEXT
158170
if RUBY_ENGINE == "jruby"
159171
omit("Fiddle::Handle::NEXT doesn't exist in JRuby")
160172
end
173+
if RUBY_ENGINE == "truffleruby"
174+
omit("Fiddle::Handle::NEXT doesn't exist in TruffleRuby")
175+
end
161176

162177
begin
163178
# Linux / Darwin

0 commit comments

Comments
 (0)