1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

objspace_dump.c: tag singleton classes and reference the superclass

This commit is contained in:
Jean Boussier 2021-01-21 09:45:11 +01:00 committed by Aaron Patterson
parent 365326a09e
commit 3a888398a6
Notes: git 2021-02-05 02:53:57 +09:00
2 changed files with 38 additions and 0 deletions

View file

@ -419,14 +419,37 @@ dump_object(VALUE obj, struct dump_config *dc)
dump_append(dc, ", \"embedded\":true");
break;
case T_ICLASS:
if (rb_class_get_superclass(obj)) {
dump_append(dc, ", \"superclass\":");
dump_append_ref(dc, rb_class_get_superclass(obj));
}
break;
case T_CLASS:
case T_MODULE:
if (rb_class_get_superclass(obj)) {
dump_append(dc, ", \"superclass\":");
dump_append_ref(dc, rb_class_get_superclass(obj));
}
if (dc->cur_obj_klass) {
VALUE mod_name = rb_mod_name(obj);
if (!NIL_P(mod_name)) {
dump_append(dc, ", \"name\":\"");
dump_append(dc, RSTRING_PTR(mod_name));
dump_append(dc, "\"");
} else {
VALUE real_mod_name = rb_mod_name(rb_class_real(obj));
if (RTEST(real_mod_name)) {
dump_append(dc, ", \"real_class_name\":\"");
dump_append(dc, RSTRING_PTR(real_mod_name));
dump_append(dc, "\"");
}
}
if (FL_TEST(obj, FL_SINGLETON)) {
dump_append(dc, ", \"singleton\":true");
}
}
break;

View file

@ -299,6 +299,21 @@ class TestObjSpace < Test::Unit::TestCase
assert_equal('{"type":"SYMBOL", "value":"foo"}', ObjectSpace.dump(:foo))
end
def test_dump_singleton_class
assert_include(ObjectSpace.dump(Object), '"name":"Object"')
assert_include(ObjectSpace.dump(Kernel), '"name":"Kernel"')
assert_include(ObjectSpace.dump(Object.new.singleton_class), '"real_class_name":"Object"')
singleton = Object.new.singleton_class
singleton_dump = ObjectSpace.dump(singleton)
assert_include(singleton_dump, '"singleton":true')
if defined?(JSON)
assert_equal(Object, singleton.superclass)
superclass_address = JSON.parse(ObjectSpace.dump(Object)).fetch('address')
assert_equal(superclass_address, JSON.parse(singleton_dump).fetch('superclass'))
end
end
def test_dump_special_floats
assert_match(/"value":"NaN"/, ObjectSpace.dump(Float::NAN))
assert_match(/"value":"Inf"/, ObjectSpace.dump(Float::INFINITY))