summaryrefslogtreecommitdiff
path: root/gc.c
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2020-05-02 16:08:36 +0200
committerBenoit Daloze <eregontp@gmail.com>2020-05-02 16:08:36 +0200
commita2be428c5fec31b8adbd5ac087e7637ddf7e54d0 (patch)
tree4754c76c0d8f265b54041169dc68376144a36f51 /gc.c
parentc9213aa864fb8527388679c21f1ea8ce129e2f1a (diff)
Fix ObjectSpace::WeakMap#key? to work if the value is nil
* Fixes [Bug #16826]
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/gc.c b/gc.c
index 205a986271..33f38bb7ad 100644
--- a/gc.c
+++ b/gc.c
@@ -10757,7 +10757,7 @@ wmap_aset(VALUE self, VALUE wmap, VALUE orig)
/* Retrieves a weakly referenced object with the given key */
static VALUE
-wmap_aref(VALUE self, VALUE wmap)
+wmap_lookup(VALUE self, VALUE key)
{
st_data_t data;
VALUE obj;
@@ -10765,17 +10765,25 @@ wmap_aref(VALUE self, VALUE wmap)
rb_objspace_t *objspace = &rb_objspace;
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
- if (!st_lookup(w->wmap2obj, (st_data_t)wmap, &data)) return Qnil;
+ if (!st_lookup(w->wmap2obj, (st_data_t)key, &data)) return Qundef;
obj = (VALUE)data;
- if (!wmap_live_p(objspace, obj)) return Qnil;
+ if (!wmap_live_p(objspace, obj)) return Qundef;
return obj;
}
+/* Retrieves a weakly referenced object with the given key */
+static VALUE
+wmap_aref(VALUE self, VALUE key)
+{
+ VALUE obj = wmap_lookup(self, key);
+ return obj != Qundef ? obj : Qnil;
+}
+
/* Returns +true+ if +key+ is registered */
static VALUE
wmap_has_key(VALUE self, VALUE key)
{
- return NIL_P(wmap_aref(self, key)) ? Qfalse : Qtrue;
+ return wmap_lookup(self, key) == Qundef ? Qfalse : Qtrue;
}
/* Returns the number of referenced objects */