Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the Ruby files of the FFI gem on TruffleRuby >= 20.1.0 #768

Merged
merged 3 commits into from Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/ffi.rb
Expand Up @@ -8,10 +8,14 @@

require 'ffi/ffi'

elsif RUBY_ENGINE == 'jruby' && Gem::Version.new(JRUBY_VERSION) >= Gem::Version.new("9.3.pre")
elsif RUBY_ENGINE == 'jruby' && Gem::Version.new(RUBY_ENGINE_VERSION) >= Gem::Version.new("9.3.pre")
JRuby::Util.load_ext("org.jruby.ext.ffi.FFIService")
require 'ffi/ffi'

elsif RUBY_ENGINE == 'truffleruby' && Gem::Version.new(RUBY_ENGINE_VERSION) >= Gem::Version.new("20.1.0-dev-a")
require 'truffleruby/ffi_backend'
require 'ffi/ffi'

else
# Remove the ffi gem dir from the load path, then reload the internal ffi implementation
$LOAD_PATH.delete(File.dirname(__FILE__))
Expand Down
31 changes: 19 additions & 12 deletions lib/ffi/pointer.rb
Expand Up @@ -31,17 +31,24 @@
#

require 'ffi/platform'

# NOTE: all method definitions in this file are conditional on
# whether they are not already defined. This is needed because
# some Ruby implementations (e.g., TruffleRuby) might already
# provide these methods due to using FFI internally, and we
# should not override them to avoid warnings.

module FFI
class Pointer

# Pointer size
SIZE = Platform::ADDRESS_SIZE / 8
SIZE = Platform::ADDRESS_SIZE / 8 unless const_defined?(:SIZE)

# Return the size of a pointer on the current platform, in bytes
# @return [Numeric]
def self.size
SIZE
end
end unless respond_to?(:size)

# @param [nil,Numeric] len length of string to return
# @return [String]
Expand All @@ -54,7 +61,7 @@ def read_string(len=nil)
else
get_string(0)
end
end
end unless method_defined?(:read_string)

# @param [Numeric] len length of string to return
# @return [String]
Expand All @@ -64,7 +71,7 @@ def read_string(len=nil)
# ptr.read_string(len) # with len not nil
def read_string_length(len)
get_bytes(0, len)
end
end unless method_defined?(:read_string_length)

# @return [String]
# Read pointer's contents as a string.
Expand All @@ -73,7 +80,7 @@ def read_string_length(len)
# ptr.read_string # with no len
def read_string_to_null
get_string(0)
end
end unless method_defined?(:read_string_to_null)

# @param [String] str string to write
# @param [Numeric] len length of string to return
Expand All @@ -84,7 +91,7 @@ def read_string_to_null
# ptr.write_string(str, len) # with len not nil
def write_string_length(str, len)
put_bytes(0, str, 0, len)
end
end unless method_defined?(:write_string_length)

# @param [String] str string to write
# @param [Numeric] len length of string to return
Expand All @@ -95,7 +102,7 @@ def write_string(str, len=nil)
len = str.bytesize unless len
# Write the string data without NUL termination
put_bytes(0, str, 0, len)
end
end unless method_defined?(:write_string)

# @param [Type] type type of data to read from pointer's contents
# @param [Symbol] reader method to send to +self+ to read +type+
Expand All @@ -113,7 +120,7 @@ def read_array_of_type(type, reader, length)
tmp += size unless j == length-1 # avoid OOB
}
ary
end
end unless method_defined?(:read_array_of_type)

# @param [Type] type type of data to write to pointer's contents
# @param [Symbol] writer method to send to +self+ to write +type+
Expand All @@ -129,12 +136,12 @@ def write_array_of_type(type, writer, ary)
self.send(writer, i * size, val)
}
self
end
end unless method_defined?(:write_array_of_type)

# @return [self]
def to_ptr
self
end
end unless method_defined?(:to_ptr)

# @param [Symbol,Type] type of data to read
# @return [Object]
Expand All @@ -144,7 +151,7 @@ def to_ptr
# ptr.get(type, 0)
def read(type)
get(type, 0)
end
end unless method_defined?(:read)

# @param [Symbol,Type] type of data to read
# @param [Object] value to write
Expand All @@ -155,6 +162,6 @@ def read(type)
# ptr.put(type, 0)
def write(type, value)
put(type, 0, value)
end
end unless method_defined?(:write)
end
end