rubyx/lib/register/collector.rb

35 lines
1.1 KiB
Ruby
Raw Normal View History

2015-10-22 18:16:29 +03:00
module Register
2015-05-31 11:07:49 +03:00
# collect anything that is in the space but and reachable from init
module Collector
def collect
# init= Parfait::Space.object_space.get_class_by_name("Kernel").get_instance_method "__init__"
self.objects.clear
2015-07-30 19:18:41 +03:00
keep Parfait::Space.object_space , 0
2015-10-28 13:00:23 +02:00
constants.each {|o| keep(o,0)}
2015-05-31 11:07:49 +03:00
end
2015-07-30 19:18:41 +03:00
def keep object , depth
return if object.nil?
2015-07-30 19:18:41 +03:00
#puts "adding #{' ' * depth}:#{object.class}"
#puts "ADD #{object.first.class}, #{object.last.class}" if object.is_a? Array
return unless self.add_object object
return unless object.respond_to? :has_layout?
layout = object.get_layout
2015-07-30 19:18:41 +03:00
keep(layout , depth + 1)
return if object.is_a? Symbol
layout.instance_names.each do |name|
#puts "Keep #{name} for #{object.class}"
inst = object.get_instance_variable name
2015-07-30 19:18:41 +03:00
keep(inst , depth + 1)
2015-05-31 11:07:49 +03:00
end
if object.is_a? Parfait::Indexed
2015-05-31 11:07:49 +03:00
object.each do |item|
2015-08-06 18:27:25 +03:00
#puts "Keep item "
2015-07-30 19:18:41 +03:00
keep(item , depth + 1)
2015-05-31 11:07:49 +03:00
end
end
end
end
end