rubyx/lib/parfait/class.rb

80 lines
2.8 KiB
Ruby
Raw Normal View History

2016-12-06 10:38:09 +01:00
# Class is mainly a list of methods with a name. The methods are untyped.
2016-02-25 21:03:11 +01:00
# The memory layout of an object is determined by the Type (see there).
# The class carries the "current" type, ie the type an object would be if you created an instance
# of the class. Note that this changes over time and so many types share the same class.
2016-12-06 10:38:09 +01:00
# For dynamic OO it is essential that the class (the object defining the class)
# can carry methods. It does so as instance variables.
2016-02-25 21:03:11 +01:00
# In fact this property is implemented in the Type, as methods
2016-12-06 10:38:09 +01:00
# may be added to any object at run-time.
2016-12-06 10:38:09 +01:00
# An Object carries the data for the instance variables it has.
2016-12-06 14:08:29 +01:00
# The Type lists the names of the instance variables
# The Class keeps a list of instance methods, these have a name and code
module Parfait
class Class < Object
include Behaviour
attr_reader :instance_type , :name , :instance_methods , :super_class_name
def initialize( name , superclass , instance_type)
super()
@name = name
@super_class_name = superclass
2017-01-12 19:38:04 +01:00
@methods = {}
2016-12-31 13:54:53 +01:00
set_instance_type( instance_type )
end
2015-06-19 18:50:53 +02:00
def sof_reference_name
name
end
def inspect
"Class(#{name})"
end
2017-01-12 19:38:04 +01:00
def add_method(method)
@methods[method.name] = method
end
def get_method(name)
@methods[name]
end
2017-01-15 13:21:57 +01:00
# adding an instance changes the instance_type to include that variable
def add_instance_variable( name , type)
@instance_type = @instance_type.add_instance_variable( name , type )
end
# setting the type generates all methods for this type
2016-12-31 13:54:53 +01:00
# (or will do, once we store the methods code to do that)
def set_instance_type( type )
2016-12-31 13:54:53 +01:00
raise "type must be type #{type}" unless type.is_a?(Type)
@instance_type = type
end
def super_class
raise "No super_class for class #{@name}" unless @super_class_name
s = Parfait.object_space.get_class_by_name(@super_class_name)
raise "superclass not found for class #{@name} (#{@super_class_name})" unless s
s
end
# ruby 2.1 list (just for reference, keep at bottom)
#:allocate, :new, :superclass
# + 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
end
2015-04-06 10:38:11 +02:00
end