Skip to content

Commit

Permalink
Import JRuby implementation
Browse files Browse the repository at this point in the history
Fig GH-104

lib/fiddle/jruby.rb is based on
https://github.com/jruby/jruby/blob/master/lib/ruby/stdlib/fiddle/jruby.rb
.

Here are changes for it:

* Move `Fiddle::TYPE_*` to `Fiddle::Types::*`
* Add `Fiddle::Types::VARIADIC`
* Add `Fiddle::Types::CONST_STRING`
* Add `Fiddle::Types::BOOL`
* Add `Fiddle::Types::INT8_T`
* Add `Fiddle::Types::UINT8_T`
* Add `Fiddle::Types::INT16_T`
* Add `Fiddle::Types::UINT16_T`
* Add `Fiddle::Types::INT32_T`
* Add `Fiddle::Types::UINT32_T`
* Add `Fiddle::Types::INT64_T`
* Add `Fiddle::Types::UINT64_T`
* Add more `Fiddle::ALIGN_*` for the above new `Fiddle::Types::*`
* Add more `Fiddle::SIZEOF_*` for the above new `Fiddle::Types::*`
* Add support for specifying type as not only `Fiddle::Types::*` but also
  `Symbol` like `:int`
* Add support for variable size arguments in `Fiddle::Function`
* Add `Fiddle::Closure#free`
* Add `Fiddle::Closure#freed?`
* Add `Fiddle::Error` as base the error class
* Add `Fiddle::Pointer#call_free` and stop using `FFI::AutoPointer` in
  `Fiddle::Pointer`
* Add support for `Fiddle::Pointer.malloc {}`
  `Fiddle::Pointer`
* Add support for `Fiddle::Pointer#free=`
* Add `Fiddle::Pointer#freed?`
* Use binary string not C string for `Fiddle::Pointer#[]`
* Add `Fiddle::Handle.sym_defined?`
* Add `Fiddle::Handle#sym_defined?`
* Add `Fiddle::Handle::DEFAULT`
* Add `Fiddle::ClearedReferenceError`
* Add no-op `Fiddle::Pinned`

Some features are still "not implemented". So there are some "omit"s
for JRuby in tests.
  • Loading branch information
kou committed Sep 27, 2024
1 parent aad5a3b commit 4b0b8c0
Show file tree
Hide file tree
Showing 16 changed files with 845 additions and 83 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- '3.1'
- '3.2'
- debug
- jruby
include:
- { os: windows-latest , ruby: mingw }
- { os: windows-latest , ruby: mswin }
Expand Down Expand Up @@ -53,13 +54,9 @@ jobs:
matrix.ruby == 'mingw' ||
matrix.ruby == 'mswin'
- run: rake build

- run: gem install pkg/*.gem

- run: rake test
env:
RUBYOPT: --disable=gems

- run: rake install

- name: Run test against installed gem
# We can't use Fiddle gem with RubyInstaller because
Expand All @@ -69,6 +66,7 @@ jobs:
matrix.os != 'windows-latest' ||
(matrix.os == 'windows-latest' && matrix.ruby == 'mswin')
run: |
ruby -run -e mkdir -- -p tmp/
ruby -run -e cp -- -pr test/ tmp/
cd tmp
ruby test/run.rb
Expand Down
13 changes: 9 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ namespace :version do
end
end

require 'rake/extensiontask'
Rake::ExtensionTask.new("fiddle")
Rake::ExtensionTask.new("-test-/memory_view")
if RUBY_ENGINE == "ruby"
require 'rake/extensiontask'
Rake::ExtensionTask.new("fiddle")
Rake::ExtensionTask.new("-test-/memory_view")
task test: :compile
else
task :compile
end

task :default => [:compile, :test]
task default: :test
5 changes: 5 additions & 0 deletions ext/fiddle/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# frozen_string_literal: true
require 'mkmf'

if RUBY_ENGINE == "jruby"
File.write('Makefile', dummy_makefile("").join)
return
end

# :stopdoc:

def gcc?
Expand Down
1 change: 1 addition & 0 deletions fiddle.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Gem::Specification.new do |spec|
"lib/fiddle/cparser.rb",
"lib/fiddle/function.rb",
"lib/fiddle/import.rb",
"lib/fiddle/jruby.rb",
"lib/fiddle/pack.rb",
"lib/fiddle/struct.rb",
"lib/fiddle/types.rb",
Expand Down
48 changes: 40 additions & 8 deletions lib/fiddle.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# frozen_string_literal: true

require 'fiddle.so'
case RUBY_ENGINE
when 'jruby'
require 'fiddle/jruby'
else
require 'fiddle.so'
end
require 'fiddle/closure'
require 'fiddle/function'
require 'fiddle/version'
Expand All @@ -10,36 +15,63 @@ module Fiddle
# Returns the last win32 +Error+ of the current executing +Thread+ or nil
# if none
def self.win32_last_error
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__]
if RUBY_ENGINE == 'jruby'
errno = FFI.errno
errno == 0 ? nil : errno
else
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__]
end
end

# Sets the last win32 +Error+ of the current executing +Thread+ to +error+
def self.win32_last_error= error
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error
if RUBY_ENGINE == 'jruby'
FFI.errno = error || 0
else
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error
end
end

# Returns the last win32 socket +Error+ of the current executing
# +Thread+ or nil if none
def self.win32_last_socket_error
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__]
if RUBY_ENGINE == 'jruby'
errno = FFI.errno
errno == 0 ? nil : errno
else
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__]
end
end

# Sets the last win32 socket +Error+ of the current executing
# +Thread+ to +error+
def self.win32_last_socket_error= error
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] = error
if RUBY_ENGINE == 'jruby'
FFI.errno = error || 0
else
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] = error
end
end
end

# Returns the last +Error+ of the current executing +Thread+ or nil if none
def self.last_error
Thread.current[:__FIDDLE_LAST_ERROR__]
if RUBY_ENGINE == 'jruby'
errno = FFI.errno
errno == 0 ? nil : errno
else
Thread.current[:__FIDDLE_LAST_ERROR__]
end
end

# Sets the last +Error+ of the current executing +Thread+ to +error+
def self.last_error= error
Thread.current[:__DL2_LAST_ERROR__] = error
Thread.current[:__FIDDLE_LAST_ERROR__] = error
if RUBY_ENGINE == 'jruby'
FFI.errno = error || 0
else
Thread.current[:__DL2_LAST_ERROR__] = error
Thread.current[:__FIDDLE_LAST_ERROR__] = error
end
end

# call-seq: dlopen(library) => Fiddle::Handle
Expand Down
Loading

0 comments on commit 4b0b8c0

Please sign in to comment.