diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | gc.c | 16 | ||||
-rw-r--r-- | test/ruby/test_weakmap.rb | 14 |
3 files changed, 34 insertions, 0 deletions
@@ -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. @@ -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); } diff --git a/test/ruby/test_weakmap.rb b/test/ruby/test_weakmap.rb index 383b5e1f31..c0d1747d2a 100644 --- a/test/ruby/test_weakmap.rb +++ b/test/ruby/test_weakmap.rb @@ -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 |