From 2aed2cbcf22aaaad1dac4c3b79df24c5a542cd61 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 10 Nov 2018 10:39:58 +0000 Subject: Parse the source in SCRIPT_LINES__ if possible git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ast.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'ast.c') diff --git a/ast.c b/ast.c index 4e2f05c2b7..0abe33fd9e 100644 --- a/ast.c +++ b/ast.c @@ -167,10 +167,24 @@ node_find(VALUE self, const int node_id) return Qnil; } +static VALUE +script_lines(VALUE path) +{ + VALUE hash, lines; + ID script_lines; + CONST_ID(script_lines, "SCRIPT_LINES__"); + if (!rb_const_defined_at(rb_cObject, script_lines)) return Qnil; + hash = rb_const_get_at(rb_cObject, script_lines); + if (!RB_TYPE_P(hash, T_HASH)) return Qnil; + lines = rb_hash_lookup(hash, path); + if (!RB_TYPE_P(lines, T_ARRAY)) return Qnil; + return lines; +} + static VALUE rb_ast_s_of(VALUE module, VALUE body) { - VALUE path, node; + VALUE path, node, lines; int node_id; const rb_iseq_t *iseq = NULL; @@ -189,7 +203,12 @@ rb_ast_s_of(VALUE module, VALUE body) path = rb_iseq_path(iseq); node_id = iseq->body->location.node_id; - node = rb_ast_parse_file(path); + if (!NIL_P(lines = script_lines(path))) { + node = rb_ast_parse_str(rb_ary_join(lines, Qnil)); + } + else { + node = rb_ast_parse_file(path); + } return node_find(node, node_id); } -- cgit v1.2.3