2015-04-06 10:38:11 +02:00
|
|
|
|
2018-08-29 20:02:49 +02:00
|
|
|
# The Space is the root object we work off, the only singleton in the parfait world
|
|
|
|
#
|
|
|
|
# Space stores the types, classes, factories and singleton objects (true/false/nil)
|
|
|
|
#
|
|
|
|
# The Space is booted at compile time, a process outside the scope of Parfait(in parfait_boot)
|
|
|
|
# Then it is used during compilation and later serialized into the resulting binary
|
|
|
|
#
|
|
|
|
#
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2016-12-30 13:10:49 +01:00
|
|
|
# Make the object space globally available
|
|
|
|
def self.object_space
|
2018-08-11 18:15:34 +02:00
|
|
|
@object_space
|
2016-12-30 13:10:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO Must get rid of the setter (move the boot process ?)
|
2018-03-25 18:33:50 +02:00
|
|
|
def self.set_object_space( space )
|
2018-08-11 18:15:34 +02:00
|
|
|
@object_space = space
|
2016-12-30 13:10:49 +01:00
|
|
|
end
|
|
|
|
|
2015-05-12 14:36:44 +02:00
|
|
|
# The Space contains all objects for a program. In functional terms it is a program, but in oo
|
|
|
|
# it is a collection of objects, some of which are data, some classes, some functions
|
|
|
|
|
2015-05-30 11:20:39 +02:00
|
|
|
# The main entry is a function called (of all things) "main".
|
|
|
|
# This _must be supplied by the compled code (similar to c)
|
2015-05-12 18:10:45 +02:00
|
|
|
# There is a start and exit block that call main, which receives an List of strings
|
2015-05-12 14:36:44 +02:00
|
|
|
|
|
|
|
# While data ususally would live in a .data section, we may also "inline" it into the code
|
|
|
|
# in an oo system all data is represented as objects
|
|
|
|
|
2018-04-02 16:06:31 +02:00
|
|
|
class Space < Object
|
2015-04-08 19:24:50 +02:00
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
attr :type, :classes , :types , :factories
|
2018-08-29 20:02:49 +02:00
|
|
|
attr :next_message , :messages
|
2018-08-11 18:15:34 +02:00
|
|
|
attr :true_object , :false_object , :nil_object
|
|
|
|
|
2018-03-25 18:33:50 +02:00
|
|
|
def initialize( classes )
|
2018-08-11 18:15:34 +02:00
|
|
|
self.classes = classes
|
|
|
|
self.types = Dictionary.new
|
|
|
|
classes.each do |name , cl|
|
2016-12-31 13:51:06 +01:00
|
|
|
add_type(cl.instance_type)
|
2016-12-07 22:34:45 +01:00
|
|
|
end
|
2018-08-24 17:49:21 +02:00
|
|
|
self.factories = Dictionary.new
|
2018-08-29 20:02:49 +02:00
|
|
|
factories[ :Integer ] = Factory.new( classes[:Integer].instance_type).get_more
|
|
|
|
factories[ :ReturnAddress ] = Factory.new( classes[:ReturnAddress].instance_type).get_more
|
2018-07-02 14:49:51 +02:00
|
|
|
message = Message.new(nil)
|
|
|
|
50.times do
|
2018-08-11 18:15:34 +02:00
|
|
|
self.messages = Message.new( message )
|
|
|
|
message.set_caller( self.messages )
|
|
|
|
message = self.messages
|
2018-07-02 14:49:51 +02:00
|
|
|
end
|
2018-08-11 18:15:34 +02:00
|
|
|
self.next_message = self.messages
|
|
|
|
self.true_object = Parfait::TrueClass.new
|
|
|
|
self.false_object = Parfait::FalseClass.new
|
|
|
|
self.nil_object = Parfait::NilClass.new
|
2015-05-12 14:36:44 +02:00
|
|
|
end
|
|
|
|
|
2018-08-11 18:15:34 +02:00
|
|
|
def self.type_length
|
|
|
|
12
|
|
|
|
end
|
|
|
|
def self.memory_size
|
|
|
|
16
|
|
|
|
end
|
2016-12-29 17:51:24 +01:00
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
# return the factory for the given type
|
|
|
|
# or more exactly the type that has a class_name "name"
|
|
|
|
def get_factory_for(name)
|
|
|
|
factories[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
# use the factory of given name to generate next_object
|
|
|
|
# just a shortcut basically
|
|
|
|
def get_next_for(name)
|
|
|
|
factories[name].get_next_object
|
2018-05-29 16:03:55 +02:00
|
|
|
end
|
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
# yield each type in the space
|
2016-12-30 13:04:59 +01:00
|
|
|
def each_type
|
2018-08-11 18:15:34 +02:00
|
|
|
types.values.each do |type|
|
2016-12-30 13:04:59 +01:00
|
|
|
yield(type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
# add a type, meaning the instance given must be a valid type
|
2018-03-25 18:33:50 +02:00
|
|
|
def add_type( type )
|
2016-12-30 19:47:28 +01:00
|
|
|
hash = type.hash
|
2016-12-31 14:10:07 +01:00
|
|
|
raise "upps #{hash} #{hash.class}" unless hash.is_a?(Fixnum)
|
2018-08-11 18:15:34 +02:00
|
|
|
was = types[hash]
|
2016-12-30 19:47:28 +01:00
|
|
|
return was if was
|
2018-08-11 18:15:34 +02:00
|
|
|
types[hash] = type
|
2016-12-30 19:47:28 +01:00
|
|
|
end
|
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
# get a type by the type hash (the hash is what uniquely identifies the type)
|
2016-12-30 19:47:28 +01:00
|
|
|
def get_type_for( hash )
|
2018-08-11 18:15:34 +02:00
|
|
|
types[hash]
|
2016-12-30 19:47:28 +01:00
|
|
|
end
|
|
|
|
|
2016-12-30 13:04:59 +01:00
|
|
|
# all methods form all types
|
2018-05-01 18:19:37 +02:00
|
|
|
def get_all_methods
|
2016-12-30 13:04:59 +01:00
|
|
|
methods = []
|
|
|
|
each_type do | type |
|
2018-04-02 15:36:43 +02:00
|
|
|
type.each_method do |meth|
|
2016-12-30 13:04:59 +01:00
|
|
|
methods << meth
|
|
|
|
end
|
|
|
|
end
|
|
|
|
methods
|
|
|
|
end
|
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
# shortcut to get the main method. main is defined on Space
|
2015-05-25 17:48:35 +02:00
|
|
|
def get_main
|
2018-04-02 16:06:31 +02:00
|
|
|
space = get_class_by_name :Space
|
|
|
|
space.instance_type.get_method :main
|
2015-05-25 17:48:35 +02:00
|
|
|
end
|
|
|
|
|
2018-08-24 17:49:21 +02:00
|
|
|
# shortcut to get the __init__ method, which is defined on Object
|
2015-06-25 15:31:09 +02:00
|
|
|
def get_init
|
2018-04-02 16:06:31 +02:00
|
|
|
object = get_class_by_name :Object
|
|
|
|
object.instance_type.get_method :__init__
|
2015-06-25 15:31:09 +02:00
|
|
|
end
|
|
|
|
|
2018-07-13 20:50:40 +02:00
|
|
|
# get the current instance_typ of the class with the given name
|
|
|
|
def get_type_by_class_name(name)
|
2018-07-15 11:32:02 +02:00
|
|
|
clazz = get_class_by_name(name)
|
|
|
|
return nil unless clazz
|
|
|
|
clazz.instance_type
|
2018-07-13 20:50:40 +02:00
|
|
|
end
|
|
|
|
|
2015-07-19 09:36:06 +02:00
|
|
|
# get a class by name (symbol)
|
|
|
|
# return nili if no such class. Use bang version if create should be implicit
|
2016-12-14 12:21:55 +01:00
|
|
|
def get_class_by_name( name )
|
2015-08-17 01:37:07 +02:00
|
|
|
raise "get_class_by_name #{name}.#{name.class}" unless name.is_a?(Symbol)
|
2018-08-11 18:15:34 +02:00
|
|
|
c = classes[name]
|
|
|
|
#puts "MISS, no class #{name} #{name.class}" unless c # " #{classes}"
|
2016-02-25 20:50:10 +01:00
|
|
|
#puts "CLAZZ, #{name} #{c.get_type.get_length}" if c
|
2015-05-12 14:36:44 +02:00
|
|
|
c
|
|
|
|
end
|
2015-05-16 13:01:48 +02:00
|
|
|
|
2015-07-19 09:36:06 +02:00
|
|
|
# get or create the class by the (symbol) name
|
|
|
|
# notice that this method of creating classes implies Object superclass
|
2017-01-12 19:38:04 +01:00
|
|
|
def get_class_by_name!(name , super_class = :Object)
|
2015-07-19 09:36:06 +02:00
|
|
|
c = get_class_by_name(name)
|
|
|
|
return c if c
|
2017-01-12 19:38:04 +01:00
|
|
|
create_class( name ,super_class)
|
2015-07-19 09:36:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# this is the way to instantiate classes (not Parfait::Class.new)
|
|
|
|
# so we get and keep exactly one per name
|
2016-12-29 17:51:24 +01:00
|
|
|
def create_class( name , superclass = nil )
|
2015-08-17 01:37:07 +02:00
|
|
|
raise "create_class #{name.class}" unless name.is_a? Symbol
|
2016-12-18 16:02:55 +01:00
|
|
|
superclass = :Object unless superclass
|
2016-12-29 17:51:24 +01:00
|
|
|
raise "create_class #{superclass.class}" unless superclass.is_a? Symbol
|
2018-07-13 20:50:40 +02:00
|
|
|
type = get_type_by_class_name(superclass)
|
2016-12-29 17:51:24 +01:00
|
|
|
c = Class.new(name , superclass , type )
|
2018-08-11 18:15:34 +02:00
|
|
|
classes[name] = c
|
2015-05-16 13:01:48 +02:00
|
|
|
end
|
2015-06-19 18:50:53 +02:00
|
|
|
|
2018-05-14 10:55:01 +02:00
|
|
|
def rxf_reference_name
|
2015-06-19 18:50:53 +02:00
|
|
|
"space"
|
|
|
|
end
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2015-05-12 14:36:44 +02:00
|
|
|
# ObjectSpace
|
|
|
|
# :each_object, :garbage_collect, :define_finalizer, :undefine_finalizer, :_id2ref, :count_objects
|
2015-04-06 10:38:11 +02:00
|
|
|
end
|