mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
ObjectSpace::WeakMap#inspect: check if living object [Bug #18392]
This commit is contained in:
parent
ec878dac90
commit
d6c5a30cfd
Notes:
git
2021-12-07 21:56:10 +09:00
2 changed files with 32 additions and 6 deletions
29
gc.c
29
gc.c
|
@ -12117,10 +12117,26 @@ struct wmap_iter_arg {
|
||||||
VALUE value;
|
VALUE value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
wmap_inspect_append(rb_objspace_t *objspace, VALUE str, VALUE obj)
|
||||||
|
{
|
||||||
|
if (SPECIAL_CONST_P(obj)) {
|
||||||
|
return rb_str_append(str, rb_inspect(obj));
|
||||||
|
}
|
||||||
|
else if (wmap_live_p(objspace, obj)) {
|
||||||
|
return rb_str_append(str, rb_any_to_s(obj));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return rb_str_catf(str, "#<collected:%p>", (void*)obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
wmap_inspect_i(st_data_t key, st_data_t val, st_data_t arg)
|
wmap_inspect_i(st_data_t key, st_data_t val, st_data_t arg)
|
||||||
{
|
{
|
||||||
VALUE str = (VALUE)arg;
|
struct wmap_iter_arg *argp = (struct wmap_iter_arg *)arg;
|
||||||
|
rb_objspace_t *objspace = argp->objspace;
|
||||||
|
VALUE str = argp->value;
|
||||||
VALUE k = (VALUE)key, v = (VALUE)val;
|
VALUE k = (VALUE)key, v = (VALUE)val;
|
||||||
|
|
||||||
if (RSTRING_PTR(str)[0] == '#') {
|
if (RSTRING_PTR(str)[0] == '#') {
|
||||||
|
@ -12130,11 +12146,9 @@ wmap_inspect_i(st_data_t key, st_data_t val, st_data_t arg)
|
||||||
rb_str_cat2(str, ": ");
|
rb_str_cat2(str, ": ");
|
||||||
RSTRING_PTR(str)[0] = '#';
|
RSTRING_PTR(str)[0] = '#';
|
||||||
}
|
}
|
||||||
k = SPECIAL_CONST_P(k) ? rb_inspect(k) : rb_any_to_s(k);
|
wmap_inspect_append(objspace, str, k);
|
||||||
rb_str_append(str, k);
|
|
||||||
rb_str_cat2(str, " => ");
|
rb_str_cat2(str, " => ");
|
||||||
v = SPECIAL_CONST_P(v) ? rb_inspect(v) : rb_any_to_s(v);
|
wmap_inspect_append(objspace, str, v);
|
||||||
rb_str_append(str, v);
|
|
||||||
|
|
||||||
return ST_CONTINUE;
|
return ST_CONTINUE;
|
||||||
}
|
}
|
||||||
|
@ -12145,11 +12159,14 @@ wmap_inspect(VALUE self)
|
||||||
VALUE str;
|
VALUE str;
|
||||||
VALUE c = rb_class_name(CLASS_OF(self));
|
VALUE c = rb_class_name(CLASS_OF(self));
|
||||||
struct weakmap *w;
|
struct weakmap *w;
|
||||||
|
struct wmap_iter_arg args;
|
||||||
|
|
||||||
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
|
TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w);
|
||||||
str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void *)self);
|
str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void *)self);
|
||||||
if (w->wmap2obj) {
|
if (w->wmap2obj) {
|
||||||
st_foreach(w->wmap2obj, wmap_inspect_i, str);
|
args.objspace = &rb_objspace;
|
||||||
|
args.value = str;
|
||||||
|
st_foreach(w->wmap2obj, wmap_inspect_i, (st_data_t)&args);
|
||||||
}
|
}
|
||||||
RSTRING_PTR(str)[0] = '#';
|
RSTRING_PTR(str)[0] = '#';
|
||||||
rb_str_cat2(str, ">");
|
rb_str_cat2(str, ">");
|
||||||
|
|
|
@ -73,6 +73,15 @@ class TestWeakMap < Test::Unit::TestCase
|
||||||
@wm.inspect)
|
@wm.inspect)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_inspect_garbage
|
||||||
|
1000.times do |i|
|
||||||
|
@wm[i] = Object.new
|
||||||
|
@wm.inspect
|
||||||
|
end
|
||||||
|
assert_match(/\A\#<#{@wm.class.name}:[^:]++:(?:\s\d+\s=>\s\#<(?:Object|collected):[^:<>]*+>(?:,|>\z))+/,
|
||||||
|
@wm.inspect)
|
||||||
|
end
|
||||||
|
|
||||||
def test_each
|
def test_each
|
||||||
m = __callee__[/test_(.*)/, 1]
|
m = __callee__[/test_(.*)/, 1]
|
||||||
x1 = Object.new
|
x1 = Object.new
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue