rubyx/lib/virtual/passes/collector.rb

37 lines
1.1 KiB
Ruby
Raw Normal View History

2015-05-31 10:07:49 +02:00
module Virtual
# collect anything that is in the space but and reachable from init
2015-05-31 10:07:49 +02:00
class Collector
def run
# init= Parfait::Space.object_space.get_class_by_name("Kernel").get_instance_method "__init__"
2015-07-30 18:18:41 +02:00
Virtual.machine.objects.clear
keep Parfait::Space.object_space , 0
2015-05-31 10:07:49 +02:00
end
2015-07-30 18:18:41 +02:00
def keep object , depth
return if object.nil?
2015-07-30 18:18:41 +02:00
#puts "adding #{' ' * depth}:#{object.class}"
#puts "ADD #{object.first.class}, #{object.last.class}" if object.is_a? Array
return unless Virtual.machine.add_object object
return unless object.respond_to? :has_layout?
2015-06-01 07:33:23 +02:00
if( object.is_a? Parfait::Method)
object.source.constants.each{|c|
2015-07-30 18:18:41 +02:00
#puts "keeping constant #{c.class}:#{c.object_id}"
keep(c , depth + 1)
}
2015-06-01 07:33:23 +02:00
end
layout = object.get_layout
2015-07-30 18:18:41 +02:00
keep(layout , depth + 1)
layout.object_instance_names.each do |name|
inst = object.get_instance_variable name
2015-07-30 18:18:41 +02:00
keep(inst , depth + 1)
2015-05-31 10:07:49 +02:00
end
if object.is_a? Parfait::List
object.each do |item|
2015-07-30 18:18:41 +02:00
keep(item , depth + 1)
2015-05-31 10:07:49 +02:00
end
end
end
end
end