Skip to content

Commit a47f153

Browse files
kouhsbt
authored andcommitted
Import JRuby implementation (#147)
Fix 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.
1 parent a392ee1 commit a47f153

File tree

15 files changed

+835
-70
lines changed

15 files changed

+835
-70
lines changed

ext/fiddle/extconf.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# frozen_string_literal: true
22
require 'mkmf'
33

4+
if RUBY_ENGINE == "jruby"
5+
File.write('Makefile', dummy_makefile("").join)
6+
return
7+
end
8+
49
# :stopdoc:
510

611
def gcc?

ext/fiddle/fiddle.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ Gem::Specification.new do |spec|
4040
"lib/fiddle/cparser.rb",
4141
"lib/fiddle/function.rb",
4242
"lib/fiddle/import.rb",
43+
"lib/fiddle/jruby.rb",
4344
"lib/fiddle/pack.rb",
45+
"lib/fiddle/ruby.rb",
4446
"lib/fiddle/struct.rb",
4547
"lib/fiddle/types.rb",
4648
"lib/fiddle/value.rb",

ext/fiddle/lib/fiddle.rb

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
require 'fiddle.so'
3+
require "fiddle/#{RUBY_ENGINE}"
44
require 'fiddle/closure'
55
require 'fiddle/function'
66
require 'fiddle/version'
@@ -10,36 +10,63 @@ module Fiddle
1010
# Returns the last win32 +Error+ of the current executing +Thread+ or nil
1111
# if none
1212
def self.win32_last_error
13-
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__]
13+
if RUBY_ENGINE == 'jruby'
14+
errno = FFI.errno
15+
errno == 0 ? nil : errno
16+
else
17+
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__]
18+
end
1419
end
1520

1621
# Sets the last win32 +Error+ of the current executing +Thread+ to +error+
1722
def self.win32_last_error= error
18-
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error
23+
if RUBY_ENGINE == 'jruby'
24+
FFI.errno = error || 0
25+
else
26+
Thread.current[:__FIDDLE_WIN32_LAST_ERROR__] = error
27+
end
1928
end
2029

2130
# Returns the last win32 socket +Error+ of the current executing
2231
# +Thread+ or nil if none
2332
def self.win32_last_socket_error
24-
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__]
33+
if RUBY_ENGINE == 'jruby'
34+
errno = FFI.errno
35+
errno == 0 ? nil : errno
36+
else
37+
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__]
38+
end
2539
end
2640

2741
# Sets the last win32 socket +Error+ of the current executing
2842
# +Thread+ to +error+
2943
def self.win32_last_socket_error= error
30-
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] = error
44+
if RUBY_ENGINE == 'jruby'
45+
FFI.errno = error || 0
46+
else
47+
Thread.current[:__FIDDLE_WIN32_LAST_SOCKET_ERROR__] = error
48+
end
3149
end
3250
end
3351

3452
# Returns the last +Error+ of the current executing +Thread+ or nil if none
3553
def self.last_error
36-
Thread.current[:__FIDDLE_LAST_ERROR__]
54+
if RUBY_ENGINE == 'jruby'
55+
errno = FFI.errno
56+
errno == 0 ? nil : errno
57+
else
58+
Thread.current[:__FIDDLE_LAST_ERROR__]
59+
end
3760
end
3861

3962
# Sets the last +Error+ of the current executing +Thread+ to +error+
4063
def self.last_error= error
41-
Thread.current[:__DL2_LAST_ERROR__] = error
42-
Thread.current[:__FIDDLE_LAST_ERROR__] = error
64+
if RUBY_ENGINE == 'jruby'
65+
FFI.errno = error || 0
66+
else
67+
Thread.current[:__DL2_LAST_ERROR__] = error
68+
Thread.current[:__FIDDLE_LAST_ERROR__] = error
69+
end
4370
end
4471

4572
# call-seq: dlopen(library) => Fiddle::Handle

0 commit comments

Comments
 (0)