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

ruby-changes:45086

From: nobu <ko1@a...>
Date: Fri, 23 Dec 2016 12:22:40 +0900 (JST)
Subject: [ruby-changes:45086] nobu:r57158 (trunk): assertions.rb: syntax check by iseq

nobu	2016-12-23 12:22:35 +0900 (Fri, 23 Dec 2016)

  New Revision: 57158

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

  Log:
    assertions.rb: syntax check by iseq
    
    * test/lib/test/unit/assertions.rb (assert_valid_syntax): use
      RubyVM::InstructionSequence.compile to get rid of executing the
      code, instead of catch&throw.  sample/trick2015/kinaba/entry.rb
      no longer raises an Invalid return.
    
    * test/lib/test/unit/assertions.rb (assert_syntax_error): ditto.

  Modified files:
    trunk/test/lib/test/unit/assertions.rb
Index: test/lib/test/unit/assertions.rb
===================================================================
--- test/lib/test/unit/assertions.rb	(revision 57157)
+++ test/lib/test/unit/assertions.rb	(revision 57158)
@@ -456,11 +456,7 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L456
       alias pend skip
 
       def assert_valid_syntax(code, fname = caller_locations(1, 1)[0], mesg = fname.to_s, verbose: nil)
-        code = code.b
-        code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
-          "#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
-        }
-        code.force_encoding(Encoding::UTF_8)
+        code = code.dup.force_encoding(Encoding::UTF_8)
         verbose, $VERBOSE = $VERBOSE, verbose
         yield if defined?(yield)
         case
@@ -469,21 +465,17 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L465
         when defined?(fname.path) && defined?(fname.lineno)
           fname, line = fname.path, fname.lineno
         else
-          line = 0
+          line = 1
         end
         assert_nothing_raised(SyntaxError, mesg) do
-          assert_equal(:ok, catch {|tag| eval(code, binding, fname, line)}, mesg)
+          RubyVM::InstructionSequence.compile(code, fname, fname, line)
         end
       ensure
         $VERBOSE = verbose
       end
 
       def assert_syntax_error(code, error, fname = caller_locations(1, 1)[0], mesg = fname.to_s)
-        code = code.b
-        code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
-          "#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ng}\n"
-        }
-        code.force_encoding(Encoding::US_ASCII)
+        code = code.dup.force_encoding(Encoding::US_ASCII)
         verbose, $VERBOSE = $VERBOSE, nil
         yield if defined?(yield)
         case
@@ -492,10 +484,10 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L484
         when defined?(fname.path) && defined?(fname.lineno)
           fname, line = fname.path, fname.lineno
         else
-          line = 0
+          line = 1
         end
         e = assert_raise(SyntaxError, mesg) do
-          catch {|tag| eval(code, binding, fname, line)}
+          RubyVM::InstructionSequence.compile(code, fname, fname, line)
         end
         assert_match(error, e.message, mesg)
         e

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

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