2015-04-15 10:38:46 +02:00
|
|
|
|
|
|
|
# A module describes the capabilities of a group of objects, ie what data it has
|
2015-05-11 17:55:49 +02:00
|
|
|
# and functions it responds to.
|
2015-04-15 10:38:46 +02:00
|
|
|
# The group may be a Class, but a module may be included into classes and objects too.
|
|
|
|
#
|
|
|
|
# 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-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2015-05-12 17:52:01 +02:00
|
|
|
class Module < Object
|
2015-05-18 11:35:01 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
# :<, :<=, :>, :>=, :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-15 10:38:46 +02:00
|
|
|
end
|