summaryrefslogtreecommitdiff
path: root/ast.c
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2021-12-26 19:41:42 +0900
committerYusuke Endoh <mame@ruby-lang.org>2021-12-26 20:57:34 +0900
commit0dc7816c4350683ccd020f5759eee4914de0085d (patch)
tree614d8e996e15667637a1477a7d3b8d1a1ded54eb /ast.c
parent18da9359de08748fe83e7a6288110c847666ae25 (diff)
downloadruby-0dc7816c4350683ccd020f5759eee4914de0085d.tar.gz
Make RubyVM::AST.of work with code written in `-e` command-line option
[Bug #18434]
Diffstat (limited to 'ast.c')
-rw-r--r--ast.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/ast.c b/ast.c
index 466e7a6a2e..0515689a29 100644
--- a/ast.c
+++ b/ast.c
@@ -195,7 +195,7 @@ script_lines(VALUE path)
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
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 {