1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

gc.c: ObjectSpace::WeakMap#size

* gc.c (wmap_size): add ObjectSpace::WeakMap#size and #length.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44093 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-12-09 07:13:40 +00:00
parent b73a306441
commit d2cc188ad5
3 changed files with 34 additions and 0 deletions

View file

@ -1,3 +1,7 @@
Mon Dec 9 16:13:31 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* gc.c (wmap_size): add ObjectSpace::WeakMap#size and #length.
Mon Dec 9 15:26:17 2013 Shugo Maeda <shugo@ruby-lang.org>
* test/test_curses.rb: removed.

16
gc.c
View file

@ -6470,6 +6470,20 @@ wmap_has_key(VALUE self, VALUE key)
return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
}
static VALUE
wmap_size(VALUE self)
{
struct weakmap *w;
st_index_t n;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
n = w->wmap2obj->num_entries;
#if SIZEOF_ST_INDEX_T <= SIZEOF_LONG
return ULONG2NUM(n);
#else
return ULL2NUM(n);
#endif
}
/*
------------------------------ GC profiler ------------------------------
@ -7304,6 +7318,8 @@ Init_GC(void)
rb_define_method(rb_cWeakMap, "each_value", wmap_each_value, 0);
rb_define_method(rb_cWeakMap, "keys", wmap_keys, 0);
rb_define_method(rb_cWeakMap, "values", wmap_values, 0);
rb_define_method(rb_cWeakMap, "size", wmap_size, 0);
rb_define_method(rb_cWeakMap, "length", wmap_size, 0);
rb_define_private_method(rb_cWeakMap, "finalize", wmap_finalize, 1);
rb_include_module(rb_cWeakMap, rb_mEnumerable);
}

View file

@ -93,4 +93,18 @@ class TestWeakMap < Test::Unit::TestCase
end
assert_equal(2, n)
end
def test_size
m = __callee__[/test_(.*)/, 1]
assert_equal(0, @wm.__send__(m))
x1 = "foo"
k1 = Object.new
@wm[k1] = x1
assert_equal(1, @wm.__send__(m))
x2 = "bar"
k2 = Object.new
@wm[k2] = x2
assert_equal(2, @wm.__send__(m))
end
alias test_length test_size
end