1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Get rid of setting SCRIPT_LINES__ by AST.parse

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65647 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-11-09 13:39:36 +00:00
parent 49c7c8ed85
commit 4bf84ede20
3 changed files with 33 additions and 6 deletions

4
ast.c
View file

@ -83,7 +83,7 @@ rb_ast_parse_str(VALUE str)
str = rb_check_string_type(str);
rb_parser_set_context(parser, NULL, 0);
ast = rb_parser_compile_string_path(parser, rb_str_new_cstr("no file name"), str, 1);
ast = rb_parser_compile_string_path(parser, Qnil, str, 1);
if (!ast->body.root) {
rb_ast_dispose(ast);
@ -127,7 +127,7 @@ rb_ast_parse_file(VALUE path)
f = rb_file_open_str(path, "r");
rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
rb_parser_set_context(parser, NULL, 0);
ast = rb_parser_compile_file_path(parser, path, f, 1);
ast = rb_parser_compile_file_path(parser, Qnil, f, 1);
rb_io_close(f);

17
parse.y
View file

@ -4873,7 +4873,7 @@ yycompile0(VALUE arg)
struct parser_params *p = (struct parser_params *)arg;
VALUE cov = Qfalse;
if (!compile_for_eval && rb_safe_level() == 0) {
if (!compile_for_eval && rb_safe_level() == 0 && !NIL_P(p->ruby_sourcefile_string)) {
p->debug_lines = debug_lines(p->ruby_sourcefile_string);
if (p->debug_lines && p->ruby_sourceline > 0) {
VALUE str = STR_NEW0();
@ -4933,8 +4933,14 @@ static rb_ast_t *
yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
{
rb_ast_t *ast;
if (NIL_P(fname)) {
p->ruby_sourcefile_string = Qnil;
p->ruby_sourcefile = "(none)";
}
else {
p->ruby_sourcefile_string = rb_str_new_frozen(fname);
p->ruby_sourcefile = StringValueCStr(fname);
}
p->ruby_sourceline = line - 1;
p->ast = ast = rb_ast_new();
@ -8716,7 +8722,14 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
return NEW_FALSE(loc);
case keyword__FILE__:
WARN_LOCATION("__FILE__");
node = NEW_STR(add_mark_object(p, rb_str_dup(p->ruby_sourcefile_string)), loc);
{
VALUE file = p->ruby_sourcefile_string;
if (NIL_P(file))
file = rb_str_new(0, 0);
else
file = rb_str_dup(file);
node = NEW_STR(add_mark_object(p, file), loc);
}
return node;
case keyword__LINE__:
WARN_LOCATION("__LINE__");

View file

@ -180,6 +180,20 @@ class TestAst < Test::Unit::TestCase
assert_instance_of(RubyVM::AbstractSyntaxTree::Node, node_proc)
assert_instance_of(RubyVM::AbstractSyntaxTree::Node, node_method)
assert_raise(TypeError) { RubyVM::AbstractSyntaxTree.of("1 + 2") }
Tempfile.create(%w"test_of .rb") do |tmp|
tmp.print "#{<<-"begin;"}\n#{<<-'end;'}"
begin;
SCRIPT_LINES__ = {}
assert_instance_of(RubyVM::AbstractSyntaxTree::Node, RubyVM::AbstractSyntaxTree.of(proc {|x| x}))
end;
tmp.close
assert_separately(["-", tmp.path], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
load ARGV[0]
assert_empty(SCRIPT_LINES__)
end;
end
end
def test_scope_local_variables