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-07-21 14:40:25 +02:00
|
|
|
attribute :name
|
|
|
|
attribute :instance_methods
|
|
|
|
attribute :super_class
|
|
|
|
attribute :meta_class
|
|
|
|
|
2015-05-18 11:35:01 +02:00
|
|
|
def initialize name , superclass
|
2015-05-30 10:55:46 +02:00
|
|
|
super()
|
2015-07-21 14:40:25 +02:00
|
|
|
self.name = name
|
|
|
|
self.instance_methods = List.new
|
|
|
|
self.super_class = superclass
|
|
|
|
self.meta_class = nil#MetaClass.new(self)
|
2015-05-20 16:11:13 +02:00
|
|
|
end
|
|
|
|
|
2015-05-24 12:53:49 +02:00
|
|
|
|
2015-05-31 17:34:18 +02:00
|
|
|
def method_names
|
|
|
|
names = List.new
|
2015-07-21 14:40:25 +02:00
|
|
|
self.instance_methods.each do |method|
|
2015-05-31 17:34:18 +02:00
|
|
|
names.push method.name
|
|
|
|
end
|
2015-06-07 10:06:08 +02:00
|
|
|
names
|
2015-05-31 17:34:18 +02:00
|
|
|
end
|
|
|
|
|
2015-05-18 11:35:01 +02:00
|
|
|
def add_instance_method method
|
2015-05-20 15:43:26 +02:00
|
|
|
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? Method
|
2015-05-31 17:34:18 +02:00
|
|
|
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
|
2015-05-30 13:47:56 +02:00
|
|
|
found = get_instance_method( method.name )
|
|
|
|
if found
|
2015-07-21 14:40:25 +02:00
|
|
|
self.instance_methods.delete(found)
|
2015-07-19 09:35:45 +02:00
|
|
|
#raise "existed in #{self.name} #{Sof.write found.source.blocks}"
|
2015-05-30 13:47:56 +02:00
|
|
|
end
|
2015-07-21 14:40:25 +02:00
|
|
|
self.instance_methods.push method
|
2015-05-24 17:05:20 +02:00
|
|
|
#puts "#{self.name} add #{method.name}"
|
2015-05-20 15:43:26 +02:00
|
|
|
method
|
|
|
|
end
|
|
|
|
|
2015-08-06 17:55:08 +02:00
|
|
|
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
|
|
|
|
self.instance_methods.delete found
|
2015-05-31 10:07:49 +02:00
|
|
|
end
|
|
|
|
|
2015-09-27 13:30:41 +02:00
|
|
|
def create_instance_method method_name , arguments
|
2015-08-17 01:37:07 +02:00
|
|
|
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
2015-07-28 15:19:10 +02:00
|
|
|
clazz = object_layout().object_class()
|
2015-07-21 14:40:25 +02:00
|
|
|
raise "??? #{method_name}" unless clazz
|
2015-07-30 18:18:41 +02:00
|
|
|
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
2015-09-27 13:30:41 +02:00
|
|
|
Method.new( clazz , method_name , arguments )
|
2015-05-18 11:35:01 +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...
|
|
|
|
def set_super_class sup
|
2015-07-21 14:40:25 +02:00
|
|
|
self.super_class = sup
|
2015-05-18 11:35:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_instance_method fname
|
2015-08-17 01:37:07 +02:00
|
|
|
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
|
2015-05-30 13:47:56 +02:00
|
|
|
#if we had a hash this would be easier. Detect or find would help too
|
2015-07-21 14:40:25 +02:00
|
|
|
self.instance_methods.each do |m|
|
2015-05-30 13:47:56 +02:00
|
|
|
return m if(m.name == fname )
|
|
|
|
end
|
|
|
|
nil
|
2015-05-18 11:35:01 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# get the method and if not found, try superclasses. raise error if not found
|
|
|
|
def resolve_method m_name
|
2015-08-17 01:37:07 +02:00
|
|
|
raise "resolve_method #{m_name}.#{m_name.class}" unless m_name.is_a?(Symbol)
|
2015-05-18 11:35:01 +02:00
|
|
|
method = get_instance_method(m_name)
|
2015-05-24 12:53:49 +02:00
|
|
|
return method if method
|
2015-07-21 14:40:25 +02:00
|
|
|
if( self.super_class )
|
|
|
|
method = self.super_class.resolve_method(m_name)
|
2015-05-24 12:53:49 +02:00
|
|
|
raise "Method not found #{m_name}, for \n#{self}" unless method
|
2015-05-18 11:35:01 +02:00
|
|
|
end
|
|
|
|
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
|