rubyx/lib/parfait/object.rb

117 lines
3.1 KiB
Ruby
Raw Normal View History

2016-02-25 21:03:11 +01:00
# From a programmers perspective an object has hash like data (with instance variables as keys)
# and functions to work on that data.
# Only the object may access it's data directly.
2014-08-05 14:55:24 +02:00
2016-12-06 10:38:09 +01:00
# From an implementation perspective it is a chunk of memory with a type as the first
# word (instance of class Type).
2014-08-05 14:55:24 +02:00
2016-02-25 21:03:11 +01:00
# Objects are arranged or layed out (in memory) according to their Type
# every object has a Type. Type objects are immutable and may be reused for a group/class
# of objects.
2016-02-25 21:03:11 +01:00
# The Type of an object may change, but then a new Type is created
# The Type also defines the class of the object
# The Type is **always** the first entry (index 0) in an object
2015-05-17 13:40:02 +02:00
module Parfait
class Object
attr_reader :type
2019-09-09 23:18:20 +02:00
def self.type_length
1
end
def self.memory_size
4
end
# Make the object space globally available
def self.object_space
@object_space
end
2019-09-09 23:18:20 +02:00
def self.new
factory = @object_space.get_factory(:Object)
object = factory.get_next
object.initialize
end
2019-09-09 23:18:20 +02:00
def type=(t)
set_type( t )
end
2015-05-17 13:40:02 +02:00
2015-06-03 09:01:59 +02:00
def == other
self.object_id == other.object_id
end
# This is the core of the object system.
# The class of an object is stored in the objects memory
#
2017-01-01 23:29:20 +01:00
# In RubyX we store the class in the Type, and so the Type is the only fixed
# data that every object carries.
def get_class()
l = get_type()
#puts "Type #{l.class} in #{self.class} , #{self}"
l.object_class()
end
# private
def set_type(typ)
raise "not type" + typ.class.to_s + "in " + object_id.to_s(16) unless typ.is_a?(Type)
@type = typ
end
# so we can keep the raise in get_type
def has_type?
! @type.nil?
end
def get_type()
raise "No type " + self.object_id.to_s(16) + " : " + self.class.name+" : " + self.to_s unless @type
@type
end
2015-05-20 09:57:20 +02:00
def get_instance_variables
@type.names
2015-05-14 18:53:56 +02:00
end
def get_instance_variable( name )
2015-05-14 18:53:56 +02:00
index = instance_variable_defined(name)
#raise "at :#{name}:" if name.to_s[0] == "@"
2015-05-14 18:53:56 +02:00
return nil if index == nil
return get_internal_word(index)
2015-05-14 18:53:56 +02:00
end
def set_instance_variable( name , value )
2015-05-14 18:53:56 +02:00
index = instance_variable_defined(name)
#puts "setting #{name} at #{index}"
2015-05-14 18:53:56 +02:00
return nil if index == nil
return set_internal_word(index , value)
2015-05-14 18:53:56 +02:00
end
def instance_variable_defined( name )
@type.variable_index(name)
2015-05-14 18:53:56 +02:00
end
# objects only come in lengths of multiple of 8 words / 32 bytes
# and there is a "hidden" 1 word that is used for debug/check memory corruption
def self.padded( len )
a = 32 * (1 + ((len + 3)/32).floor )
#puts "#{a} for #{len}"
return a
end
def self.padded_words( words )
padded(words*4) # 4 == word length, a constant waiting for a home
end
def padded_length
Object.padded_words( @type.instance_length )
end
# parfait versions are deliberately called different, so we "relay"
# have to put the "" on the names for rfx to take them off again
def instance_variables
get_instance_variables.to_a.collect{ |n| n.to_s.to_sym }
end
end
end