From acac2b8128980b97c64b4d057acdf2ceffb0b981 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Sun, 19 Dec 2021 03:40:44 +0900 Subject: Make RubyVM::AbstractSyntaxTree.of raise for backtrace location in eval This check is needed to fix a bug of error_highlight when NameError occurred in eval'ed code. https://github.com/ruby/error_highlight/pull/16 The same check for proc/method has been already introduced since 64ac984129a7a4645efe5ac57c168ef880b479b2. --- ast.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'ast.c') diff --git a/ast.c b/ast.c index 05bfc755a3..e180213ef7 100644 --- a/ast.c +++ b/ast.c @@ -196,14 +196,15 @@ static VALUE ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script_lines) { VALUE path, node, lines = Qnil; + const rb_iseq_t *iseq; int node_id; if (rb_frame_info_p(body)) { - rb_frame_info_get(body, &path, &lines, &node_id); - if (NIL_P(path) && NIL_P(lines)) return Qnil; + iseq = rb_get_iseq_from_frame_info(body); + node_id = rb_get_node_id_from_frame_info(body); } else { - const rb_iseq_t *iseq = NULL; + iseq = NULL; if (rb_obj_is_proc(body)) { iseq = vm_proc_iseq(body); @@ -213,17 +214,20 @@ ast_s_of(rb_execution_context_t *ec, VALUE module, VALUE body, VALUE keep_script else { iseq = rb_method_iseq(body); } - if (!iseq) { - return Qnil; + if (iseq) { + node_id = iseq->body->location.node_id; } - if (rb_iseq_from_eval_p(iseq)) { - rb_raise(rb_eArgError, "cannot get AST for method defined in eval"); - } - path = rb_iseq_path(iseq); - lines = iseq->body->variable.script_lines; - node_id = iseq->body->location.node_id; } + if (!iseq) { + return Qnil; + } + if (rb_iseq_from_eval_p(iseq)) { + rb_raise(rb_eArgError, "cannot get AST for method defined in eval"); + } + path = rb_iseq_path(iseq); + lines = iseq->body->variable.script_lines; + if (!NIL_P(lines) || !NIL_P(lines = script_lines(path))) { node = rb_ast_parse_array(lines, keep_script_lines); } -- cgit v1.2.3