Skip to content

Commit ed2679c

Browse files
committed
Update non-gem stdlib from CRuby 3.3
1 parent 449075b commit ed2679c

File tree

14 files changed

+202
-117
lines changed

14 files changed

+202
-117
lines changed

lib/ruby/stdlib/expect.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# frozen_string_literal: true
22
$expect_verbose = false
33

4-
# Expect library adds the IO instance method #expect, which does similar act to
5-
# tcl's expect extension.
6-
#
7-
# In order to use this method, you must require expect:
8-
#
9-
# require 'expect'
10-
#
11-
# Please see #expect for usage.
124
class IO
135
# call-seq:
146
# IO#expect(pattern,timeout=9999999) -> Array
157
# IO#expect(pattern,timeout=9999999) { |result| ... } -> nil
168
#
9+
# The +expect+ library adds instance method IO#expect,
10+
# which is similar to the
11+
# {TCL expect extension}[https://www.tcl.tk/man/expect5.31/expect.1.html].
12+
#
13+
# To use this method, you must require +expect+:
14+
#
15+
# require 'expect'
16+
#
1717
# Reads from the IO until the given +pattern+ matches or the +timeout+ is over.
1818
#
1919
# It returns an array with the read buffer, followed by the matches.

lib/ruby/stdlib/fiddle.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,36 @@ def self.last_error= error
8888
#
8989
# See Fiddle::Handle.new for more.
9090
def dlopen library
91-
Fiddle::Handle.new library
91+
begin
92+
Fiddle::Handle.new(library)
93+
rescue DLError => error
94+
case RUBY_PLATFORM
95+
when /linux/
96+
case error.message
97+
when /\A(\/.+?): (?:invalid ELF header|file too short)/
98+
# This may be a linker script:
99+
# https://sourceware.org/binutils/docs/ld.html#Scripts
100+
path = $1
101+
else
102+
raise
103+
end
104+
else
105+
raise
106+
end
107+
108+
File.open(path) do |input|
109+
input.each_line do |line|
110+
case line
111+
when /\A\s*(?:INPUT|GROUP)\s*\(\s*([^\s,\)]+)/
112+
# TODO: Should we support multiple files?
113+
return dlopen($1)
114+
end
115+
end
116+
end
117+
118+
# Not found
119+
raise
120+
end
92121
end
93122
module_function :dlopen
94123

@@ -97,4 +126,8 @@ def dlopen library
97126
RTLD_GLOBAL = Handle::RTLD_GLOBAL # :nodoc:
98127
RTLD_LAZY = Handle::RTLD_LAZY # :nodoc:
99128
RTLD_NOW = Handle::RTLD_NOW # :nodoc:
129+
130+
Fiddle::Types.constants.each do |type|
131+
const_set "TYPE_#{type}", Fiddle::Types.const_get(type)
132+
end
100133
end

lib/ruby/stdlib/fiddle/closure.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
# frozen_string_literal: true
22
module Fiddle
33
class Closure
4+
class << self
5+
# Create a new closure. If a block is given, the created closure
6+
# is automatically freed after the given block is executed.
7+
#
8+
# The all given arguments are passed to Fiddle::Closure.new. So
9+
# using this method without block equals to Fiddle::Closure.new.
10+
#
11+
# == Example
12+
#
13+
# Fiddle::Closure.create(TYPE_INT, [TYPE_INT]) do |closure|
14+
# # closure is freed automatically when this block is finished.
15+
# end
16+
def create(*args)
17+
if block_given?
18+
closure = new(*args)
19+
begin
20+
yield(closure)
21+
ensure
22+
closure.free
23+
end
24+
else
25+
new(*args)
26+
end
27+
end
28+
end
429

530
# the C type of the return of the FFI closure
631
attr_reader :ctype

lib/ruby/stdlib/fiddle/cparser.rb

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,35 @@ def parse_ctype(ty, tymap=nil)
164164
unless Fiddle.const_defined?(:TYPE_LONG_LONG)
165165
raise(RuntimeError, "unsupported type: #{ty}")
166166
end
167-
return -TYPE_LONG_LONG
168-
when /\A(?:signed\s+)?long(?:\s+int\s+)?(?:\s+\w+)?\z/
167+
return TYPE_ULONG_LONG
168+
when /\Aunsigned\s+long(?:\s+int\s+)?(?:\s+\w+)?\z/,
169+
/\Aunsigned\s+int\s+long(?:\s+\w+)?\z/,
170+
/\Along(?:\s+int)?\s+unsigned(?:\s+\w+)?\z/,
171+
/\Aint\s+unsigned\s+long(?:\s+\w+)?\z/,
172+
/\A(?:int\s+)?long\s+unsigned(?:\s+\w+)?\z/
173+
return TYPE_ULONG
174+
when /\A(?:signed\s+)?long(?:\s+int\s+)?(?:\s+\w+)?\z/,
175+
/\A(?:signed\s+)?int\s+long(?:\s+\w+)?\z/,
176+
/\Along(?:\s+int)?\s+signed(?:\s+\w+)?\z/
169177
return TYPE_LONG
170-
when /\Aunsigned\s+long(?:\s+int\s+)?(?:\s+\w+)?\z/
171-
return -TYPE_LONG
178+
when /\Aunsigned\s+short(?:\s+int\s+)?(?:\s+\w+)?\z/,
179+
/\Aunsigned\s+int\s+short(?:\s+\w+)?\z/,
180+
/\Ashort(?:\s+int)?\s+unsigned(?:\s+\w+)?\z/,
181+
/\Aint\s+unsigned\s+short(?:\s+\w+)?\z/,
182+
/\A(?:int\s+)?short\s+unsigned(?:\s+\w+)?\z/
183+
return TYPE_USHORT
184+
when /\A(?:signed\s+)?short(?:\s+int\s+)?(?:\s+\w+)?\z/,
185+
/\A(?:signed\s+)?int\s+short(?:\s+\w+)?\z/,
186+
/\Aint\s+(?:signed\s+)?short(?:\s+\w+)?\z/
187+
return TYPE_SHORT
172188
when /\A(?:signed\s+)?int(?:\s+\w+)?\z/
173189
return TYPE_INT
174190
when /\A(?:unsigned\s+int|uint)(?:\s+\w+)?\z/
175-
return -TYPE_INT
176-
when /\A(?:signed\s+)?short(?:\s+int\s+)?(?:\s+\w+)?\z/
177-
return TYPE_SHORT
178-
when /\Aunsigned\s+short(?:\s+int\s+)?(?:\s+\w+)?\z/
179-
return -TYPE_SHORT
191+
return TYPE_UINT
180192
when /\A(?:signed\s+)?char(?:\s+\w+)?\z/
181193
return TYPE_CHAR
182194
when /\Aunsigned\s+char(?:\s+\w+)?\z/
183-
return -TYPE_CHAR
195+
return TYPE_UCHAR
184196
when /\Aint8_t(?:\s+\w+)?\z/
185197
unless Fiddle.const_defined?(:TYPE_INT8_T)
186198
raise(RuntimeError, "unsupported type: #{ty}")
@@ -190,7 +202,7 @@ def parse_ctype(ty, tymap=nil)
190202
unless Fiddle.const_defined?(:TYPE_INT8_T)
191203
raise(RuntimeError, "unsupported type: #{ty}")
192204
end
193-
return -TYPE_INT8_T
205+
return TYPE_UINT8_T
194206
when /\Aint16_t(?:\s+\w+)?\z/
195207
unless Fiddle.const_defined?(:TYPE_INT16_T)
196208
raise(RuntimeError, "unsupported type: #{ty}")
@@ -200,7 +212,7 @@ def parse_ctype(ty, tymap=nil)
200212
unless Fiddle.const_defined?(:TYPE_INT16_T)
201213
raise(RuntimeError, "unsupported type: #{ty}")
202214
end
203-
return -TYPE_INT16_T
215+
return TYPE_UINT16_T
204216
when /\Aint32_t(?:\s+\w+)?\z/
205217
unless Fiddle.const_defined?(:TYPE_INT32_T)
206218
raise(RuntimeError, "unsupported type: #{ty}")
@@ -210,7 +222,7 @@ def parse_ctype(ty, tymap=nil)
210222
unless Fiddle.const_defined?(:TYPE_INT32_T)
211223
raise(RuntimeError, "unsupported type: #{ty}")
212224
end
213-
return -TYPE_INT32_T
225+
return TYPE_UINT32_T
214226
when /\Aint64_t(?:\s+\w+)?\z/
215227
unless Fiddle.const_defined?(:TYPE_INT64_T)
216228
raise(RuntimeError, "unsupported type: #{ty}")
@@ -220,7 +232,7 @@ def parse_ctype(ty, tymap=nil)
220232
unless Fiddle.const_defined?(:TYPE_INT64_T)
221233
raise(RuntimeError, "unsupported type: #{ty}")
222234
end
223-
return -TYPE_INT64_T
235+
return TYPE_UINT64_T
224236
when /\Afloat(?:\s+\w+)?\z/
225237
return TYPE_FLOAT
226238
when /\Adouble(?:\s+\w+)?\z/
@@ -235,6 +247,8 @@ def parse_ctype(ty, tymap=nil)
235247
return TYPE_INTPTR_T
236248
when /\Auintptr_t(?:\s+\w+)?\z/
237249
return TYPE_UINTPTR_T
250+
when "bool"
251+
return TYPE_BOOL
238252
when /\*/, /\[[\s\d]*\]/
239253
return TYPE_VOIDP
240254
when "..."

lib/ruby/stdlib/fiddle/import.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def sizeof(ty)
119119
return SIZEOF_VOIDP
120120
when TYPE_CONST_STRING
121121
return SIZEOF_CONST_STRING
122+
when TYPE_BOOL
123+
return SIZEOF_BOOL
122124
else
123125
if defined?(TYPE_LONG_LONG) and
124126
ty == TYPE_LONG_LONG

lib/ruby/stdlib/fiddle/pack.rb

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,36 @@ module PackInfo # :nodoc: all
1111
TYPE_LONG => ALIGN_LONG,
1212
TYPE_FLOAT => ALIGN_FLOAT,
1313
TYPE_DOUBLE => ALIGN_DOUBLE,
14-
-TYPE_CHAR => ALIGN_CHAR,
15-
-TYPE_SHORT => ALIGN_SHORT,
16-
-TYPE_INT => ALIGN_INT,
17-
-TYPE_LONG => ALIGN_LONG,
14+
TYPE_UCHAR => ALIGN_CHAR,
15+
TYPE_USHORT => ALIGN_SHORT,
16+
TYPE_UINT => ALIGN_INT,
17+
TYPE_ULONG => ALIGN_LONG,
18+
TYPE_BOOL => ALIGN_BOOL,
1819
}
1920

2021
PACK_MAP = {
21-
TYPE_VOIDP => "l!",
22+
TYPE_VOIDP => "L!",
2223
TYPE_CHAR => "c",
2324
TYPE_SHORT => "s!",
2425
TYPE_INT => "i!",
2526
TYPE_LONG => "l!",
2627
TYPE_FLOAT => "f",
2728
TYPE_DOUBLE => "d",
28-
-TYPE_CHAR => "c",
29-
-TYPE_SHORT => "s!",
30-
-TYPE_INT => "i!",
31-
-TYPE_LONG => "l!",
29+
TYPE_UCHAR => "C",
30+
TYPE_USHORT => "S!",
31+
TYPE_UINT => "I!",
32+
TYPE_ULONG => "L!",
3233
}
34+
case SIZEOF_BOOL
35+
when SIZEOF_CHAR
36+
PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_UCHAR]
37+
when SIZEOF_SHORT
38+
PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_USHORT]
39+
when SIZEOF_INT
40+
PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_UINT]
41+
when SIZEOF_LONG
42+
PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_ULONG]
43+
end
3344

3445
SIZE_MAP = {
3546
TYPE_VOIDP => SIZEOF_VOIDP,
@@ -39,16 +50,18 @@ module PackInfo # :nodoc: all
3950
TYPE_LONG => SIZEOF_LONG,
4051
TYPE_FLOAT => SIZEOF_FLOAT,
4152
TYPE_DOUBLE => SIZEOF_DOUBLE,
42-
-TYPE_CHAR => SIZEOF_CHAR,
43-
-TYPE_SHORT => SIZEOF_SHORT,
44-
-TYPE_INT => SIZEOF_INT,
45-
-TYPE_LONG => SIZEOF_LONG,
53+
TYPE_UCHAR => SIZEOF_CHAR,
54+
TYPE_USHORT => SIZEOF_SHORT,
55+
TYPE_UINT => SIZEOF_INT,
56+
TYPE_ULONG => SIZEOF_LONG,
57+
TYPE_BOOL => SIZEOF_BOOL,
4658
}
4759
if defined?(TYPE_LONG_LONG)
48-
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_MAP[-TYPE_LONG_LONG] = ALIGN_LONG_LONG
49-
PACK_MAP[TYPE_LONG_LONG] = PACK_MAP[-TYPE_LONG_LONG] = "q"
50-
SIZE_MAP[TYPE_LONG_LONG] = SIZE_MAP[-TYPE_LONG_LONG] = SIZEOF_LONG_LONG
51-
PACK_MAP[TYPE_VOIDP] = "q" if SIZEOF_LONG_LONG == SIZEOF_VOIDP
60+
ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_MAP[TYPE_ULONG_LONG] = ALIGN_LONG_LONG
61+
PACK_MAP[TYPE_LONG_LONG] = "q"
62+
PACK_MAP[TYPE_ULONG_LONG] = "Q"
63+
SIZE_MAP[TYPE_LONG_LONG] = SIZE_MAP[TYPE_ULONG_LONG] = SIZEOF_LONG_LONG
64+
PACK_MAP[TYPE_VOIDP] = "Q" if SIZEOF_LONG_LONG == SIZEOF_VOIDP
5265
end
5366

5467
def align(addr, align)

lib/ruby/stdlib/fiddle/value.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ module ValueUtil #:nodoc: all
66
def unsigned_value(val, ty)
77
case ty.abs
88
when TYPE_CHAR
9-
[val].pack("c").unpack("C")[0]
9+
[val].pack("c").unpack1("C")
1010
when TYPE_SHORT
11-
[val].pack("s!").unpack("S!")[0]
11+
[val].pack("s!").unpack1("S!")
1212
when TYPE_INT
13-
[val].pack("i!").unpack("I!")[0]
13+
[val].pack("i!").unpack1("I!")
1414
when TYPE_LONG
15-
[val].pack("l!").unpack("L!")[0]
15+
[val].pack("l!").unpack1("L!")
1616
else
1717
if defined?(TYPE_LONG_LONG) and
1818
ty.abs == TYPE_LONG_LONG
19-
[val].pack("q").unpack("Q")[0]
19+
[val].pack("q").unpack1("Q")
2020
else
2121
val
2222
end
@@ -26,17 +26,17 @@ def unsigned_value(val, ty)
2626
def signed_value(val, ty)
2727
case ty.abs
2828
when TYPE_CHAR
29-
[val].pack("C").unpack("c")[0]
29+
[val].pack("C").unpack1("c")
3030
when TYPE_SHORT
31-
[val].pack("S!").unpack("s!")[0]
31+
[val].pack("S!").unpack1("s!")
3232
when TYPE_INT
33-
[val].pack("I!").unpack("i!")[0]
33+
[val].pack("I!").unpack1("i!")
3434
when TYPE_LONG
35-
[val].pack("L!").unpack("l!")[0]
35+
[val].pack("L!").unpack1("l!")
3636
else
3737
if defined?(TYPE_LONG_LONG) and
3838
ty.abs == TYPE_LONG_LONG
39-
[val].pack("Q").unpack("q")[0]
39+
[val].pack("Q").unpack1("q")
4040
else
4141
val
4242
end
@@ -80,11 +80,11 @@ def wrap_arg(arg, ty, funcs = [], &block)
8080
else
8181
case SIZEOF_VOIDP
8282
when SIZEOF_LONG
83-
return [arg].pack("p").unpack("l!")[0]
83+
return [arg].pack("p").unpack1("l!")
8484
else
8585
if defined?(SIZEOF_LONG_LONG) and
8686
SIZEOF_VOIDP == SIZEOF_LONG_LONG
87-
return [arg].pack("p").unpack("q")[0]
87+
return [arg].pack("p").unpack1("q")
8888
else
8989
raise(RuntimeError, "sizeof(void*)?")
9090
end
@@ -102,10 +102,8 @@ def wrap_arg(arg, ty, funcs = [], &block)
102102
return val.unpack('C*')
103103
end
104104
end
105-
return arg
106-
else
107-
return arg
108105
end
106+
return arg
109107
else
110108
if( arg.respond_to?(:to_ptr) )
111109
return arg.to_ptr.to_i

lib/ruby/stdlib/fiddle/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Fiddle
2-
VERSION = "1.1.0"
2+
VERSION = "1.1.2"
33
end

0 commit comments

Comments
 (0)