[前][次][番号順一覧][スレッド一覧]

ruby-changes:70601

From: Yusuke <ko1@a...>
Date: Sun, 26 Dec 2021 20:57:49 +0900 (JST)
Subject: [ruby-changes:70601] 0dc7816c43 (master): Make RubyVM::AST.of work with code written in `-e` command-line option

https://git.ruby-lang.org/ruby.git/commit/?id=0dc7816c43

From 0dc7816c4350683ccd020f5759eee4914de0085d Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Sun, 26 Dec 2021 19:41:42 +0900
Subject: Make RubyVM::AST.of work with code written in `-e` command-line
 option

[Bug #18434]
---
 ast.c                 | 11 +++++++----
 test/ruby/test_ast.rb |  5 +++++
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/ast.c b/ast.c
index 466e7a6a2e5..0515689a299 100644
--- a/ast.c
+++ b/ast.c
@@ -195,7 +195,7 @@ script_lines(VALUE path) https://github.com/ruby/ruby/blob/trunk/ast.c#L195
 static VALUE
 ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines)
 {
-    VALUE path, node, lines = Qnil;
+    VALUE node, lines = Qnil;
     const rb_iseq_t *iseq;
     int node_id;
 
@@ -223,15 +223,18 @@ ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script https://github.com/ruby/ruby/blob/trunk/ast.c#L223
         return Qnil;
     }
     lines = iseq->body->variable.script_lines;
-    if (NIL_P(lines) && rb_iseq_from_eval_p(iseq)) {
+
+    VALUE path = rb_iseq_path(iseq);
+    int e_option = RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0;
+
+    if (NIL_P(lines) && rb_iseq_from_eval_p(iseq) && !e_option) {
         rb_raise(rb_eArgError, "cannot get AST for method defined in eval");
     }
-    path = rb_iseq_path(iseq);
 
     if (!NIL_P(lines) || !NIL_P(lines = script_lines(path))) {
         node = rb_ast_parse_array(lines, keep_script_lines);
     }
-    else if (RSTRING_LEN(path) == 2 && memcmp(RSTRING_PTR(path), "-e", 2) == 0) {
+    else if (e_option) {
         node = rb_ast_parse_str(rb_e_script, keep_script_lines);
     }
     else {
diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb
index ce0942a6fb2..a4edfd3cbeb 100644
--- a/test/ruby/test_ast.rb
+++ b/test/ruby/test_ast.rb
@@ -537,4 +537,9 @@ dummy https://github.com/ruby/ruby/blob/trunk/test/ruby/test_ast.rb#L537
     assert_equal("{ 1 + 2 }", node_proc.source)
     assert_equal("def test_keep_script_lines_for_of\n", node_method.source.lines.first)
   end
+
+  def test_e_option
+    assert_in_out_err(["-e", "def foo; end; pp RubyVM::AbstractSyntaxTree.of(method(:foo)).type"],
+                      "", [":SCOPE"], [])
+  end
 end
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]