Skip to content
Open
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
20 changes: 14 additions & 6 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,24 @@ def frame_eval_core src, b, binding_location: false
[:return_value, "_return"],
]

def frame_eval src, re_raise: false, binding_location: false
def frame_eval src, re_raise: false, binding_location: false, update_last_value: false
@success_last_eval = false

b = current_frame&.eval_binding || TOPLEVEL_BINDING
frame = current_frame
b = frame&.eval_binding || TOPLEVEL_BINDING

special_local_variables current_frame do |name, var|
special_local_variables frame do |name, var|
b.local_variable_set(name, var) if /\%/ !~ name
end

# eval_binding is a dup that shares the frame's locals, so setting an
# existing `_` would overwrite the debuggee's own variable.
if frame && !(frame.binding || TOPLEVEL_BINDING).local_variable_defined?(:_)
b.local_variable_set(:_, @last_value)
end

result = frame_eval_core(src, b, binding_location: binding_location)
@last_value = result if update_last_value

@success_last_eval = true
result
Expand Down Expand Up @@ -1045,19 +1053,19 @@ def wait_next_action_

case eval_type
when :p
result = frame_eval(eval_src)
result = frame_eval(eval_src, update_last_value: true)
puts "=> " + color_pp(result, 2 ** 30)
if alloc_path = ObjectSpace.allocation_sourcefile(result)
puts "allocated at #{alloc_path}:#{ObjectSpace.allocation_sourceline(result)}"
end
when :pp
result = frame_eval(eval_src)
result = frame_eval(eval_src, update_last_value: true)
puts color_pp(result, SESSION.width)
if alloc_path = ObjectSpace.allocation_sourcefile(result)
puts "allocated at #{alloc_path}:#{ObjectSpace.allocation_sourceline(result)}"
end
when :call
result = frame_eval(eval_src)
result = frame_eval(eval_src, update_last_value: true)
when :display, :try_display
failed_results = []
eval_src.each_with_index{|src, i|
Expand Down
47 changes: 47 additions & 0 deletions test/console/debugger_local_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,53 @@ def test_locals_added_in_locals_are_accessible_between_evaluations
type "c"
end
end

def test_evaluated_result_is_set_as_underscore_local
debug_code(program) do
type "_"
assert_line_text(/nil/)
type "3 * 3"
type "foo = _"
type "9 == foo"
assert_line_text(/true/)
type "c"
end
end

def test_display_does_not_update_underscore_local
debug_code(program) do
type "display 42"
type "3 * 3"
type "display"
type "_"
assert_line_text(/9/)
type "c"
end
end
end

class UnderscoreDefinedByProgramTest < ConsoleTestCase
def program
<<~RUBY
1| _ = :mine
2| binding.b
3| p _
4| binding.b
5| nil
RUBY
end

def test_underscore_defined_by_program_is_not_clobbered
debug_code(program) do
type "c"
type "3 * 3"
type "_"
assert_line_text(/:mine/)
type "c"
assert_line_text(/:mine/)
type "c"
end
end
end

class RaisedTest < ConsoleTestCase
Expand Down
14 changes: 14 additions & 0 deletions test/console/irb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ def test_irb_console_config_activates_irb
ENV["RUBY_DEBUG_IRB_CONSOLE"] = nil
end

def test_irb_console_evaluated_result_is_set_as_underscore_local
debug_code(program, remote: false) do
type 'irb'
type "_"
assert_line_text(/nil/)
type "3 * 3"
assert_raw_line_text 'irb:rdbg(main):003> 3 * 3'
type "foo = _"
type "9 == foo"
assert_line_text(/true/)
type "c"
end
end

private

# assert_line_text ignores the prompt line, so we can't use it to assert the prompt transition
Expand Down