Temporary tag for this failure. Updated CI spec coming.
[rbx.git] / kernel / core / objectspace.rb
blob7f9479df359372211a866b4daef7c1dfb1c0ea25
1 # depends on: module.rb
3 module ObjectSpace
4   def self.each_object(what=nil, &block)
5     unless what == Class
6       raise ArgumentError, "ObjectSpace doesn't support '#{what}' yet"
7     end
8     
9     emit_subclasses(Object, block)
10   end
11   
12   def self.emit_subclasses(start, prc)
13     subs = start.__subclasses__
14     return 0 unless subs
15     count = 0
16     
17     subs.each do |c|
18       count += 1
19       prc.call(c)
20       count += emit_subclasses(c, prc)
21     end
22     
23     return count
24   end
26   # Finalizer support. Uses WeakRef to detect object death.
27   # WeakRef uses the GC to do all the real work.
29   @finalizers = Hash.new
31   def self.define_finalizer(obj, prc=nil, &block)
32     prc ||= block
34     if prc.nil? or !prc.respond_to?(:call)
35       raise ArgumentError, "action must respond to call"
36     end
38     @finalizers[obj.object_id] = [WeakRef.new(obj), prc]
39     return nil
40   end
42   def self.undefine_finalizer(obj)
43     @finalizers.delete(obj.object_id)
44   end
46   def self.run_finalizers
47     @finalizers.each_pair do |key, val|
48       unless val[0].weakref_alive?
49         @finalizers.delete key
50         val[1].call(key)
51       end
52     end
53   end
55   def self.garbage_collect
56     GC.start
57   end
59   def self.after_loaded
60     # Fire up the Thread that will process finalization
61     @thread = Thread.new do
62       loop do
63         Rubinius::ON_GC.receive
64         ObjectSpace.run_finalizers
65       end
66     end
67   end
68 end