2015-10-23 14:20:02 +02:00
|
|
|
# Class is mainly a list of methods with a name (for now)
|
2015-05-12 08:54:36 +02:00
|
|
|
# layout of object is seperated into Layout
|
2015-04-15 10:38:46 +02:00
|
|
|
|
2015-10-23 14:20:02 +02:00
|
|
|
# A class describes the capabilities of a group of objects, ie what data it has
|
|
|
|
# and functions it responds to.
|
|
|
|
#
|
|
|
|
# The class also keeps a list of class methods (with names+code)
|
|
|
|
# Class methods are instance methods on the class object
|
|
|
|
|
|
|
|
# So it is essential that the class (the object defining the class)
|
|
|
|
# can carry methods. It does so as instance variables.
|
|
|
|
# In fact this property is implemented in the Object, as methods
|
|
|
|
# may be added to any object at run-time
|
2015-04-15 10:38:46 +02:00
|
|
|
|
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
|
2015-10-23 14:20:02 +02:00
|
|
|
class Class < Object
|
2015-10-25 14:32:38 +01:00
|
|
|
attributes [:object_layout , :name , :instance_methods , :super_class_name , :meta_class]
|
2015-05-12 08:54:36 +02:00
|
|
|
|
2015-10-25 11:31:35 +01:00
|
|
|
def initialize name , superclass
|
2015-10-23 14:20:02 +02:00
|
|
|
super()
|
|
|
|
self.name = name
|
|
|
|
self.instance_methods = List.new
|
2015-10-25 14:32:38 +01:00
|
|
|
self.super_class_name = superclass
|
|
|
|
self.meta_class = MetaClass.new(self)
|
2015-05-19 19:29:33 +02:00
|
|
|
# the layout for this class (class = object of type Class) carries the class
|
|
|
|
# as an instance. The relation is from an object through the Layout to it's class
|
2015-07-21 14:40:25 +02:00
|
|
|
self.object_layout = Layout.new(self)
|
2015-05-22 21:51:36 +02:00
|
|
|
end
|
|
|
|
|
2015-05-19 19:29:33 +02:00
|
|
|
def allocate_object
|
|
|
|
#space, and ruby allocate
|
2015-05-12 08:54:36 +02:00
|
|
|
end
|
|
|
|
|
2015-10-25 14:32:38 +01:00
|
|
|
def meta
|
|
|
|
m = self.meta_class
|
|
|
|
return m if m
|
|
|
|
self.meta_class = MetaClass.new(self)
|
|
|
|
end
|
2015-05-18 11:35:01 +02:00
|
|
|
def add_instance_name name
|
2015-07-21 14:40:25 +02:00
|
|
|
self.object_layout.push name
|
2015-05-12 08:54:36 +02:00
|
|
|
end
|
|
|
|
|
2015-06-19 18:50:53 +02:00
|
|
|
def sof_reference_name
|
|
|
|
name
|
|
|
|
end
|
2015-10-13 13:46:07 +02:00
|
|
|
|
|
|
|
def inspect
|
|
|
|
"Class(#{name})"
|
|
|
|
end
|
2015-10-23 14:20:02 +02:00
|
|
|
|
|
|
|
def method_names
|
|
|
|
names = List.new
|
|
|
|
self.instance_methods.each do |method|
|
|
|
|
names.push method.name
|
|
|
|
end
|
|
|
|
names
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_instance_method method
|
|
|
|
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? Method
|
|
|
|
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
|
2015-10-25 12:19:18 +01:00
|
|
|
raise "Adding to wrong class, should be #{method.for_class}" if method.for_class != self
|
2015-10-23 14:20:02 +02:00
|
|
|
found = get_instance_method( method.name )
|
|
|
|
if found
|
|
|
|
self.instance_methods.delete(found)
|
|
|
|
#raise "existed in #{self.name} #{Sof.write found.source.blocks}"
|
|
|
|
end
|
|
|
|
self.instance_methods.push method
|
|
|
|
#puts "#{self.name} add #{method.name}"
|
|
|
|
method
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_instance_method method_name
|
|
|
|
found = get_instance_method( method_name )
|
|
|
|
if found
|
|
|
|
self.instance_methods.delete(found)
|
|
|
|
else
|
|
|
|
raise "No such method #{method_name} in #{self.name}"
|
|
|
|
end
|
2015-10-25 12:19:18 +01:00
|
|
|
return true
|
2015-10-23 14:20:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_instance_method method_name , arguments
|
|
|
|
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
|
|
|
clazz = object_layout().object_class()
|
|
|
|
raise "??? #{method_name}" unless clazz
|
|
|
|
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
2015-10-25 12:19:18 +01:00
|
|
|
add_instance_method Method.new( clazz , method_name , arguments )
|
2015-10-23 14:20:02 +02:00
|
|
|
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...
|
2015-10-25 14:32:38 +01:00
|
|
|
def set_super_class_name sup
|
|
|
|
self.super_class_name = sup
|
2015-10-23 14:20:02 +02:00
|
|
|
end
|
|
|
|
|
2015-10-25 14:32:38 +01:00
|
|
|
def super_class
|
|
|
|
Parfait::Space.object_space.get_class_by_name(self.super_class_name)
|
|
|
|
end
|
|
|
|
|
2015-10-23 14:20:02 +02:00
|
|
|
def get_instance_method fname
|
|
|
|
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
|
|
|
|
#if we had a hash this would be easier. Detect or find would help too
|
|
|
|
self.instance_methods.each do |m|
|
|
|
|
return m if(m.name == fname )
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# get the method and if not found, try superclasses. raise error if not found
|
|
|
|
def resolve_method m_name
|
|
|
|
raise "resolve_method #{m_name}.#{m_name.class}" unless m_name.is_a?(Symbol)
|
|
|
|
method = get_instance_method(m_name)
|
|
|
|
return method if method
|
2015-10-25 14:32:38 +01:00
|
|
|
if( self.super_class_name )
|
|
|
|
method = self.super_class.resolve_method(m_name)
|
2015-10-23 14:20:02 +02:00
|
|
|
raise "Method not found #{m_name}, for \n#{self}" unless method
|
|
|
|
end
|
|
|
|
method
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
# ruby 2.1 list (just for reference, keep at bottom)
|
|
|
|
#:allocate, :new, :superclass
|
2015-10-13 13:46:07 +02:00
|
|
|
|
2015-10-23 14:20:02 +02:00
|
|
|
# + modules
|
|
|
|
# :<, :<=, :>, :>=, :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,
|
|
|
|
# :class_variable_defined?, :public_constant, :private_constant, :singleton_class?, :include, :prepend,
|
|
|
|
# :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?,
|
|
|
|
# :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload,
|
|
|
|
# :autoload?, :instance_method, :public_instance_method
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2015-04-06 10:38:11 +02:00
|
|
|
end
|