Skip to content

Commit

Permalink
Move input line mutation out of Context#evaluate
Browse files Browse the repository at this point in the history
This makes sure `Context#evaluate` really just evaluates the input.
It will also make #575's implementation cleaner.
  • Loading branch information
st0012 committed Jun 27, 2023
1 parent e68c612 commit 929c45d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
23 changes: 20 additions & 3 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,11 @@ def eval_input
if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty?
IRB.set_measure_callback
end
# Assignment expression check should be done before @context.evaluate to handle code like `a /2#/ if false; a = 1`
# Assignment expression check should be done before evaluate_line to handle code like `a /2#/ if false; a = 1`
is_assignment = assignment_expression?(line)
if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty?
result = nil
last_proc = proc{ result = @context.evaluate(line, line_no, exception: exc) }
last_proc = proc{ result = evaluate_line(line, line_no, exception: exc) }
IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) { |chain, item|
_name, callback, arg = item
proc {
Expand All @@ -578,7 +578,7 @@ def eval_input
}.call
@context.set_last_value(result)
else
@context.evaluate(line, line_no, exception: exc)
evaluate_line(line, line_no, exception: exc)
end
if @context.echo?
if is_assignment
Expand All @@ -604,6 +604,23 @@ def eval_input
end
end

def evaluate_line(line, line_no, exception: nil)
# Transform a non-identifier alias (@, $) or keywords (next, break)
command, args = line.split(/\s/, 2)
if original = @context.command_aliases[command.to_sym]
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
command = original
end

# Hook command-specific transformation
command_class = ExtendCommandBundle.load_command(command)
if command_class&.respond_to?(:transform_args)
line = "#{command} #{command_class.transform_args(args)}"
end

@context.evaluate(line, line_no, exception: exception)
end

def convert_invalid_byte_sequence(str, enc)
str.force_encoding(enc)
str.scrub { |c|
Expand Down
14 changes: 1 addition & 13 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,13 @@ def inspect_mode=(opt)

def evaluate(line, line_no, exception: nil) # :nodoc:
@line_no = line_no

if exception
line_no -= 1
line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
@workspace.local_variable_set(:_, exception)
end

# Transform a non-identifier alias (@, $) or keywords (next, break)
command, args = line.split(/\s/, 2)
if original = command_aliases[command.to_sym]
line = line.gsub(/\A#{Regexp.escape(command)}/, original.to_s)
command = original
end

# Hook command-specific transformation
command_class = ExtendCommandBundle.load_command(command)
if command_class&.respond_to?(:transform_args)
line = "#{command} #{command_class.transform_args(args)}"
end

set_last_value(@workspace.evaluate(line, irb_path, line_no))
end

Expand Down

0 comments on commit 929c45d

Please sign in to comment.