Skip to content

Commit a287393

Browse files
committed
Merge branch 'master' into mmtk-with-master
2 parents aa3bcc5 + d75dc39 commit a287393

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+685
-395
lines changed

Diff for: lib/ipaddr.rb

+14
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,20 @@ def netmask
471471
_to_string(@mask_addr)
472472
end
473473

474+
# Returns the wildcard mask in string format e.g. 0.0.255.255
475+
def wildcard_mask
476+
case @family
477+
when Socket::AF_INET
478+
mask = IN4MASK ^ @mask_addr
479+
when Socket::AF_INET6
480+
mask = IN6MASK ^ @mask_addr
481+
else
482+
raise AddressFamilyError, "unsupported address family"
483+
end
484+
485+
_to_string(mask)
486+
end
487+
474488
# Returns the IPv6 zone identifier, if present.
475489
# Raises InvalidAddressError if not an IPv6 address.
476490
def zone_id

Diff for: lib/irb.rb

+37-21
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ class Irb
929929
# Creates a new irb session
930930
def initialize(workspace = nil, input_method = nil)
931931
@context = Context.new(self, workspace, input_method)
932-
@context.workspace.load_commands_to_main
932+
@context.workspace.load_helper_methods_to_main
933933
@signal_status = :IN_IRB
934934
@scanner = RubyLex.new
935935
@line_no = 1
@@ -950,7 +950,7 @@ def debug_break
950950
def debug_readline(binding)
951951
workspace = IRB::WorkSpace.new(binding)
952952
context.replace_workspace(workspace)
953-
context.workspace.load_commands_to_main
953+
context.workspace.load_helper_methods_to_main
954954
@line_no += 1
955955

956956
# When users run:
@@ -1028,7 +1028,15 @@ def eval_input
10281028
return statement.code
10291029
end
10301030

1031-
@context.evaluate(statement.evaluable_code, line_no)
1031+
case statement
1032+
when Statement::EmptyInput
1033+
# Do nothing
1034+
when Statement::Expression
1035+
@context.evaluate(statement.code, line_no)
1036+
when Statement::Command
1037+
ret = statement.command_class.execute(@context, statement.arg)
1038+
@context.set_last_value(ret)
1039+
end
10321040

10331041
if @context.echo? && !statement.suppresses_echo?
10341042
if statement.is_assignment?
@@ -1084,10 +1092,7 @@ def readmultiline
10841092
end
10851093

10861094
code << line
1087-
1088-
# Accept any single-line input for symbol aliases or commands that transform
1089-
# args
1090-
return code if single_line_command?(code)
1095+
return code if command?(code)
10911096

10921097
tokens, opens, terminated = @scanner.check_code_state(code, local_variables: @context.local_variables)
10931098
return code if terminated
@@ -1114,23 +1119,36 @@ def build_statement(code)
11141119
end
11151120

11161121
code.force_encoding(@context.io.encoding)
1117-
command_or_alias, arg = code.split(/\s/, 2)
1118-
# Transform a non-identifier alias (@, $) or keywords (next, break)
1119-
command_name = @context.command_aliases[command_or_alias.to_sym]
1120-
command = command_name || command_or_alias
1121-
command_class = ExtendCommandBundle.load_command(command)
1122-
1123-
if command_class
1124-
Statement::Command.new(code, command, arg, command_class)
1122+
if (command, arg = parse_command(code))
1123+
command_class = ExtendCommandBundle.load_command(command)
1124+
Statement::Command.new(code, command_class, arg)
11251125
else
11261126
is_assignment_expression = @scanner.assignment_expression?(code, local_variables: @context.local_variables)
11271127
Statement::Expression.new(code, is_assignment_expression)
11281128
end
11291129
end
11301130

1131-
def single_line_command?(code)
1132-
command = code.split(/\s/, 2).first
1133-
@context.symbol_alias?(command) || @context.transform_args?(command)
1131+
def parse_command(code)
1132+
command_name, arg = code.strip.split(/\s+/, 2)
1133+
return unless code.lines.size == 1 && command_name
1134+
1135+
arg ||= ''
1136+
command = command_name.to_sym
1137+
# Command aliases are always command. example: $, @
1138+
if (alias_name = @context.command_aliases[command])
1139+
return [alias_name, arg]
1140+
end
1141+
1142+
# Check visibility
1143+
public_method = !!Kernel.instance_method(:public_method).bind_call(@context.main, command) rescue false
1144+
private_method = !public_method && !!Kernel.instance_method(:method).bind_call(@context.main, command) rescue false
1145+
if ExtendCommandBundle.execute_as_command?(command, public_method: public_method, private_method: private_method)
1146+
[command, arg]
1147+
end
1148+
end
1149+
1150+
def command?(code)
1151+
!!parse_command(code)
11341152
end
11351153

11361154
def configure_io
@@ -1148,9 +1166,7 @@ def configure_io
11481166
false
11491167
end
11501168
else
1151-
# Accept any single-line input for symbol aliases or commands that transform
1152-
# args
1153-
next true if single_line_command?(code)
1169+
next true if command?(code)
11541170

11551171
_tokens, _opens, terminated = @scanner.check_code_state(code, local_variables: @context.local_variables)
11561172
terminated

Diff for: lib/irb/command.rb

+31-87
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,17 @@ module Command; end
1212

1313
# Installs the default irb extensions command bundle.
1414
module ExtendCommandBundle
15-
EXCB = ExtendCommandBundle # :nodoc:
16-
17-
# See #install_alias_method.
15+
# See ExtendCommandBundle.execute_as_command?.
1816
NO_OVERRIDE = 0
19-
# See #install_alias_method.
2017
OVERRIDE_PRIVATE_ONLY = 0x01
21-
# See #install_alias_method.
2218
OVERRIDE_ALL = 0x02
2319

24-
# Displays current configuration.
25-
#
26-
# Modifying the configuration is achieved by sending a message to IRB.conf.
27-
def irb_context
28-
IRB.CurrentContext
29-
end
30-
31-
@ALIASES = [
32-
[:context, :irb_context, NO_OVERRIDE],
33-
[:conf, :irb_context, NO_OVERRIDE],
34-
]
35-
36-
3720
@EXTEND_COMMANDS = [
21+
[
22+
:irb_context, :Context, "command/context",
23+
[:context, NO_OVERRIDE],
24+
[:conf, NO_OVERRIDE],
25+
],
3826
[
3927
:irb_exit, :Exit, "command/exit",
4028
[:exit, OVERRIDE_PRIVATE_ONLY],
@@ -204,6 +192,26 @@ def irb_context
204192
],
205193
]
206194

195+
def self.command_override_policies
196+
@@command_override_policies ||= @EXTEND_COMMANDS.flat_map do |cmd_name, cmd_class, load_file, *aliases|
197+
[[cmd_name, OVERRIDE_ALL]] + aliases
198+
end.to_h
199+
end
200+
201+
def self.execute_as_command?(name, public_method:, private_method:)
202+
case command_override_policies[name]
203+
when OVERRIDE_ALL
204+
true
205+
when OVERRIDE_PRIVATE_ONLY
206+
!public_method
207+
when NO_OVERRIDE
208+
!public_method && !private_method
209+
end
210+
end
211+
212+
def self.command_names
213+
command_override_policies.keys.map(&:to_s)
214+
end
207215

208216
@@commands = []
209217

@@ -247,77 +255,13 @@ def self.load_command(command)
247255
nil
248256
end
249257

250-
# Installs the default irb commands.
251-
def self.install_extend_commands
252-
for args in @EXTEND_COMMANDS
253-
def_extend_command(*args)
254-
end
255-
end
256-
257-
# Evaluate the given +cmd_name+ on the given +cmd_class+ Class.
258-
#
259-
# Will also define any given +aliases+ for the method.
260-
#
261-
# The optional +load_file+ parameter will be required within the method
262-
# definition.
263258
def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
264-
case cmd_class
265-
when Symbol
266-
cmd_class = cmd_class.id2name
267-
when String
268-
when Class
269-
cmd_class = cmd_class.name
270-
end
271-
272-
line = __LINE__; eval %[
273-
def #{cmd_name}(*opts, **kwargs, &b)
274-
Kernel.require_relative "#{load_file}"
275-
::IRB::Command::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
276-
end
277-
], nil, __FILE__, line
278-
279-
for ali, flag in aliases
280-
@ALIASES.push [ali, cmd_name, flag]
281-
end
282-
end
283-
284-
# Installs alias methods for the default irb commands, see
285-
# ::install_extend_commands.
286-
def install_alias_method(to, from, override = NO_OVERRIDE)
287-
to = to.id2name unless to.kind_of?(String)
288-
from = from.id2name unless from.kind_of?(String)
259+
@EXTEND_COMMANDS.delete_if { |name,| name == cmd_name }
260+
@EXTEND_COMMANDS << [cmd_name, cmd_class, load_file, *aliases]
289261

290-
if override == OVERRIDE_ALL or
291-
(override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
292-
(override == NO_OVERRIDE) && !respond_to?(to, true)
293-
target = self
294-
(class << self; self; end).instance_eval{
295-
if target.respond_to?(to, true) &&
296-
!target.respond_to?(EXCB.irb_original_method_name(to), true)
297-
alias_method(EXCB.irb_original_method_name(to), to)
298-
end
299-
alias_method to, from
300-
}
301-
else
302-
Kernel.warn "irb: warn: can't alias #{to} from #{from}.\n"
303-
end
304-
end
305-
306-
def self.irb_original_method_name(method_name) # :nodoc:
307-
"irb_" + method_name + "_org"
308-
end
309-
310-
# Installs alias methods for the default irb commands on the given object
311-
# using #install_alias_method.
312-
def self.extend_object(obj)
313-
unless (class << obj; ancestors; end).include?(EXCB)
314-
super
315-
for ali, com, flg in @ALIASES
316-
obj.install_alias_method(ali, com, flg)
317-
end
318-
end
262+
# Just clear memoized values
263+
@@commands = []
264+
@@command_override_policies = nil
319265
end
320-
321-
install_extend_commands
322266
end
323267
end

Diff for: lib/irb/command/backtrace.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ module IRB
77

88
module Command
99
class Backtrace < DebugCommand
10-
def self.transform_args(args)
11-
args&.dump
12-
end
13-
14-
def execute(*args)
15-
super(pre_cmds: ["backtrace", *args].join(" "))
10+
def execute(arg)
11+
execute_debug_command(pre_cmds: "backtrace #{arg}")
1612
end
1713
end
1814
end

Diff for: lib/irb/command/base.rb

+26-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ module IRB
1010
module Command
1111
class CommandArgumentError < StandardError; end
1212

13+
def self.extract_ruby_args(*args, **kwargs)
14+
throw :EXTRACT_RUBY_ARGS, [args, kwargs]
15+
end
16+
1317
class Base
1418
class << self
1519
def category(category = nil)
@@ -29,19 +33,13 @@ def help_message(help_message = nil)
2933

3034
private
3135

32-
def string_literal?(args)
33-
sexp = Ripper.sexp(args)
34-
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
35-
end
36-
3736
def highlight(text)
3837
Color.colorize(text, [:BOLD, :BLUE])
3938
end
4039
end
4140

42-
def self.execute(irb_context, *opts, **kwargs, &block)
43-
command = new(irb_context)
44-
command.execute(*opts, **kwargs, &block)
41+
def self.execute(irb_context, arg)
42+
new(irb_context).execute(arg)
4543
rescue CommandArgumentError => e
4644
puts e.message
4745
end
@@ -52,7 +50,26 @@ def initialize(irb_context)
5250

5351
attr_reader :irb_context
5452

55-
def execute(*opts)
53+
def unwrap_string_literal(str)
54+
return if str.empty?
55+
56+
sexp = Ripper.sexp(str)
57+
if sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
58+
@irb_context.workspace.binding.eval(str).to_s
59+
else
60+
str
61+
end
62+
end
63+
64+
def ruby_args(arg)
65+
# Use throw and catch to handle arg that includes `;`
66+
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
67+
catch(:EXTRACT_RUBY_ARGS) do
68+
@irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
69+
end || [[], {}]
70+
end
71+
72+
def execute(arg)
5673
#nop
5774
end
5875
end

Diff for: lib/irb/command/break.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ module IRB
77

88
module Command
99
class Break < DebugCommand
10-
def self.transform_args(args)
11-
args&.dump
12-
end
13-
14-
def execute(args = nil)
15-
super(pre_cmds: "break #{args}")
10+
def execute(arg)
11+
execute_debug_command(pre_cmds: "break #{arg}")
1612
end
1713
end
1814
end

Diff for: lib/irb/command/catch.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ module IRB
77

88
module Command
99
class Catch < DebugCommand
10-
def self.transform_args(args)
11-
args&.dump
12-
end
13-
14-
def execute(*args)
15-
super(pre_cmds: ["catch", *args].join(" "))
10+
def execute(arg)
11+
execute_debug_command(pre_cmds: "catch #{arg}")
1612
end
1713
end
1814
end

0 commit comments

Comments
 (0)