Project

General

Profile

Bug #11360

Singleton class doesn't appear by ObjectSpace.each_object

Added by ko1 (Koichi Sasada) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:70006]

Description

Singleton class doesn't appear by ObjectSpace.each_object.

The following is reproducible code.

class C
  class << self
    p [self, self.class]
    $c = self
  end
end

ObjectSpace.each_object(Class){|o|
  exit if $c == o
}
raise "#{$c} is not found!"

This is because internal_object_p in gc.c skips singleton classes.

static int
internal_object_p(VALUE obj)
{
    RVALUE *p = (RVALUE *)obj;

    if (p->as.basic.flags) {
    switch (BUILTIN_TYPE(p)) {
      case T_NONE:
      case T_IMEMO:
      case T_ICLASS:
      case T_NODE:
      case T_ZOMBIE:
        break;
      case T_CLASS:
        if (FL_TEST(p, FL_SINGLETON))
          break;
      default:
        if (!p->as.basic.klass) break;
        return 0;
    }
    }
    return 1;
}

Associated revisions

Revision 51320
Added by ko1 (Koichi Sasada) over 2 years ago

  • gc.c (internal_object_p): Now a singleton classes appear by ObjectSpace.each_object. [Bug #11360]
  • test/ruby/test_objectspace.rb: add a test about it.

Revision 51320
Added by ko1 (Koichi Sasada) over 2 years ago

  • gc.c (internal_object_p): Now a singleton classes appear by ObjectSpace.each_object. [Bug #11360]
  • test/ruby/test_objectspace.rb: add a test about it.

Revision 51320
Added by ko1 (Koichi Sasada) over 2 years ago

  • gc.c (internal_object_p): Now a singleton classes appear by ObjectSpace.each_object. [Bug #11360]
  • test/ruby/test_objectspace.rb: add a test about it.

History

#1 [ruby-core:70007] Updated by ko1 (Koichi Sasada) over 2 years ago

From Ruby 1.6, singleton classes are not appeared.
Is it intentional?

#2 [ruby-core:70010] Updated by Eregon (Benoit Daloze) over 2 years ago

Koichi Sasada wrote:

From Ruby 1.6, singleton classes are not appeared.
Is it intentional?

I guess the reason is exposing the last layer of singleton classes would expose "VM-internal singleton classes/metaclasses".
The documentation of rb_singleton_class explains it a bit.
But basically the VM needs the metaclass of any class exposed to the user, and exposing them would require another layer, which if exposed as well would need another, etc.
Singleton classes which already have a metaclass could be iterated though.

#3 [ruby-core:70071] Updated by matz (Yukihiro Matsumoto) over 2 years ago

It was intentional, since in the old days singleton classes are merely internal data structure. After intoduction of #singleton_class method, singleton classes are objects visible to Ruby world. In that sense, now it's natural for them to be visible from #each_object.

Matz.

#4 Updated by ko1 (Koichi Sasada) over 2 years ago

  • Status changed from Open to Closed

Applied in changeset r51320.


  • gc.c (internal_object_p): Now a singleton classes appear by ObjectSpace.each_object. [Bug #11360]
  • test/ruby/test_objectspace.rb: add a test about it.

Also available in: Atom PDF