Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions core/binding.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@ class Binding
#
def eval: (string src, ?string filename, ?int lineno) -> untyped

# <!--
# rdoc-file=proc.c
# - binding.implicit_parameter_defined?(symbol) -> obj
# -->
# Returns `true` if the numbered parameter or "it" parameter exists.
#
# def foo
# [42].each do
# it
# binding.implicit_parameter_defined?(:it) #=> true
# binding.implicit_parameter_defined?(:_1) #=> false
# end
#
# { k: 42 }.each do
# _2
# binding.implicit_parameter_defined?(:_1) #=> true
# binding.implicit_parameter_defined?(:_2) #=> true
# binding.implicit_parameter_defined?(:_3) #=> false
# binding.implicit_parameter_defined?(:it) #=> false
# end
# end
#
def implicit_parameter_defined?: (interned symbol) -> bool

# <!--
# rdoc-file=proc.c
# - binding.implicit_parameter_get(symbol) -> obj
# -->
# Returns the value of the numbered parameter or "it" parameter.
#
# def foo
# [42].each do
# it
# binding.implicit_parameter_get(:it) #=> 42
# end
#
# { k: 42 }.each do
# _2
# binding.implicit_parameter_get(:_1) #=> :k
# binding.implicit_parameter_get(:_2) #=> 42
# end
# end
#
def implicit_parameter_get: (interned symbol) -> untyped

# <!--
# rdoc-file=proc.c
# - binding.implicit_parameters -> Array
# -->
# Returns the names of numbered parameters and "it" parameter that are defined
# in the binding.
#
# def foo
# [42].each do
# it
# binding.implicit_parameters #=> [:it]
# end
#
# { k: 42 }.each do
# _2
# binding.implicit_parameters #=> [:_1, :_2]
# end
# end
#
def implicit_parameters: () -> Array[Symbol]

# <!--
# rdoc-file=proc.c
# - binding.local_variable_defined?(symbol) -> obj
Expand Down
22 changes: 22 additions & 0 deletions core/io.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,17 @@ class IO < Object
#
def eof: () -> bool

# <!--
# rdoc-file=io.c
# - external_encoding -> encoding or nil
# -->
# Returns the Encoding object that represents the encoding of the stream, or
# `nil` if the stream is in write mode and no encoding is specified.
#
# See [Encodings](rdoc-ref:File@Encodings).
#
def external_encoding: () -> Encoding?

# <!--
# rdoc-file=io.c
# - fcntl(integer_cmd, argument) -> integer
Expand Down Expand Up @@ -2176,6 +2187,17 @@ class IO < Object
#
def to_io: () -> self

# <!-- rdoc-file=io.c -->
# Returns the path associated with the IO, or `nil` if there is no path
# associated with the IO. It is not guaranteed that the path exists on the
# filesystem.
#
# $stdin.path # => "<STDIN>"
#
# File.open("testfile") {|f| f.path} # => "testfile"
#
alias to_path path

# <!-- rdoc-file=io.c -->
# Returns `true` if the stream is associated with a terminal device (tty),
# `false` otherwise:
Expand Down
11 changes: 11 additions & 0 deletions core/io/wait.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ class IO

type wait_mode = :read | :r | :readable | :write | :w | :writable | :read_write | :rw | :readable_writable

# <!--
# rdoc-file=io.c
# - io.wait_priority -> truthy or falsy
# - io.wait_priority(timeout) -> truthy or falsy
# -->
# Waits until IO is priority and returns a truthy value or a falsy value when
# times out. Priority data is sent and received using the Socket::MSG_OOB flag
# and is typically limited to streams.
#
def wait_priority: (?Time::_Timeout? timeout) -> boolish

# <!--
# rdoc-file=io.c
# - io.wait_readable -> truthy or falsy
Expand Down
19 changes: 19 additions & 0 deletions core/process.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,25 @@ end
# Placeholder for rusage
#
class Process::Tms < Struct[Float]
# <!-- rdoc-file=process.c -->
# user time used in this process
#
attr_accessor utime: Float

# <!-- rdoc-file=process.c -->
# system time used in this process
#
attr_accessor stime: Float

# <!-- rdoc-file=process.c -->
# user time used in the child processes
#
attr_accessor cutime: Float

# <!-- rdoc-file=process.c -->
# system time used in the child processes
#
attr_accessor cstime: Float
end

class Process::Waiter < Thread
Expand Down
13 changes: 13 additions & 0 deletions core/thread.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,19 @@ class Thread < Object
# check $SAFE.
def safe_level: () -> Integer

# <!--
# rdoc-file=vm_trace.c
# - thr.set_trace_func(proc) -> proc
# - thr.set_trace_func(nil) -> nil
# -->
# Establishes *proc* on *thr* as the handler for tracing, or disables tracing if
# the parameter is `nil`.
#
# See Kernel#set_trace_func.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding Kernel#set_trace_func as well?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 #3221

#
def set_trace_func: (Proc proc) -> Proc
| (nil) -> nil

# <!--
# rdoc-file=thread.c
# - thr.status -> string, false or nil
Expand Down
43 changes: 43 additions & 0 deletions test/stdlib/Binding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ def test_eval
end
end

def test_implicit_parameter_defined?
[1].each do
it
b = binding

with_interned :it do |symbol|
assert_send_type '(interned) -> bool',
b, :implicit_parameter_defined?, symbol
end
assert_send_type '(Symbol) -> bool',
b, :implicit_parameter_defined?, :_1
end
end

def test_implicit_parameter_get
[1].each do
_1
b = binding

with_interned :_1 do |symbol|
assert_send_type '(interned) -> untyped',
b, :implicit_parameter_get, symbol
end
end
end

def test_implicit_parameters
assert_send_type '() -> Array[Symbol]',
binding, :implicit_parameters

[1].each do
it
assert_send_type '() -> Array[Symbol]',
binding, :implicit_parameters
end

[1].each do
_1
assert_send_type '() -> Array[Symbol]',
binding, :implicit_parameters
end
end

def test_local_variable_defined?
with_interned :hello do |varname|
assert_send_type '(interned) -> bool',
Expand Down
47 changes: 47 additions & 0 deletions test/stdlib/IO_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ def test_each_line
end
end

def test_external_encoding
File.open(File.expand_path(__FILE__)) do |io|
assert_send_type(
"() -> Encoding",
io, :external_encoding
)
end

File.open(IO::NULL, "w") do |io|
assert_send_type(
"() -> nil",
io, :external_encoding
)
end
end

def test_path
IO.open(IO.sysopen(File.expand_path(__FILE__)), path: "foo") do |io|
assert_send_type(
Expand All @@ -327,6 +343,22 @@ def test_path
end
end

def test_to_path
IO.open(IO.sysopen(File.expand_path(__FILE__)), path: "foo") do |io|
assert_send_type(
"() -> String",
io, :to_path
)
end

IO.open(IO.sysopen(File.expand_path(__FILE__)), path: nil) do |io|
assert_send_type(
"() -> nil",
io, :to_path
)
end
end

def test_read
IO.open(IO.sysopen(File.expand_path(__FILE__))) do |io|
assert_send_type "() -> String",
Expand Down Expand Up @@ -593,6 +625,21 @@ def test_wait_readable
end
end

def test_wait_priority
IO.pipe do |r, w|
assert_send_type(
"(Integer) -> nil",
r, :wait_priority, 0
)

r.ungetc("a")
assert_send_type(
"() -> true",
r, :wait_priority
)
end
end

def test_wait_writable
if_ruby "3.0.0"..."3.2.0" do
IO.pipe.tap do |r, w|
Expand Down
39 changes: 39 additions & 0 deletions test/stdlib/Process_Tms_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative "test_helper"

class Process_TmsInstanceTest < Test::Unit::TestCase
include TestHelper

testing "::Process::Tms"

def test_utime
assert_send_type "() -> Float", Process.times, :utime
end

def test_utime=
assert_send_type "(Float) -> Float", Process.times, :utime=, 1.0
end

def test_stime
assert_send_type "() -> Float", Process.times, :stime
end

def test_stime=
assert_send_type "(Float) -> Float", Process.times, :stime=, 1.0
end

def test_cutime
assert_send_type "() -> Float", Process.times, :cutime
end

def test_cutime=
assert_send_type "(Float) -> Float", Process.times, :cutime=, 1.0
end

def test_cstime
assert_send_type "() -> Float", Process.times, :cstime
end

def test_cstime=
assert_send_type "(Float) -> Float", Process.times, :cstime=, 1.0
end
end
11 changes: 11 additions & 0 deletions test/stdlib/Thread_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,15 @@ def test_run
def test_wakeup
assert_send_type "() -> ThreadTest::Sub", Sub.new{}, :wakeup
end

def test_set_trace_func
th = Thread.new { sleep }
trace = proc { }

assert_send_type "(Proc) -> Proc", th, :set_trace_func, trace
assert_send_type "(nil) -> nil", th, :set_trace_func, nil
ensure
th&.set_trace_func(nil)
th&.kill&.join
end
end
Loading