aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--hash.c69
-rw-r--r--test/ruby/test_array.rb1
-rw-r--r--test/ruby/test_thread.rb13
4 files changed, 87 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 92e929ab8a..b8a2526872 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,9 @@
-Tue Dec 3 22:18:27 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Tue Dec 3 22:32:18 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * hash.c (rb_hash_recursive): make similar (recursive) constructs
+ return same hash value. execute recursively, and rewind to the
+ topmost frame with an object which .eql? to the recursive
+ object, if recursion is detected.
* hash.c (rb_hash): detect recursion for all `hash' methods. each
`hash' methods no longer need to use rb_exec_recursive().
diff --git a/hash.c b/hash.c
index 7d27c543d7..a69d99a821 100644
--- a/hash.c
+++ b/hash.c
@@ -48,7 +48,7 @@ rb_hash_freeze(VALUE hash)
VALUE rb_cHash;
static VALUE envtbl;
-static ID id_hash, id_yield, id_default;
+static ID id_hash, id_yield, id_default, id_recursive_hash;
VALUE
rb_hash_set_ifnone(VALUE hash, VALUE ifnone)
@@ -76,6 +76,71 @@ rb_any_cmp(VALUE a, VALUE b)
return !rb_eql(a, b);
}
+struct hash_recursive_args {
+ VALUE (*func)(VALUE, VALUE, int);
+ VALUE obj;
+ VALUE arg;
+};
+
+static VALUE
+call_hash(RB_BLOCK_CALL_FUNC_ARGLIST(tag, data))
+{
+ struct hash_recursive_args *p = (struct hash_recursive_args *)tag;
+ return p->func(p->obj, p->arg, 0);
+}
+
+static VALUE
+rb_hash_recursive(VALUE (*func)(VALUE, VALUE, int), VALUE obj, VALUE arg)
+{
+ VALUE hval;
+ VALUE thread = rb_thread_current();
+ VALUE recursion_list = rb_thread_local_aref(thread, id_recursive_hash);
+ struct hash_recursive_args args;
+ long len = 0;
+ int state;
+
+ if (!NIL_P(recursion_list) && (len = RARRAY_LEN(recursion_list)) > 0) {
+ VALUE outer;
+ struct hash_recursive_args *outerp;
+ const VALUE *ptr = RARRAY_CONST_PTR(recursion_list);
+ long i = 0;
+ do {
+ outerp = (struct hash_recursive_args *)(ptr[i] & ~FIXNUM_FLAG);
+ if (outerp->obj == obj) {
+ len = i;
+ for (i = 0; i < len; ++i) {
+ outer = RARRAY_AREF(recursion_list, i) & ~FIXNUM_FLAG;
+ outerp = (struct hash_recursive_args *)outer;
+ if (rb_eql(obj, outerp->obj)) {
+ rb_throw_obj(outer, outer);
+ }
+ }
+ outer = RARRAY_AREF(recursion_list, len) & ~FIXNUM_FLAG;
+ outerp = (struct hash_recursive_args *)outer;
+ rb_throw_obj(outer, outer);
+ }
+ } while (++i < len);
+ }
+ else {
+ recursion_list = rb_ary_new();
+ rb_thread_local_aset(thread, id_recursive_hash, recursion_list);
+ }
+ args.func = func;
+ args.obj = obj;
+ args.arg = arg;
+ rb_ary_push(recursion_list, (VALUE)&args | FIXNUM_FLAG);
+ hval = rb_catch_protect((VALUE)&args, call_hash, (VALUE)&args, &state);
+ if (RARRAY_LEN(recursion_list) >= len) {
+ rb_ary_set_len(recursion_list, len);
+ if (len == 0) rb_thread_local_aset(thread, id_recursive_hash, Qnil);
+ }
+ if (state) rb_jump_tag(state);
+ if (hval == (VALUE)&args) {
+ hval = (*func)(obj, arg, 1);
+ }
+ return hval;
+}
+
static VALUE
hash_recursive(VALUE obj, VALUE arg, int recurse)
{
@@ -89,7 +154,7 @@ hash_recursive(VALUE obj, VALUE arg, int recurse)
VALUE
rb_hash(VALUE obj)
{
- VALUE hval = rb_exec_recursive_paired(hash_recursive, obj, obj, 0);
+ VALUE hval = rb_hash_recursive(hash_recursive, obj, 0);
retry:
switch (TYPE(hval)) {
case T_FIXNUM:
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index d53b345898..0235f00471 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2053,6 +2053,7 @@ class TestArray < Test::Unit::TestCase
assert_not_equal([[1]].hash, [[2]].hash)
a = []
a << a
+ assert_equal([[a]].hash, a.hash)
assert_not_equal([a, 1].hash, [a, 2].hash)
assert_not_equal([a, a].hash, a.hash) # Implementation dependent
end
diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb
index f74c4ec9cb..ab1b81a8fe 100644
--- a/test/ruby/test_thread.rb
+++ b/test/ruby/test_thread.rb
@@ -467,6 +467,19 @@ class TestThread < Test::Unit::TestCase
m.unlock
end
+ def test_recursive_outer
+ arr = []
+ obj = Struct.new(:foo, :visited).new(arr, false)
+ arr << obj
+ def obj.hash
+ self[:visited] = true
+ super
+ raise "recursive_outer should short circuit intermediate calls"
+ end
+ assert_nothing_raised {arr.hash}
+ assert(obj[:visited], "obj.hash was not called")
+ end
+
def test_thread_instance_variable
bug4389 = '[ruby-core:35192]'
assert_in_out_err([], <<-INPUT, %w(), [], bug4389)