ruby-changes:60978
From: Benoit <ko1@a...>
Date: Sat, 2 May 2020 23:09:40 +0900 (JST)
Subject: [ruby-changes:60978] a2be428c5f (master): Fix ObjectSpace::WeakMap#key? to work if the value is nil
https://git.ruby-lang.org/ruby.git/commit/?id=a2be428c5f From a2be428c5fec31b8adbd5ac087e7637ddf7e54d0 Mon Sep 17 00:00:00 2001 From: Benoit Daloze <eregontp@g...> Date: Sat, 2 May 2020 16:08:36 +0200 Subject: Fix ObjectSpace::WeakMap#key? to work if the value is nil * Fixes [Bug #16826] diff --git a/gc.c b/gc.c index 205a986..33f38bb 100644 --- a/gc.c +++ b/gc.c @@ -10757,7 +10757,7 @@ wmap_aset(VALUE self, VALUE wmap, VALUE orig) https://github.com/ruby/ruby/blob/trunk/gc.c#L10757 /* 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) https://github.com/ruby/ruby/blob/trunk/gc.c#L10765 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 */ diff --git a/spec/ruby/core/objectspace/weakmap/shared/include.rb b/spec/ruby/core/objectspace/weakmap/shared/include.rb index 370359e..f9c174b 100644 --- a/spec/ruby/core/objectspace/weakmap/shared/include.rb +++ b/spec/ruby/core/objectspace/weakmap/shared/include.rb @@ -21,7 +21,7 @@ describe :weakmap_include?, shared: true do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/objectspace/weakmap/shared/include.rb#L21 end 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 map = ObjectSpace::WeakMap.new key = Object.new -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/