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

ruby-changes:40048

From: nobu <ko1@a...>
Date: Thu, 15 Oct 2015 13:37:48 +0900 (JST)
Subject: [ruby-changes:40048] nobu:r52129 (trunk): proc.c: proc without env

nobu	2015-10-15 13:37:26 +0900 (Thu, 15 Oct 2015)

  New Revision: 52129

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

  Log:
    proc.c: proc without env
    
    * proc.c (rb_sym_to_proc): move from string.c and create a Proc
      with no environments.  [ruby-core:71088] [Bug #11594]

  Modified files:
    trunk/ChangeLog
    trunk/proc.c
    trunk/string.c
    trunk/test/ruby/test_symbol.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52128)
+++ ChangeLog	(revision 52129)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Oct 15 13:37:23 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* proc.c (rb_sym_to_proc): move from string.c and create a Proc
+	  with no environments.  [ruby-core:71088] [Bug #11594]
+
 Thu Oct 15 01:57:03 2015  CHIKANAGA Tomoyuki  <nagachika@r...>
 
 	* test/objspace/test_objspace.rb
Index: string.c
===================================================================
--- string.c	(revision 52128)
+++ string.c	(revision 52129)
@@ -8960,6 +8960,7 @@ rb_sym_proc_call(VALUE args, VALUE sym, https://github.com/ruby/ruby/blob/trunk/string.c#L8960
     return rb_funcall_with_block(obj, (ID)sym, argc - 1, argv + 1, passed_proc);
 }
 
+#if 0
 /*
  * call-seq:
  *   sym.to_proc
@@ -8972,34 +8973,8 @@ rb_sym_proc_call(VALUE args, VALUE sym, https://github.com/ruby/ruby/blob/trunk/string.c#L8973
 VALUE
 rb_sym_to_proc(VALUE sym)
 {
-    static VALUE sym_proc_cache = Qfalse;
-    enum {SYM_PROC_CACHE_SIZE = 67};
-    VALUE proc;
-    long index;
-    ID id;
-    VALUE *aryp;
-
-    if (!sym_proc_cache) {
-	sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
-	rb_gc_register_mark_object(sym_proc_cache);
-	rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
-    }
-
-    id = SYM2ID(sym);
-    index = (id % SYM_PROC_CACHE_SIZE) << 1;
-
-    aryp = RARRAY_PTR(sym_proc_cache);
-    if (aryp[index] == sym) {
-	return aryp[index + 1];
-    }
-    else {
-	proc = rb_proc_new(rb_sym_proc_call, (VALUE)id);
-	rb_block_clear_env_self(proc);
-	aryp[index] = sym;
-	aryp[index + 1] = proc;
-	return proc;
-    }
 }
+#endif
 
 /*
  * call-seq:
Index: proc.c
===================================================================
--- proc.c	(revision 52128)
+++ proc.c	(revision 52129)
@@ -1035,6 +1035,42 @@ rb_hash_proc(st_index_t hash, VALUE prc) https://github.com/ruby/ruby/blob/trunk/proc.c#L1035
     return rb_hash_uint(hash, (st_index_t)proc->block.ep >> 16);
 }
 
+VALUE
+rb_sym_to_proc(VALUE sym)
+{
+    static VALUE sym_proc_cache = Qfalse;
+    enum {SYM_PROC_CACHE_SIZE = 67};
+    VALUE proc;
+    long index;
+    ID id;
+    VALUE *aryp;
+
+    if (!sym_proc_cache) {
+	sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
+	rb_gc_register_mark_object(sym_proc_cache);
+	rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
+    }
+
+    id = SYM2ID(sym);
+    index = (id % SYM_PROC_CACHE_SIZE) << 1;
+
+    aryp = RARRAY_PTR(sym_proc_cache);
+    if (aryp[index] == sym) {
+	return aryp[index + 1];
+    }
+    else {
+	rb_proc_t *ptr;
+	VALUE ifunc = (VALUE)IFUNC_NEW(rb_sym_proc_call, (VALUE)id, 0);
+	proc = rb_proc_alloc(rb_cProc);
+	ptr = RTYPEDDATA_DATA(proc);
+	ptr->block.iseq = (rb_iseq_t *)ifunc;
+	ptr->block.proc = ifunc;
+	aryp[index] = sym;
+	aryp[index + 1] = proc;
+	return proc;
+    }
+}
+
 /*
  * call-seq:
  *   prc.hash   ->  integer
Index: test/ruby/test_symbol.rb
===================================================================
--- test/ruby/test_symbol.rb	(revision 52128)
+++ test/ruby/test_symbol.rb	(revision 52129)
@@ -147,6 +147,14 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L147
     end;
   end
 
+  def test_to_proc_call_with_symbol_proc
+    first = 1
+    bug11594 = "[ruby-core:71088] [Bug #11594] corrupted the first local variable"
+    # symbol which does not have a Proc
+    ->(&blk) {}.call(&:test_to_proc_call_with_symbol_proc)
+    assert_equal(1, first, bug11594)
+  end
+
   def test_call
     o = Object.new
     def o.foo(x, y); x + y; end

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

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