summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2021-09-30 16:58:46 +0900
committerKoichi Sasada <ko1@atdot.net>2021-10-21 16:17:39 +0900
commitc7550537f11dcf6450a9d3df3af3fa1f4fe05b15 (patch)
tree9b73c5b1be4e42f0aaf92e64b9c6041777666251 /test
parent3b16d07e457264d7c171f8d1fcfaddb0dad90f57 (diff)
`RubyVM.keep_script_lines`
`RubyVM.keep_script_lines` enables to keep script lines for each ISeq and AST. This feature is for debugger/REPL support. ```ruby RubyVM.keep_script_lines = true RubyVM::keep_script_lines = true eval("def foo = nil\ndef bar = nil") pp RubyVM::InstructionSequence.of(method(:foo)).script_lines ```
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4913
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_rubyvm.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/ruby/test_rubyvm.rb b/test/ruby/test_rubyvm.rb
index 67d46e27ad..fecaacfa78 100644
--- a/test/ruby/test_rubyvm.rb
+++ b/test/ruby/test_rubyvm.rb
@@ -15,4 +15,56 @@ class TestRubyVM < Test::Unit::TestCase
assert_raise(ArgumentError){ RubyVM.stat(:unknown) }
assert_raise_with_message(ArgumentError, /\u{30eb 30d3 30fc}/) {RubyVM.stat(:"\u{30eb 30d3 30fc}")}
end
+
+ def parse_and_compile
+ script = <<~RUBY
+ a = 1
+ def foo
+ b = 2
+ end
+ 1.times{
+ c = 3
+ }
+ RUBY
+
+ ast = RubyVM::AbstractSyntaxTree.parse(script)
+ iseq = RubyVM::InstructionSequence.compile(script)
+
+ [ast, iseq]
+ end
+
+ def test_keep_script_lines
+ prev_conf = RubyVM.keep_script_lines
+
+ # keep
+ RubyVM.keep_script_lines = true
+
+ ast, iseq = *parse_and_compile
+
+ lines = ast.script_lines
+ assert_equal Array, lines.class
+
+ lines = iseq.script_lines
+ assert_equal Array, lines.class
+ iseq.each_child{|child|
+ assert_equal lines, child.script_lines
+ }
+
+ # don't keep
+ RubyVM.keep_script_lines = false
+
+ ast, iseq = *parse_and_compile
+
+ lines = ast.script_lines
+ assert_equal nil, lines
+
+ lines = iseq.script_lines
+ assert_equal nil, lines
+ iseq.each_child{|child|
+ assert_equal lines, child.script_lines
+ }
+
+ ensure
+ RubyVM.keep_script_lines = prev_conf
+ end
end