Skip to content

Commit

Permalink
Rename the FFI backend since it is no longer JRuby-specific
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 9, 2024
1 parent 41c21f5 commit bf06273
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 137 deletions.
4 changes: 1 addition & 3 deletions fiddle.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ Gem::Specification.new do |spec|
"lib/fiddle.rb",
"lib/fiddle/closure.rb",
"lib/fiddle/cparser.rb",
"lib/fiddle/ffi_backend.rb",
"lib/fiddle/function.rb",
"lib/fiddle/import.rb",
"lib/fiddle/jruby.rb",
"lib/fiddle/pack.rb",
"lib/fiddle/ruby.rb",
"lib/fiddle/struct.rb",
"lib/fiddle/truffleruby.rb",
"lib/fiddle/types.rb",
"lib/fiddle/value.rb",
"lib/fiddle/version.rb",
Expand Down
6 changes: 5 additions & 1 deletion lib/fiddle.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

require "fiddle/#{RUBY_ENGINE}"
if RUBY_ENGINE == 'ruby'
require 'fiddle.so'
else
require 'fiddle/ffi_backend'
end
require 'fiddle/closure'
require 'fiddle/function'
require 'fiddle/version'
Expand Down
120 changes: 60 additions & 60 deletions lib/fiddle/jruby.rb → lib/fiddle/ffi_backend.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This is part of JRuby's FFI-based fiddle implementation.
# This is based on JRuby's FFI-based fiddle implementation.

require 'ffi'

Expand Down Expand Up @@ -74,7 +74,7 @@ module Types

WINDOWS = FFI::Platform.windows?

module JRuby
module FFIBackend
FFITypes = {
'c' => FFI::Type::INT8,
'h' => FFI::Type::INT16,
Expand Down Expand Up @@ -104,16 +104,16 @@ module JRuby
Types::VARIADIC => FFI::Type::Builtin::VARARGS,
}

def self.__ffi_type__(dl_type)
if dl_type.is_a?(Symbol)
dl_type = Types.const_get(dl_type.to_s.upcase)
def self.to_ffi_type(fiddle_type)
if fiddle_type.is_a?(Symbol)
fiddle_type = Types.const_get(fiddle_type.to_s.upcase)
end
if !dl_type.is_a?(Integer) && dl_type.respond_to?(:to_int)
dl_type = dl_type.to_int
if !fiddle_type.is_a?(Integer) && fiddle_type.respond_to?(:to_int)
fiddle_type = fiddle_type.to_int
end
ffi_type = FFITypes[dl_type]
ffi_type = FFITypes[-dl_type] if ffi_type.nil? && dl_type.is_a?(Integer) && dl_type < 0
raise TypeError.new("cannot convert #{dl_type} to ffi") unless ffi_type
ffi_type = FFITypes[fiddle_type]
ffi_type = FFITypes[-fiddle_type] if ffi_type.nil? && fiddle_type.is_a?(Integer) && fiddle_type < 0
raise TypeError.new("cannot convert #{fiddle_type} to ffi") unless ffi_type
ffi_type
end
end
Expand All @@ -133,8 +133,8 @@ def initialize(ptr, args, return_type, abi = DEFAULT, kwargs = nil)
@ptr, @args, @return_type, @abi = ptr, args, return_type, abi
raise TypeError.new "invalid argument types" unless args.is_a?(Array)

ffi_return_type = Fiddle::JRuby::__ffi_type__(@return_type)
ffi_args = @args.map { |t| Fiddle::JRuby.__ffi_type__(t) }
ffi_return_type = Fiddle::FFIBackend.to_ffi_type(@return_type)
ffi_args = @args.map { |t| Fiddle::FFIBackend.to_ffi_type(t) }
pointer = FFI::Pointer.new(ptr.to_i)
options = {convention: @abi}
if ffi_args.last == FFI::Type::Builtin::VARARGS
Expand All @@ -156,7 +156,7 @@ def call(*args, &block)
if args[i] == :const_string || args[i] == Types::CONST_STRING
args[i + 1] = String.try_convert(args[i + 1]) || args[i + 1]
end
args[i] = Fiddle::JRuby.__ffi_type__(args[i])
args[i] = Fiddle::FFIBackend.to_ffi_type(args[i])
end
else
args.map! do |arg|
Expand All @@ -181,11 +181,11 @@ def initialize(ret, args, abi = Function::DEFAULT)
raise TypeError.new "invalid argument types" unless args.is_a?(Array)

@ctype, @args = ret, args
ffi_args = @args.map { |t| Fiddle::JRuby.__ffi_type__(t) }
ffi_args = @args.map { |t| Fiddle::FFIBackend.to_ffi_type(t) }
if ffi_args.size == 1 && ffi_args[0] == FFI::Type::Builtin::VOID
ffi_args = []
end
return_type = Fiddle::JRuby.__ffi_type__(@ctype)
return_type = Fiddle::FFIBackend.to_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)
Expand Down Expand Up @@ -563,51 +563,51 @@ def cleared?
RUBY_FREE = Fiddle::Pointer::LibC::FREE.address
NULL = Fiddle::Pointer.new(0)

ALIGN_VOIDP = Fiddle::JRuby::FFITypes[Types::VOIDP].alignment
ALIGN_CHAR = Fiddle::JRuby::FFITypes[Types::CHAR].alignment
ALIGN_SHORT = Fiddle::JRuby::FFITypes[Types::SHORT].alignment
ALIGN_INT = Fiddle::JRuby::FFITypes[Types::INT].alignment
ALIGN_LONG = Fiddle::JRuby::FFITypes[Types::LONG].alignment
ALIGN_LONG_LONG = Fiddle::JRuby::FFITypes[Types::LONG_LONG].alignment
ALIGN_INT8_T = Fiddle::JRuby::FFITypes[Types::INT8_T].alignment
ALIGN_INT16_T = Fiddle::JRuby::FFITypes[Types::INT16_T].alignment
ALIGN_INT32_T = Fiddle::JRuby::FFITypes[Types::INT32_T].alignment
ALIGN_INT64_T = Fiddle::JRuby::FFITypes[Types::INT64_T].alignment
ALIGN_FLOAT = Fiddle::JRuby::FFITypes[Types::FLOAT].alignment
ALIGN_DOUBLE = Fiddle::JRuby::FFITypes[Types::DOUBLE].alignment
ALIGN_BOOL = Fiddle::JRuby::FFITypes[Types::BOOL].alignment
ALIGN_SIZE_T = Fiddle::JRuby::FFITypes[Types::SIZE_T].alignment
ALIGN_VOIDP = Fiddle::FFIBackend::FFITypes[Types::VOIDP].alignment
ALIGN_CHAR = Fiddle::FFIBackend::FFITypes[Types::CHAR].alignment
ALIGN_SHORT = Fiddle::FFIBackend::FFITypes[Types::SHORT].alignment
ALIGN_INT = Fiddle::FFIBackend::FFITypes[Types::INT].alignment
ALIGN_LONG = Fiddle::FFIBackend::FFITypes[Types::LONG].alignment
ALIGN_LONG_LONG = Fiddle::FFIBackend::FFITypes[Types::LONG_LONG].alignment
ALIGN_INT8_T = Fiddle::FFIBackend::FFITypes[Types::INT8_T].alignment
ALIGN_INT16_T = Fiddle::FFIBackend::FFITypes[Types::INT16_T].alignment
ALIGN_INT32_T = Fiddle::FFIBackend::FFITypes[Types::INT32_T].alignment
ALIGN_INT64_T = Fiddle::FFIBackend::FFITypes[Types::INT64_T].alignment
ALIGN_FLOAT = Fiddle::FFIBackend::FFITypes[Types::FLOAT].alignment
ALIGN_DOUBLE = Fiddle::FFIBackend::FFITypes[Types::DOUBLE].alignment
ALIGN_BOOL = Fiddle::FFIBackend::FFITypes[Types::BOOL].alignment
ALIGN_SIZE_T = Fiddle::FFIBackend::FFITypes[Types::SIZE_T].alignment
ALIGN_SSIZE_T = ALIGN_SIZE_T
ALIGN_PTRDIFF_T = Fiddle::JRuby::FFITypes[Types::PTRDIFF_T].alignment
ALIGN_INTPTR_T = Fiddle::JRuby::FFITypes[Types::INTPTR_T].alignment
ALIGN_UINTPTR_T = Fiddle::JRuby::FFITypes[Types::UINTPTR_T].alignment

SIZEOF_VOIDP = Fiddle::JRuby::FFITypes[Types::VOIDP].size
SIZEOF_CHAR = Fiddle::JRuby::FFITypes[Types::CHAR].size
SIZEOF_UCHAR = Fiddle::JRuby::FFITypes[Types::UCHAR].size
SIZEOF_SHORT = Fiddle::JRuby::FFITypes[Types::SHORT].size
SIZEOF_USHORT = Fiddle::JRuby::FFITypes[Types::USHORT].size
SIZEOF_INT = Fiddle::JRuby::FFITypes[Types::INT].size
SIZEOF_UINT = Fiddle::JRuby::FFITypes[Types::UINT].size
SIZEOF_LONG = Fiddle::JRuby::FFITypes[Types::LONG].size
SIZEOF_ULONG = Fiddle::JRuby::FFITypes[Types::ULONG].size
SIZEOF_LONG_LONG = Fiddle::JRuby::FFITypes[Types::LONG_LONG].size
SIZEOF_ULONG_LONG = Fiddle::JRuby::FFITypes[Types::ULONG_LONG].size
SIZEOF_INT8_T = Fiddle::JRuby::FFITypes[Types::INT8_T].size
SIZEOF_UINT8_T = Fiddle::JRuby::FFITypes[Types::UINT8_T].size
SIZEOF_INT16_T = Fiddle::JRuby::FFITypes[Types::INT16_T].size
SIZEOF_UINT16_T = Fiddle::JRuby::FFITypes[Types::UINT16_T].size
SIZEOF_INT32_T = Fiddle::JRuby::FFITypes[Types::INT32_T].size
SIZEOF_UINT32_T = Fiddle::JRuby::FFITypes[Types::UINT32_T].size
SIZEOF_INT64_T = Fiddle::JRuby::FFITypes[Types::INT64_T].size
SIZEOF_UINT64_T = Fiddle::JRuby::FFITypes[Types::UINT64_T].size
SIZEOF_FLOAT = Fiddle::JRuby::FFITypes[Types::FLOAT].size
SIZEOF_DOUBLE = Fiddle::JRuby::FFITypes[Types::DOUBLE].size
SIZEOF_BOOL = Fiddle::JRuby::FFITypes[Types::BOOL].size
SIZEOF_SIZE_T = Fiddle::JRuby::FFITypes[Types::SIZE_T].size
ALIGN_PTRDIFF_T = Fiddle::FFIBackend::FFITypes[Types::PTRDIFF_T].alignment
ALIGN_INTPTR_T = Fiddle::FFIBackend::FFITypes[Types::INTPTR_T].alignment
ALIGN_UINTPTR_T = Fiddle::FFIBackend::FFITypes[Types::UINTPTR_T].alignment

SIZEOF_VOIDP = Fiddle::FFIBackend::FFITypes[Types::VOIDP].size
SIZEOF_CHAR = Fiddle::FFIBackend::FFITypes[Types::CHAR].size
SIZEOF_UCHAR = Fiddle::FFIBackend::FFITypes[Types::UCHAR].size
SIZEOF_SHORT = Fiddle::FFIBackend::FFITypes[Types::SHORT].size
SIZEOF_USHORT = Fiddle::FFIBackend::FFITypes[Types::USHORT].size
SIZEOF_INT = Fiddle::FFIBackend::FFITypes[Types::INT].size
SIZEOF_UINT = Fiddle::FFIBackend::FFITypes[Types::UINT].size
SIZEOF_LONG = Fiddle::FFIBackend::FFITypes[Types::LONG].size
SIZEOF_ULONG = Fiddle::FFIBackend::FFITypes[Types::ULONG].size
SIZEOF_LONG_LONG = Fiddle::FFIBackend::FFITypes[Types::LONG_LONG].size
SIZEOF_ULONG_LONG = Fiddle::FFIBackend::FFITypes[Types::ULONG_LONG].size
SIZEOF_INT8_T = Fiddle::FFIBackend::FFITypes[Types::INT8_T].size
SIZEOF_UINT8_T = Fiddle::FFIBackend::FFITypes[Types::UINT8_T].size
SIZEOF_INT16_T = Fiddle::FFIBackend::FFITypes[Types::INT16_T].size
SIZEOF_UINT16_T = Fiddle::FFIBackend::FFITypes[Types::UINT16_T].size
SIZEOF_INT32_T = Fiddle::FFIBackend::FFITypes[Types::INT32_T].size
SIZEOF_UINT32_T = Fiddle::FFIBackend::FFITypes[Types::UINT32_T].size
SIZEOF_INT64_T = Fiddle::FFIBackend::FFITypes[Types::INT64_T].size
SIZEOF_UINT64_T = Fiddle::FFIBackend::FFITypes[Types::UINT64_T].size
SIZEOF_FLOAT = Fiddle::FFIBackend::FFITypes[Types::FLOAT].size
SIZEOF_DOUBLE = Fiddle::FFIBackend::FFITypes[Types::DOUBLE].size
SIZEOF_BOOL = Fiddle::FFIBackend::FFITypes[Types::BOOL].size
SIZEOF_SIZE_T = Fiddle::FFIBackend::FFITypes[Types::SIZE_T].size
SIZEOF_SSIZE_T = SIZEOF_SIZE_T
SIZEOF_PTRDIFF_T = Fiddle::JRuby::FFITypes[Types::PTRDIFF_T].size
SIZEOF_INTPTR_T = Fiddle::JRuby::FFITypes[Types::INTPTR_T].size
SIZEOF_UINTPTR_T = Fiddle::JRuby::FFITypes[Types::UINTPTR_T].size
SIZEOF_CONST_STRING = Fiddle::JRuby::FFITypes[Types::VOIDP].size
SIZEOF_PTRDIFF_T = Fiddle::FFIBackend::FFITypes[Types::PTRDIFF_T].size
SIZEOF_INTPTR_T = Fiddle::FFIBackend::FFITypes[Types::INTPTR_T].size
SIZEOF_UINTPTR_T = Fiddle::FFIBackend::FFITypes[Types::UINTPTR_T].size
SIZEOF_CONST_STRING = Fiddle::FFIBackend::FFITypes[Types::VOIDP].size
end
1 change: 0 additions & 1 deletion lib/fiddle/ruby.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/fiddle/truffleruby.rb

This file was deleted.

4 changes: 4 additions & 0 deletions test/fiddle/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def teardown
end
end

def ffi_backend?
RUBY_ENGINE != 'ruby'
end

def under_gc_stress
stress, GC.stress = GC.stress, true
yield
Expand Down
4 changes: 2 additions & 2 deletions test/fiddle/test_closure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def call thing
end

def test_const_string
if RUBY_ENGINE == "jruby" or RUBY_ENGINE == "truffleruby"
if ffi_backend?
omit("Closure with :const_string works but " +
"Function with :const_string doesn't work with JRuby")
"Function with :const_string doesn't work with FFI backend")
end

closure_class = Class.new(Closure) do
Expand Down
15 changes: 6 additions & 9 deletions test/fiddle/test_fiddle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class TestFiddle < Fiddle::TestCase
def test_nil_true_etc
if RUBY_ENGINE == "jruby"
omit("Fiddle::Q* aren't supported with JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Q* aren't supported with TruffleRuby")
if ffi_backend?
omit("Fiddle::Q* aren't supported with FFI backend")
end

assert_equal Fiddle::Qtrue, Fiddle.dlwrap(true)
Expand All @@ -33,8 +30,8 @@ def test_dlopen_linker_script_input_linux
if Dir.glob("/usr/lib/*/libncurses.so").empty?
omit("libncurses.so is needed")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Handle#file_name doesn't exist in TruffleRuby")
if ffi_backend?
omit("Fiddle::Handle#file_name doesn't exist in FFI backend")
end

# libncurses.so uses INPUT() on Debian GNU/Linux
Expand All @@ -51,8 +48,8 @@ def test_dlopen_linker_script_input_linux

def test_dlopen_linker_script_group_linux
omit("This is only for Linux") unless RUBY_PLATFORM.match?("linux")
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Handle#file_name doesn't exist in TruffleRuby")
if ffi_backend?
omit("Fiddle::Handle#file_name doesn't exist in FFI backend")
end

# libc.so uses GROUP() on Debian GNU/Linux
Expand Down
7 changes: 2 additions & 5 deletions test/fiddle/test_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,8 @@ def call one
end

def test_last_error
if RUBY_ENGINE == "jruby"
omit("Fiddle.last_error doesn't work with JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle.last_error doesn't work with TruffleRuby")
if ffi_backend?
omit("Fiddle.last_error doesn't work with FFI backend")
end

func = Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
Expand Down
35 changes: 10 additions & 25 deletions test/fiddle/test_handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@ class TestHandle < TestCase
include Fiddle

def test_to_i
if RUBY_ENGINE == "jruby"
omit("Fiddle::Handle#to_i is unavailable with JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Handle#to_i is unavailable with TruffleRuby")
if ffi_backend?
omit("Fiddle::Handle#to_i is unavailable with FFI backend")
end

handle = Fiddle::Handle.new(LIBC_SO)
assert_kind_of Integer, handle.to_i
end

def test_to_ptr
if RUBY_ENGINE == "jruby"
omit("Fiddle::Handle#to_i is unavailable with JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Handle#to_i is unavailable with TruffleRuby")
if ffi_backend?
omit("Fiddle::Handle#to_i is unavailable with FFI backend")
end

handle = Fiddle::Handle.new(LIBC_SO)
Expand All @@ -40,11 +34,8 @@ def test_static_sym_unknown
end

def test_static_sym
if RUBY_ENGINE == "jruby"
omit("We can't assume static symbols with JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("We can't assume static symbols with TruffleRuby")
if ffi_backend?
omit("We can't assume static symbols with FFI backend")
end

begin
Expand Down Expand Up @@ -142,11 +133,8 @@ def test_disable_close
end

def test_file_name
if RUBY_ENGINE == "jruby"
omit("Fiddle::Handle#file_name doesn't exist in JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Handle#file_name doesn't exist in TruffleRuby")
if ffi_backend?
omit("Fiddle::Handle#file_name doesn't exist in FFI backend")
end

file_name = Handle.new(LIBC_SO).file_name
Expand All @@ -167,11 +155,8 @@ def test_file_name
end

def test_NEXT
if RUBY_ENGINE == "jruby"
omit("Fiddle::Handle::NEXT doesn't exist in JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("Fiddle::Handle::NEXT doesn't exist in TruffleRuby")
if ffi_backend?
omit("Fiddle::Handle::NEXT doesn't exist in FFI backend")
end

begin
Expand Down
7 changes: 2 additions & 5 deletions test/fiddle/test_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,8 @@ def test_unsigned_result()
end

def test_io()
if RUBY_ENGINE == "jruby"
omit("BUILD_RUBY_PLATFORM doesn't exist in JRuby")
end
if RUBY_ENGINE == "truffleruby"
omit("BUILD_RUBY_PLATFORM doesn't exist in TruffleRuby")
if ffi_backend?
omit("BUILD_RUBY_PLATFORM doesn't exist in FFI backend")
end

if( RUBY_PLATFORM != BUILD_RUBY_PLATFORM ) || !defined?(LIBC.fprintf)
Expand Down
Loading

0 comments on commit bf06273

Please sign in to comment.