mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Fix ObjectSpace::WeakMap#key? to work if the value is nil
* Fixes [Bug #16826]
This commit is contained in:
parent
c9213aa864
commit
a2be428c5f
2 changed files with 13 additions and 5 deletions
16
gc.c
16
gc.c
|
@ -10757,7 +10757,7 @@ wmap_aset(VALUE self, VALUE wmap, VALUE orig)
|
||||||
|
|
||||||
/* Retrieves a weakly referenced object with the given key */
|
/* Retrieves a weakly referenced object with the given key */
|
||||||
static VALUE
|
static VALUE
|
||||||
wmap_aref(VALUE self, VALUE wmap)
|
wmap_lookup(VALUE self, VALUE key)
|
||||||
{
|
{
|
||||||
st_data_t data;
|
st_data_t data;
|
||||||
VALUE obj;
|
VALUE obj;
|
||||||
|
@ -10765,17 +10765,25 @@ wmap_aref(VALUE self, VALUE wmap)
|
||||||
rb_objspace_t *objspace = &rb_objspace;
|
rb_objspace_t *objspace = &rb_objspace;
|
||||||
|
|
||||||
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
|
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;
|
obj = (VALUE)data;
|
||||||
if (!wmap_live_p(objspace, obj)) return Qnil;
|
if (!wmap_live_p(objspace, obj)) return Qundef;
|
||||||
return obj;
|
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 */
|
/* Returns +true+ if +key+ is registered */
|
||||||
static VALUE
|
static VALUE
|
||||||
wmap_has_key(VALUE self, VALUE key)
|
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 */
|
/* Returns the number of referenced objects */
|
||||||
|
|
|
@ -21,7 +21,7 @@ describe :weakmap_include?, shared: true do
|
||||||
end
|
end
|
||||||
|
|
||||||
ruby_version_is "2.7" do
|
ruby_version_is "2.7" do
|
||||||
ruby_bug "#16826", "2.7.0"..."2.8.1" do
|
ruby_bug "#16826", "2.7.0"..."2.7.2" do
|
||||||
it "reports true if the pair exists and the value is nil" do
|
it "reports true if the pair exists and the value is nil" do
|
||||||
map = ObjectSpace::WeakMap.new
|
map = ObjectSpace::WeakMap.new
|
||||||
key = Object.new
|
key = Object.new
|
||||||
|
|
Loading…
Reference in a new issue