shift code from class to module

as class derives from module, module carries
much of the code that one thinks of as class
class only handles allocation really
This commit is contained in:
Torsten Ruger 2015-05-18 12:35:01 +03:00
parent c983b1cba5
commit 4e3640e432
6 changed files with 51 additions and 50 deletions

View File

@ -16,49 +16,13 @@ module Parfait
class Class < Module
def initialize name , super_class = nil
super()
super( name , super_class)
# class methods
@instance_methods = []
@name = name.to_sym
@super_class = super_class
@meta_class = Virtual::MetaClass.new(self)
@object_layout = []
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
# 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
def set_instance_names list
@object_layout = Layout.new_object
@object_layout.set_names list
end
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
unless( @name == "Object" )
supr = Space.space.get_class_by_name(@super_class_name)
method = supr.resolve_method(m_name)
end
end
raise "Method not found #{m_name}, for #{@name}" unless method
method
def add_instance_name name
@object_layout.push name
end
@@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}

View File

@ -13,6 +13,45 @@
module Parfait
class Module < Object
def initialize name , superclass
@instance_methods = []
@name = name.to_sym
@super_class = superclass
@meta_class = Virtual::MetaClass.new(self)
end
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
# 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
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
unless( @name == "Object" )
supr = Space.space.get_class_by_name(@super_class_name)
method = supr.resolve_method(m_name)
end
end
raise "Method not found #{m_name}, for #{@name}" unless method
method
end
# :<, :<=, :>, :>=, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods,
# :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?,
# :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set,

View File

@ -17,10 +17,15 @@ module Parfait
LAYOUT_INDEX = 1
CLASS_INDEX = 2 #only used in class, but keep constants together
def self.new_object *args
object = self.new(*args)
object
end
def get_type_of( index )
type_word = internal_object_get( TYPE_WORD )
res = type_word >> (index*4)
# least significant nibble, this is still adhoc, not test. but the idea is there
# least significant nibble, this is still adhoc, not tested. but the idea is there
res & 0xF
end
@ -31,7 +36,7 @@ module Parfait
# data that every object carries.
def get_class()
l = get_layout()
puts "Layout #{l}"
puts "Layout #{l.class} in #{self.class}"
l.get_object_class()
end

View File

@ -58,9 +58,8 @@ module Parfait
c
end
def create_class name , variable_names
def create_class name
c = Class.new_object(name)
c.set_instance_names Virtual.new_list(variable_names)
@classes[name] = c
end

View File

@ -16,12 +16,6 @@ module Parfait
# These are the same functions that Builtin implements at run-time
class Object
include FakeMem
def self.new_object *args
# Space.space.get_class_by_name(:Word)
#puts "I am #{self}"
object = self.new(*args)
object
end
# these internal functions are _really_ internal
# they respresent the smallest code needed to build larger functionality
# but should _never_ be used outside parfait. in fact that should be impossible

View File

@ -134,7 +134,7 @@ module Virtual
def boot_classes!
values = [ "Integer" , "Object" , "Value" , "Kernel"]
rest = ["Word" , "Class" , "Dictionary" , "Space" , "List", "Layout"]
(values + rest).each { |cl| @space.create_class(cl , []) }
(values + rest).each { |cl| @space.create_class(cl) }
value_class = @space.get_class_by_name "Value"
@space.get_class_by_name("Integer").set_super_class( value_class )
object_class = @space.get_class_by_name("Object")