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

ruby-changes:53431

From: nobu <ko1@a...>
Date: Fri, 9 Nov 2018 22:39:43 +0900 (JST)
Subject: [ruby-changes:53431] nobu:r65647 (trunk): Get rid of setting SCRIPT_LINES__ by AST.parse

nobu	2018-11-09 22:39:36 +0900 (Fri, 09 Nov 2018)

  New Revision: 65647

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65647

  Log:
    Get rid of setting SCRIPT_LINES__ by AST.parse

  Modified files:
    trunk/ast.c
    trunk/parse.y
    trunk/test/ruby/test_ast.rb
Index: parse.y
===================================================================
--- parse.y	(revision 65646)
+++ parse.y	(revision 65647)
@@ -4873,7 +4873,7 @@ yycompile0(VALUE arg) https://github.com/ruby/ruby/blob/trunk/parse.y#L4873
     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 * https://github.com/ruby/ruby/blob/trunk/parse.y#L4933
 yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
 {
     rb_ast_t *ast;
-    p->ruby_sourcefile_string = rb_str_new_frozen(fname);
-    p->ruby_sourcefile = StringValueCStr(fname);
+    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, https://github.com/ruby/ruby/blob/trunk/parse.y#L8722
 	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__");
Index: ast.c
===================================================================
--- ast.c	(revision 65646)
+++ ast.c	(revision 65647)
@@ -83,7 +83,7 @@ rb_ast_parse_str(VALUE str) https://github.com/ruby/ruby/blob/trunk/ast.c#L83
 
     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) https://github.com/ruby/ruby/blob/trunk/ast.c#L127
     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);
 
Index: test/ruby/test_ast.rb
===================================================================
--- test/ruby/test_ast.rb	(revision 65646)
+++ test/ruby/test_ast.rb	(revision 65647)
@@ -180,6 +180,20 @@ class TestAst < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_ast.rb#L180
     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

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

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