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 class Class < Module
def initialize name , super_class = nil def initialize name , super_class = nil
super() super( name , super_class)
# class methods # 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 = Layout.new_object
@object_layout.set_names list
end end
def get_instance_method fname def add_instance_name name
fname = fname.to_sym @object_layout.push name
@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 end
@@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]} @@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}

View File

@ -13,6 +13,45 @@
module Parfait module Parfait
class Module < Object 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, # :<, :<=, :>, :>=, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods,
# :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, # :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, # :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set,

View File

@ -17,10 +17,15 @@ module Parfait
LAYOUT_INDEX = 1 LAYOUT_INDEX = 1
CLASS_INDEX = 2 #only used in class, but keep constants together 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 ) def get_type_of( index )
type_word = internal_object_get( TYPE_WORD ) type_word = internal_object_get( TYPE_WORD )
res = type_word >> (index*4) 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 res & 0xF
end end
@ -31,7 +36,7 @@ module Parfait
# data that every object carries. # data that every object carries.
def get_class() def get_class()
l = get_layout() l = get_layout()
puts "Layout #{l}" puts "Layout #{l.class} in #{self.class}"
l.get_object_class() l.get_object_class()
end end

View File

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

View File

@ -16,12 +16,6 @@ module Parfait
# These are the same functions that Builtin implements at run-time # These are the same functions that Builtin implements at run-time
class Object class Object
include FakeMem 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 # these internal functions are _really_ internal
# they respresent the smallest code needed to build larger functionality # they respresent the smallest code needed to build larger functionality
# but should _never_ be used outside parfait. in fact that should be impossible # but should _never_ be used outside parfait. in fact that should be impossible

View File

@ -134,7 +134,7 @@ module Virtual
def boot_classes! def boot_classes!
values = [ "Integer" , "Object" , "Value" , "Kernel"] values = [ "Integer" , "Object" , "Value" , "Kernel"]
rest = ["Word" , "Class" , "Dictionary" , "Space" , "List", "Layout"] 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" value_class = @space.get_class_by_name "Value"
@space.get_class_by_name("Integer").set_super_class( value_class ) @space.get_class_by_name("Integer").set_super_class( value_class )
object_class = @space.get_class_by_name("Object") object_class = @space.get_class_by_name("Object")