Skip to content

Commit

Permalink
Restore MAIN_CONTEXT correctly (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Apr 30, 2024
1 parent a5a2337 commit c41f460
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ def debug_readline(binding)
def run(conf = IRB.conf)
in_nested_session = !!conf[:MAIN_CONTEXT]
conf[:IRB_RC].call(context) if conf[:IRB_RC]
prev_context = conf[:MAIN_CONTEXT]
conf[:MAIN_CONTEXT] = context

save_history = !in_nested_session && conf[:SAVE_HISTORY] && context.io.support_history_saving?
Expand All @@ -1014,6 +1015,9 @@ def run(conf = IRB.conf)
eval_input
end
ensure
# Do not restore to nil. It will cause IRB crash when used with threads.
IRB.conf[:MAIN_CONTEXT] = prev_context if prev_context

RubyVM.keep_script_lines = keep_script_lines_backup if defined?(RubyVM.keep_script_lines)
trap("SIGINT", prev_trap)
conf[:AT_EXIT].each{|hook| hook.call}
Expand Down
20 changes: 20 additions & 0 deletions test/irb/test_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ def test_empty_input_echoing_behaviour
end
end

class NestedBindingIrbTest < IntegrationTestCase
def test_current_context_restore
write_ruby <<~'RUBY'
binding.irb
RUBY

output = run_ruby_file do
type '$ctx = IRB.CurrentContext'
type 'binding.irb'
type 'p context_changed: IRB.CurrentContext != $ctx'
type 'exit'
type 'p context_restored: IRB.CurrentContext == $ctx'
type 'exit'
end

assert_include output, '{:context_changed=>true}'
assert_include output, '{:context_restored=>true}'
end
end

class IrbIOConfigurationTest < TestCase
Row = Struct.new(:content, :current_line_spaces, :new_line_spaces, :indent_level)

Expand Down

0 comments on commit c41f460

Please sign in to comment.