2018-06-27 16:09:50 +02:00
|
|
|
# Class is mainly a list of methods with a name.
|
|
|
|
# The methods are untyped, sis VoolMethod.
|
2015-10-23 14:20:02 +02:00
|
|
|
|
2016-02-25 21:03:11 +01:00
|
|
|
# The memory layout of an object is determined by the Type (see there).
|
2018-06-27 16:09:50 +02:00
|
|
|
# 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-02-25 21:03:11 +01:00
|
|
|
|
2016-12-06 10:38:09 +01:00
|
|
|
# For dynamic OO it is essential that the class (the object defining the class)
|
2018-06-27 16:09:50 +02:00
|
|
|
# can carry methods. It does so in an instance variable methods.
|
2015-04-15 10:38:46 +02:00
|
|
|
|
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
|
2018-06-27 16:09:50 +02:00
|
|
|
# The Class keeps a list of instance methods, these have a name and (vool) code
|
2018-07-07 08:11:09 +02:00
|
|
|
# Each type in turn has a list of CallableMethods that hold binary code
|
2015-04-08 19:24:50 +02:00
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2015-10-23 14:20:02 +02:00
|
|
|
class Class < Object
|
2015-10-26 12:27:56 +01:00
|
|
|
include Behaviour
|
2015-05-22 21:51:36 +02:00
|
|
|
|
2019-09-09 23:18:20 +02:00
|
|
|
attr_reader :instance_type , :name , :instance_methods
|
2019-09-09 19:26:54 +02:00
|
|
|
attr_reader :super_class_name , :meta_class
|
2018-08-11 18:15:34 +02:00
|
|
|
|
|
|
|
def self.type_length
|
2019-02-16 22:24:16 +01:00
|
|
|
6
|
2018-08-11 18:15:34 +02:00
|
|
|
end
|
2019-09-09 23:18:20 +02:00
|
|
|
def self.memory_size
|
|
|
|
8
|
|
|
|
end
|
2016-12-29 17:47:45 +01:00
|
|
|
|
|
|
|
def initialize( name , superclass , instance_type)
|
|
|
|
super()
|
2019-09-09 19:26:54 +02:00
|
|
|
@name = name
|
|
|
|
@super_class_name = superclass
|
|
|
|
@instance_methods = List.new
|
2016-12-31 13:54:53 +01:00
|
|
|
set_instance_type( instance_type )
|
2019-09-09 19:26:54 +02:00
|
|
|
@meta_class = MetaClass.new( self )
|
2015-05-12 08:54:36 +02:00
|
|
|
end
|
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def rxf_reference_name
|
2015-06-19 18:50:53 +02:00
|
|
|
name
|
|
|
|
end
|
2015-10-13 13:46:07 +02:00
|
|
|
|
|
|
|
def inspect
|
|
|
|
"Class(#{name})"
|
|
|
|
end
|
2015-10-23 14:20:02 +02:00
|
|
|
|
2018-06-27 16:09:50 +02:00
|
|
|
def add_method_for(name , type , frame , body )
|
|
|
|
method = Parfait::VoolMethod.new(name , type , frame , body )
|
|
|
|
add_method( method )
|
|
|
|
method
|
|
|
|
end
|
|
|
|
|
2017-01-12 19:38:04 +01:00
|
|
|
def add_method(method)
|
2017-12-10 19:47:26 +01:00
|
|
|
raise "Must be untyped method #{method}" unless method.is_a? Parfait::VoolMethod
|
2019-09-10 11:33:57 +02:00
|
|
|
@instance_methods.push(method)
|
2017-01-12 19:38:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_method(name)
|
2019-09-10 11:33:57 +02:00
|
|
|
@instance_methods.find{|m| m.name == name }
|
2017-01-12 19:38:04 +01:00
|
|
|
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)
|
2019-09-10 11:33:57 +02:00
|
|
|
@instance_type = @instance_type.add_instance_variable( name , type )
|
2017-01-15 13:21:57 +01:00
|
|
|
end
|
|
|
|
|
2016-12-19 13:20:47 +01:00
|
|
|
# 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)
|
2016-12-19 13:20:47 +01:00
|
|
|
def set_instance_type( type )
|
2016-12-31 13:54:53 +01:00
|
|
|
raise "type must be type #{type}" unless type.is_a?(Type)
|
2019-09-09 19:26:54 +02:00
|
|
|
@instance_type = type
|
2015-10-23 14:20:02 +02:00
|
|
|
end
|
|
|
|
|
2017-04-25 08:06:49 +02:00
|
|
|
# return the super class, but raise exception if either the super class name
|
|
|
|
# or the super classs is nil.
|
|
|
|
# Use only for non Object base class
|
|
|
|
def super_class!
|
2019-09-10 11:33:57 +02:00
|
|
|
raise "No super_class for class #{@name}" unless @super_class_name
|
2017-04-25 08:06:49 +02:00
|
|
|
s = super_class
|
2019-09-10 11:33:57 +02:00
|
|
|
raise "superclass not found for class #{@name} (#{@super_class_name})" unless s
|
2015-11-07 23:54:24 +01:00
|
|
|
s
|
2015-10-25 14:32:38 +01:00
|
|
|
end
|
2015-10-26 11:22:32 +01:00
|
|
|
|
2017-04-25 08:06:49 +02:00
|
|
|
# return the super class
|
|
|
|
# we only store the name, and so have to resolve.
|
|
|
|
# Nil name means no superclass, and so nil is a valid return value
|
|
|
|
def super_class
|
2019-09-10 11:33:57 +02:00
|
|
|
return nil unless @super_class_name
|
|
|
|
Parfait.object_space.get_class_by_name(@super_class_name)
|
2017-04-25 08:06:49 +02:00
|
|
|
end
|
2015-10-23 14:20:02 +02:00
|
|
|
|
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
|