From e12799fd31d04d73e58a3045ccc9f45faca8ad09 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 18 Oct 2013 06:59:12 +0000 Subject: [PATCH] gc.c: ObjectSpace::WeakMap#key? * gc.c (wmap_each_i): add ObjectSpace::WeakMap#key? method. * lib/weakref.rb (WeakRef#weakref_alive): use above method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43351 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- gc.c | 10 ++++++++++ lib/weakref.rb | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/gc.c b/gc.c index 0a6f615b06..e84fb27728 100644 --- a/gc.c +++ b/gc.c @@ -5305,6 +5305,13 @@ wmap_aref(VALUE self, VALUE wmap) return obj; } +/* Returns +true+ if +key+ is registered */ +static VALUE +wmap_has_key(VALUE self, VALUE key) +{ + return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue; +} + /* ------------------------------ GC profiler ------------------------------ @@ -6081,6 +6088,9 @@ Init_GC(void) rb_define_alloc_func(rb_cWeakMap, wmap_allocate); rb_define_method(rb_cWeakMap, "[]=", wmap_aset, 2); rb_define_method(rb_cWeakMap, "[]", wmap_aref, 1); + rb_define_method(rb_cWeakMap, "include?", wmap_has_key, 1); + rb_define_method(rb_cWeakMap, "member?", wmap_has_key, 1); + rb_define_method(rb_cWeakMap, "key?", wmap_has_key, 0); rb_define_private_method(rb_cWeakMap, "finalize", wmap_finalize, 1); } diff --git a/lib/weakref.rb b/lib/weakref.rb index dd2cee16aa..36cbe3f10f 100644 --- a/lib/weakref.rb +++ b/lib/weakref.rb @@ -101,7 +101,7 @@ class WeakRef < Delegator # Returns true if the referenced object is still alive. def weakref_alive? - !!(@@__map[self] or defined?(@delegate_sd_obj)) + @@__map.key?(self) or defined?(@delegate_sd_obj) end end