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

ruby-changes:37770

From: shugo <ko1@a...>
Date: Thu, 5 Mar 2015 11:56:16 +0900 (JST)
Subject: [ruby-changes:37770] shugo:r49851 (trunk): * vm_eval.c (eval_string_with_cref): A binding should keep

shugo	2015-03-05 11:56:03 +0900 (Thu, 05 Mar 2015)

  New Revision: 49851

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

  Log:
    * vm_eval.c (eval_string_with_cref): A binding should keep
      refinements activation information and the refinements should be
      activated in subsequent eval calls with the binding.
      [ruby-core:67945] [Bug #10818]

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_refinement.rb
    trunk/vm_eval.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49850)
+++ ChangeLog	(revision 49851)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Mar  5 11:50:54 2015  Shugo Maeda  <shugo@r...>
+
+	* vm_eval.c (eval_string_with_cref): A binding should keep
+	  refinements activation information and the refinements should be
+	  activated in subsequent eval calls with the binding.
+	  [ruby-core:67945] [Bug #10818]
+
 Thu Mar  5 11:16:55 2015  Shugo Maeda  <shugo@r...>
 
 	* test/ruby/test_refinement.rb: There is no need anymore to supress
Index: vm_eval.c
===================================================================
--- vm_eval.c	(revision 49850)
+++ vm_eval.c	(revision 49851)
@@ -1304,10 +1304,15 @@ eval_string_with_cref(VALUE self, VALUE https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1304
 	th->parse_in_eval--;
 
 	if (!cref && base_block->iseq) {
-	    orig_cref = rb_vm_get_cref(base_block->iseq, base_block->ep);
-	    cref = NEW_CREF(Qnil);
-	    crefval = (VALUE) cref;
-	    COPY_CREF(cref, orig_cref);
+	    if (NIL_P(scope)) {
+		orig_cref = rb_vm_get_cref(base_block->iseq, base_block->ep);
+		cref = NEW_CREF(Qnil);
+		crefval = (VALUE) cref;
+		COPY_CREF(cref, orig_cref);
+	    }
+	    else {
+		cref = rb_vm_get_cref(base_block->iseq, base_block->ep);
+	    }
 	}
 	vm_set_eval_stack(th, iseqval, cref, base_block);
 	th->cfp->klass = CLASS_OF(base_block->self);
Index: test/ruby/test_refinement.rb
===================================================================
--- test/ruby/test_refinement.rb	(revision 49850)
+++ test/ruby/test_refinement.rb	(revision 49851)
@@ -1,6 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L1
 require 'test/unit'
 
 class TestRefinement < Test::Unit::TestCase
+  module Sandbox
+    BINDING = binding
+  end
+
   class Foo
     def x
       return "Foo#x"
@@ -65,7 +69,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L69
     end
   end
 
-  eval <<-EOF, TOPLEVEL_BINDING
+  eval <<-EOF, Sandbox::BINDING
     using TestRefinement::FooExt
 
     class TestRefinement::FooExtClient
@@ -95,7 +99,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L99
     end
   EOF
 
-  eval <<-EOF, TOPLEVEL_BINDING
+  eval <<-EOF, Sandbox::BINDING
     using TestRefinement::FooExt
     using TestRefinement::FooExt2
 
@@ -411,7 +415,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L415
 
   def test_main_using_is_private
     assert_raise(NoMethodError) do
-      eval("self.using Module.new", TOPLEVEL_BINDING)
+      eval("self.using Module.new", Sandbox::BINDING)
     end
   end
 
@@ -426,7 +430,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L430
 
   def test_module_using_class
     assert_raise(TypeError) do
-      eval("using TestRefinement::UsingClass", TOPLEVEL_BINDING)
+      eval("using TestRefinement::UsingClass", Sandbox::BINDING)
     end
   end
 
@@ -587,7 +591,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L591
 
   def test_using_in_module
     assert_raise(RuntimeError) do
-      eval(<<-EOF, TOPLEVEL_BINDING)
+      eval(<<-EOF, Sandbox::BINDING)
         $main = self
         module M
         end
@@ -600,14 +604,16 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L604
 
   def test_using_in_method
     assert_raise(RuntimeError) do
-      eval(<<-EOF, TOPLEVEL_BINDING)
+      eval(<<-EOF, Sandbox::BINDING)
         $main = self
         module M
         end
-        def call_using_in_method
-          $main.send(:using, M)
+        class C
+          def call_using_in_method
+            $main.send(:using, M)
+          end
         end
-        call_using_in_method
+        C.new.call_using_in_method
       EOF
     end
   end
@@ -648,7 +654,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L654
     end
   end
 
-  eval <<-EOF, TOPLEVEL_BINDING
+  eval <<-EOF, Sandbox::BINDING
     using TestRefinement::IncludeIntoRefinement::M
 
     module TestRefinement::IncludeIntoRefinement::User
@@ -711,7 +717,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L717
     end
   end
 
-  eval <<-EOF, TOPLEVEL_BINDING
+  eval <<-EOF, Sandbox::BINDING
     using TestRefinement::PrependIntoRefinement::M
 
     module TestRefinement::PrependIntoRefinement::User
@@ -857,7 +863,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L863
 
   def test_module_using_invalid_self
     assert_raise(RuntimeError) do
-      eval <<-EOF, TOPLEVEL_BINDING
+      eval <<-EOF, Sandbox::BINDING
         module TestRefinement::TestModuleUsingInvalidSelf
           Module.new.send(:using, TestRefinement::FooExt)
         end
@@ -936,7 +942,7 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L942
   end
 
   def test_eval_with_binding_scoping
-    assert_in_out_err([], <<-INPUT, ["HELLO WORLD", "dlrow olleh", "HELLO WORLD"], [])
+    assert_in_out_err([], <<-INPUT, ["HELLO WORLD", "dlrow olleh", "dlrow olleh"], [])
       module M
         refine String do
           def upcase
@@ -946,8 +952,9 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L952
       end
 
       puts "hello world".upcase
-      puts eval(%{using M; "hello world".upcase}, TOPLEVEL_BINDING)
-      puts eval(%{"hello world".upcase}, TOPLEVEL_BINDING)
+      b = binding
+      puts eval(%{using M; "hello world".upcase}, b)
+      puts eval(%{"hello world".upcase}, b)
     INPUT
   end
 
@@ -1424,6 +1431,6 @@ class TestRefinement < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_refinement.rb#L1431
   private
 
   def eval_using(mod, s)
-    eval("using #{mod}; #{s}", TOPLEVEL_BINDING)
+    eval("using #{mod}; #{s}", Sandbox::BINDING)
   end
 end

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

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