2015-04-08 19:24:50 +02:00
|
|
|
|
2015-04-15 10:38:46 +02:00
|
|
|
# Class derives from and derives most of it's functionality (that you would associate with a class)
|
|
|
|
# from there
|
2015-05-12 08:54:36 +02:00
|
|
|
# class is mainly a list of methods with a name (for now)
|
|
|
|
# layout of object is seperated into Layout
|
2015-04-15 10:38:46 +02:00
|
|
|
|
|
|
|
# A Class is a module that can be instantiated
|
|
|
|
|
2015-04-08 19:24:50 +02:00
|
|
|
# An Object carries the data for the instance variables it has
|
|
|
|
# The Layout lists the names of the instance variables
|
|
|
|
# The class keeps a list of instance methods, these have a name and code
|
|
|
|
|
2015-05-12 08:54:36 +02:00
|
|
|
require_relative "meta_class"
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
|
|
|
class Class < Module
|
2015-05-12 08:54:36 +02:00
|
|
|
|
2015-05-16 19:16:49 +02:00
|
|
|
def initialize name , super_class = nil
|
2015-05-12 08:54:36 +02:00
|
|
|
super()
|
|
|
|
# class methods
|
|
|
|
@instance_methods = []
|
|
|
|
@name = name.to_sym
|
2015-05-16 19:16:49 +02:00
|
|
|
@super_class = super_class
|
2015-05-12 08:54:36 +02:00
|
|
|
@meta_class = Virtual::MetaClass.new(self)
|
2015-05-16 13:01:48 +02:00
|
|
|
@object_layout = []
|
2015-05-12 08:54:36 +02:00
|
|
|
end
|
|
|
|
attr_reader :name , :instance_methods , :meta_class , :context , :super_class_name
|
|
|
|
def add_instance_method method
|
|
|
|
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? Virtual::CompiledMethod
|
|
|
|
raise "syserr " unless method.name.is_a? Symbol
|
|
|
|
@instance_methods << method
|
|
|
|
end
|
|
|
|
|
2015-05-16 19:16:49 +02:00
|
|
|
# this needs to be done during booting as we can't have all the classes and superclassses
|
|
|
|
# instantiated. By that logic it should maybe be part of vm rather.
|
|
|
|
# On the other hand vague plans to load the hierachy from sof exist, so for now...
|
|
|
|
def set_super_class sup
|
|
|
|
@super_class = sup
|
|
|
|
end
|
|
|
|
|
2015-05-16 13:01:48 +02:00
|
|
|
def set_instance_names list
|
|
|
|
@object_layout = Layout.new_object
|
|
|
|
@object_layout.set_names list
|
|
|
|
end
|
|
|
|
|
2015-05-12 08:54:36 +02:00
|
|
|
def get_instance_method fname
|
|
|
|
fname = fname.to_sym
|
|
|
|
@instance_methods.detect{ |fun| fun.name == fname }
|
|
|
|
end
|
|
|
|
|
|
|
|
# get the method and if not found, try superclasses. raise error if not found
|
|
|
|
def resolve_method m_name
|
|
|
|
method = get_instance_method(m_name)
|
|
|
|
unless method
|
2015-05-16 19:16:49 +02:00
|
|
|
unless( @name == "Object" )
|
2015-05-16 11:54:11 +02:00
|
|
|
supr = Space.space.get_class_by_name(@super_class_name)
|
2015-05-12 08:54:36 +02:00
|
|
|
method = supr.resolve_method(m_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raise "Method not found #{m_name}, for #{@name}" unless method
|
|
|
|
method
|
|
|
|
end
|
|
|
|
|
|
|
|
@@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
2015-05-17 13:41:18 +02:00
|
|
|
def old_layout
|
2015-05-12 08:54:36 +02:00
|
|
|
@@CLAZZ
|
|
|
|
end
|
|
|
|
def mem_length
|
|
|
|
padded_words(3)
|
|
|
|
end
|
|
|
|
def to_s
|
|
|
|
inspect[0...300]
|
|
|
|
end
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
# ruby 2.1 list (just for reference, keep at bottom)
|
|
|
|
#:allocate, :new, :superclass
|
|
|
|
end
|
2015-04-06 10:38:11 +02:00
|
|
|
end
|