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
|
2018-08-11 18:15:34 +02:00
|
|
|
# every object has a Type. Type objects are immutable and may be reused for a group/class
|
2018-05-14 10:55:01 +02:00
|
|
|
# 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
|
2018-05-14 10:55:01 +02:00
|
|
|
# The Type is **always** the first entry (index 0) in an object
|
2015-05-17 13:40:02 +02:00
|
|
|
|
2019-09-09 10:54:45 +02:00
|
|
|
module Parfait
|
2016-12-16 00:14:09 +01:00
|
|
|
class Object
|
2019-09-09 19:26:54 +02:00
|
|
|
attr_reader :type
|
2019-09-09 23:18:20 +02:00
|
|
|
def self.type_length
|
|
|
|
1
|
|
|
|
end
|
|
|
|
def self.memory_size
|
|
|
|
4
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-09-09 19:26:54 +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
|
|
|
|
|
2018-08-11 18:15:34 +02:00
|
|
|
# This is the core of the object system.
|
|
|
|
# The class of an object is stored in the objects memory
|
2015-05-11 17:55:49 +02:00
|
|
|
#
|
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
|
2015-05-11 17:55:49 +02:00
|
|
|
# data that every object carries.
|
|
|
|
def get_class()
|
2016-02-25 20:50:10 +01:00
|
|
|
l = get_type()
|
|
|
|
#puts "Type #{l.class} in #{self.class} , #{self}"
|
2015-07-21 14:40:25 +02:00
|
|
|
l.object_class()
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2015-04-08 19:24:50 +02:00
|
|
|
|
2015-05-22 21:51:36 +02:00
|
|
|
# private
|
2018-08-11 18:15:34 +02:00
|
|
|
def set_type(typ)
|
2019-02-10 20:00:25 +01:00
|
|
|
raise "not type" + typ.class.to_s + "in " + object_id.to_s(16) unless typ.is_a?(Type)
|
2019-09-09 19:26:54 +02:00
|
|
|
@type = typ
|
2015-05-22 21:51:36 +02:00
|
|
|
end
|
|
|
|
|
2016-02-25 20:50:10 +01:00
|
|
|
# so we can keep the raise in get_type
|
|
|
|
def has_type?
|
2019-09-09 19:26:54 +02:00
|
|
|
! @type.nil?
|
2015-05-31 12:02:29 +02:00
|
|
|
end
|
|
|
|
|
2016-02-25 20:50:10 +01:00
|
|
|
def get_type()
|
2019-02-10 20:00:25 +01:00
|
|
|
raise "No type " + self.object_id.to_s(16) + ":" + self.class.name unless has_type?
|
2019-09-09 19:26:54 +02:00
|
|
|
@type
|
2015-05-16 11:53:10 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 09:57:20 +02:00
|
|
|
def get_instance_variables
|
2019-09-09 19:26:54 +02:00
|
|
|
@type.names
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2016-12-29 17:49:03 +01:00
|
|
|
def get_instance_variable( name )
|
2015-05-14 18:53:56 +02:00
|
|
|
index = instance_variable_defined(name)
|
2019-09-09 19:26:54 +02:00
|
|
|
#raise "at :#{name}:" if name.to_s[0] == "@"
|
2015-05-14 18:53:56 +02:00
|
|
|
return nil if index == nil
|
2015-11-18 14:36:43 +01:00
|
|
|
return get_internal_word(index)
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2016-12-29 17:49:03 +01:00
|
|
|
def set_instance_variable( name , value )
|
2015-05-14 18:53:56 +02:00
|
|
|
index = instance_variable_defined(name)
|
2019-09-09 19:26:54 +02:00
|
|
|
#puts "setting #{name} at #{index}"
|
2015-05-14 18:53:56 +02:00
|
|
|
return nil if index == nil
|
2015-11-18 14:36:43 +01:00
|
|
|
return set_internal_word(index , value)
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2016-12-29 17:49:03 +01:00
|
|
|
def instance_variable_defined( name )
|
2019-09-09 19:26:54 +02:00
|
|
|
@type.variable_index(name)
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2019-08-17 20:07:07 +02:00
|
|
|
# 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}"
|
2019-08-17 22:29:42 +02:00
|
|
|
return a
|
2019-08-17 20:07:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.padded_words( words )
|
|
|
|
padded(words*4) # 4 == word length, a constant waiting for a home
|
|
|
|
end
|
|
|
|
|
2015-11-04 09:33:10 +01:00
|
|
|
def padded_length
|
2019-09-09 19:26:54 +02:00
|
|
|
Object.padded_words( @type.instance_length )
|
2015-05-24 17:05:20 +02:00
|
|
|
end
|
|
|
|
|
2015-11-18 10:55:29 +01:00
|
|
|
# parfait versions are deliberately called different, so we "relay"
|
2018-08-11 18:15:34 +02:00
|
|
|
# have to put the "" on the names for rfx to take them off again
|
2015-11-18 10:55:29 +01:00
|
|
|
def instance_variables
|
2019-02-10 20:00:25 +01:00
|
|
|
get_instance_variables.to_a.collect{ |n| n.to_s.to_sym }
|
2015-11-18 10:55:29 +01:00
|
|
|
end
|
2016-12-07 22:35:51 +01:00
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
end
|
2019-09-09 10:54:45 +02:00
|
|
|
end
|