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

ruby-changes:22666

From: marcandre <ko1@a...>
Date: Tue, 21 Feb 2012 09:13:55 +0900 (JST)
Subject: [ruby-changes:22666] marcandRe: r34715 (trunk): * proc.c (method_hash, proc_hash): Fix {Unbound}Method#hash

marcandre	2012-02-21 09:13:44 +0900 (Tue, 21 Feb 2012)

  New Revision: 34715

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

  Log:
    * proc.c (method_hash, proc_hash): Fix {Unbound}Method#hash
      [Bug #6048]. Isolate hash computation for proc
    
    * internal.h: Declaration for above
    
    * vm_method.c (rb_method_definition_hash): Computation for
      hash part of a method definition
    
    * method.h: Declaration for above
    
    * test/ruby/test_method.rb: Test for above

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/method.h
    trunk/proc.c
    trunk/test/ruby/test_method.rb
    trunk/vm_method.c

Index: method.h
===================================================================
--- method.h	(revision 34714)
+++ method.h	(revision 34715)
@@ -96,6 +96,7 @@
 
 int rb_method_entry_arity(const rb_method_entry_t *me);
 int rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2);
+st_index_t rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me);
 
 void rb_mark_method_entry(const rb_method_entry_t *me);
 void rb_free_method_entry(rb_method_entry_t *me);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34714)
+++ ChangeLog	(revision 34715)
@@ -1,3 +1,17 @@
+Tue Feb 21 09:13:25 2012  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* proc.c (method_hash, proc_hash): Fix {Unbound}Method#hash
+	  [Bug #6048]. Isolate hash computation for proc
+	
+	* internal.h: Declaration for above
+	
+	* vm_method.c (rb_method_definition_hash): Computation for
+	  hash part of a method definition
+	
+	* method.h: Declaration for above
+	
+	* test/ruby/test_method.rb: Test for above
+
 Tue Feb 21 02:56:15 2012  Yukihiro Matsumoto  <matz@r...>
 
 	* enumerator.c (enumerator_rewind): update the documentation.
Index: proc.c
===================================================================
--- proc.c	(revision 34714)
+++ proc.c	(revision 34715)
@@ -794,6 +794,15 @@
     return Qfalse;
 }
 
+st_index_t
+rb_hash_proc(st_index_t hash, VALUE prc)
+{
+    const rb_proc_t *proc = (const rb_proc_t *)prc;
+    hash = rb_hash_uint(hash, (st_index_t)proc->block.iseq);
+    hash = rb_hash_uint(hash, (st_index_t)proc->envval);
+    return rb_hash_uint(hash, (st_index_t)proc->block.lfp >> 16);
+}
+
 /*
  * call-seq:
  *   prc.hash   ->  integer
@@ -807,9 +816,8 @@
     st_index_t hash;
     rb_proc_t *proc;
     GetProcPtr(self, proc);
-    hash = rb_hash_start((st_index_t)proc->block.iseq);
-    hash = rb_hash_uint(hash, (st_index_t)proc->envval);
-    hash = rb_hash_uint(hash, (st_index_t)proc->block.lfp >> 16);
+    hash = rb_hash_start(0);
+    hash = rb_hash_proc(hash, proc);
     hash = rb_hash_end(hash);
     return LONG2FIX(hash);
 }
@@ -1075,7 +1083,7 @@
     TypedData_Get_Struct(method, struct METHOD, &method_data_type, m);
     hash = rb_hash_start((st_index_t)m->rclass);
     hash = rb_hash_uint(hash, (st_index_t)m->recv);
-    hash = rb_hash_uint(hash, (st_index_t)m->me->def);
+    hash = rb_hash_method_entry(hash, m->me);
     hash = rb_hash_end(hash);
 
     return INT2FIX(hash);
Index: vm_method.c
===================================================================
--- vm_method.c	(revision 34714)
+++ vm_method.c	(revision 34715)
@@ -904,6 +904,40 @@
     }
 }
 
+static st_index_t
+rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
+{
+    hash = rb_hash_uint(hash, def->type);
+    switch (def->type) {
+      case VM_METHOD_TYPE_ISEQ:
+	return rb_hash_uint(hash, (st_index_t)def->body.iseq);
+      case VM_METHOD_TYPE_CFUNC:
+	hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
+	return rb_hash_uint(hash, def->body.cfunc.argc);
+      case VM_METHOD_TYPE_ATTRSET:
+      case VM_METHOD_TYPE_IVAR:
+	return rb_hash_uint(hash, def->body.attr.id);
+      case VM_METHOD_TYPE_BMETHOD:
+	return rb_hash_proc(hash, def->body.proc);
+      case VM_METHOD_TYPE_MISSING:
+	return rb_hash_uint(hash, def->original_id);
+      case VM_METHOD_TYPE_ZSUPER:
+      case VM_METHOD_TYPE_NOTIMPLEMENTED:
+      case VM_METHOD_TYPE_UNDEF:
+	return hash;
+      case VM_METHOD_TYPE_OPTIMIZED:
+	return rb_hash_uint(hash, def->body.optimize_type);
+      default:
+	rb_bug("rb_hash_method_definition: unsupported method type (%d)\n", def->type);
+    }
+    return hash;
+}
+
+st_index_t
+rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me) {
+    return rb_hash_method_definition(hash, me->def);
+}
+
 void
 rb_alias(VALUE klass, ID name, ID def)
 {
Index: internal.h
===================================================================
--- internal.h	(revision 34714)
+++ internal.h	(revision 34715)
@@ -150,6 +150,7 @@
 
 /* proc.c */
 VALUE rb_proc_location(VALUE self);
+st_index_t rb_hash_proc(st_index_t hash, VALUE proc);
 
 /* rational.c */
 VALUE rb_lcm(VALUE x, VALUE y);
Index: test/ruby/test_method.rb
===================================================================
--- test/ruby/test_method.rb	(revision 34714)
+++ test/ruby/test_method.rb	(revision 34715)
@@ -158,6 +158,7 @@
     o = Object.new
     def o.foo; end
     assert_kind_of(Integer, o.method(:foo).hash)
+    assert_equal(Array.instance_method(:map).hash, Array.instance_method(:collect).hash)
   end
 
   def test_receiver_name_owner

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

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