2016-02-25 12:03:11 -08: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 15:55:24 +03:00
|
|
|
|
2016-12-06 11:38:09 +02: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 15:55:24 +03:00
|
|
|
|
2016-02-25 12:03:11 -08:00
|
|
|
# Objects are arranged or layed out (in memory) according to their Type
|
|
|
|
# every object has a Type. Type objects are immutalbe and may be reused for a group/class
|
2015-05-17 14:40:02 +03:00
|
|
|
# off objects.
|
2016-02-25 12:03:11 -08: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 1) in an object
|
2015-05-17 14:40:02 +03:00
|
|
|
|
2015-05-11 18:55:49 +03:00
|
|
|
module Parfait
|
2016-02-25 12:03:11 -08:00
|
|
|
TYPE_INDEX = 1
|
2015-04-08 20:24:50 +03:00
|
|
|
|
2016-12-16 01:14:09 +02:00
|
|
|
class Object
|
2015-05-17 14:40:02 +03:00
|
|
|
|
2015-07-20 13:01:15 +03:00
|
|
|
def self.new *args
|
|
|
|
object = self.allocate
|
2016-12-06 15:08:29 +02:00
|
|
|
|
2015-07-21 15:40:25 +03:00
|
|
|
# have to grab the class, because we are in the ruby class not the parfait one
|
2016-12-30 14:10:49 +02:00
|
|
|
cl = Parfait.object_space.get_class_by_name( self.name.split("::").last.to_sym)
|
2016-12-06 15:08:29 +02:00
|
|
|
|
2016-02-25 11:50:10 -08:00
|
|
|
# and have to set the type before we let the object do anything. otherwise boom
|
2016-02-25 12:16:13 -08:00
|
|
|
object.set_type cl.instance_type
|
2015-07-21 15:40:25 +03:00
|
|
|
|
2015-07-20 13:01:15 +03:00
|
|
|
object.send :initialize , *args
|
2015-05-18 12:35:01 +03:00
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2015-11-18 11:55:29 +02:00
|
|
|
# 1 -based index
|
2015-11-18 15:36:43 +02:00
|
|
|
def get_internal_word(index)
|
2016-12-29 18:49:03 +02:00
|
|
|
name = get_type().name_at(index)
|
|
|
|
return nil unless name
|
|
|
|
eval "@#{name}"
|
2015-11-18 11:55:29 +02:00
|
|
|
end
|
2016-12-16 00:56:25 +02:00
|
|
|
|
2015-11-18 11:55:29 +02:00
|
|
|
# 1 -based index
|
2015-11-18 15:36:43 +02:00
|
|
|
def set_internal_word(index , value)
|
2016-12-29 18:49:03 +02:00
|
|
|
return set_type(value) if( index == 1)
|
|
|
|
raise "not type #{@type.class}" unless @type.is_a?(Type)
|
|
|
|
name = @type.name_at(index)
|
|
|
|
raise "object type has no name at index #{index} " unless name
|
|
|
|
eval "@#{name} = value"
|
2015-11-18 11:55:29 +02:00
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2015-06-03 10:01:59 +03:00
|
|
|
def == other
|
|
|
|
self.object_id == other.object_id
|
|
|
|
end
|
|
|
|
|
2015-05-11 18:55:49 +03:00
|
|
|
# This is the crux of the object system. The class of an object is stored in the objects
|
|
|
|
# memory (as opposed to an integer that has no memory and so always has the same class)
|
|
|
|
#
|
2017-01-02 00:29:20 +02:00
|
|
|
# In RubyX we store the class in the Type, and so the Type is the only fixed
|
2015-05-11 18:55:49 +03:00
|
|
|
# data that every object carries.
|
|
|
|
def get_class()
|
2016-02-25 11:50:10 -08:00
|
|
|
l = get_type()
|
|
|
|
#puts "Type #{l.class} in #{self.class} , #{self}"
|
2015-07-21 15:40:25 +03:00
|
|
|
l.object_class()
|
2015-05-11 18:55:49 +03:00
|
|
|
end
|
2015-04-08 20:24:50 +03:00
|
|
|
|
2015-05-22 22:51:36 +03:00
|
|
|
# private
|
2016-02-25 11:50:10 -08:00
|
|
|
def set_type(type)
|
|
|
|
# puts "Type was set for #{self.class}"
|
2016-12-29 18:49:03 +02:00
|
|
|
raise "not type #{type.class}" unless type.is_a?(Type)
|
|
|
|
@type = type
|
2015-05-22 22:51:36 +03:00
|
|
|
end
|
|
|
|
|
2016-02-25 11:50:10 -08:00
|
|
|
# so we can keep the raise in get_type
|
|
|
|
def has_type?
|
2016-12-29 18:49:03 +02:00
|
|
|
! @type.nil?
|
2015-05-31 13:02:29 +03:00
|
|
|
end
|
|
|
|
|
2016-02-25 11:50:10 -08:00
|
|
|
def get_type()
|
2016-12-29 18:49:03 +02:00
|
|
|
raise "No type #{self.object_id.to_s(16)}:#{self.class} " unless has_type?
|
|
|
|
@type
|
2015-05-16 12:53:10 +03:00
|
|
|
end
|
|
|
|
|
2015-11-07 00:12:17 +02:00
|
|
|
# return the metaclass
|
|
|
|
def meta
|
|
|
|
MetaClass.new self
|
|
|
|
end
|
|
|
|
|
2015-05-20 10:57:20 +03:00
|
|
|
def get_instance_variables
|
2016-12-29 18:49:03 +02:00
|
|
|
@type.names
|
2015-05-14 19:53:56 +03:00
|
|
|
end
|
|
|
|
|
2016-12-29 18:49:03 +02:00
|
|
|
def get_instance_variable( name )
|
2015-05-14 19:53:56 +03:00
|
|
|
index = instance_variable_defined(name)
|
2015-07-21 15:40:25 +03:00
|
|
|
#puts "getting #{name} at #{index}"
|
2015-05-14 19:53:56 +03:00
|
|
|
return nil if index == nil
|
2015-11-18 15:36:43 +02:00
|
|
|
return get_internal_word(index)
|
2015-05-14 19:53:56 +03:00
|
|
|
end
|
|
|
|
|
2016-12-29 18:49:03 +02:00
|
|
|
def set_instance_variable( name , value )
|
2015-05-14 19:53:56 +03:00
|
|
|
index = instance_variable_defined(name)
|
|
|
|
return nil if index == nil
|
2015-11-18 15:36:43 +02:00
|
|
|
return set_internal_word(index , value)
|
2015-05-14 19:53:56 +03:00
|
|
|
end
|
|
|
|
|
2016-12-29 18:49:03 +02:00
|
|
|
def instance_variable_defined( name )
|
|
|
|
@type.variable_index(name)
|
2015-05-14 19:53:56 +03:00
|
|
|
end
|
|
|
|
|
2015-11-04 10:33:10 +02:00
|
|
|
def padded_length
|
2016-12-31 20:08:33 +02:00
|
|
|
Padding.padded_words( @type.instance_length )
|
2015-05-24 18:05:20 +03:00
|
|
|
end
|
|
|
|
|
2015-11-18 11:55:29 +02:00
|
|
|
# parfait versions are deliberately called different, so we "relay"
|
|
|
|
# have to put the "@" on the names for sof to take them off again
|
|
|
|
def instance_variables
|
|
|
|
get_instance_variables.to_a.collect{ |n| "@#{n}".to_sym }
|
|
|
|
end
|
2016-12-07 23:35:51 +02:00
|
|
|
|
2015-11-18 11:55:29 +02:00
|
|
|
# name comes in as a ruby @var name
|
|
|
|
def instance_variable_get name
|
|
|
|
var = get_instance_variable name.to_s[1 .. -1].to_sym
|
|
|
|
#puts "getting #{name} #{var}"
|
|
|
|
var
|
|
|
|
end
|
|
|
|
|
2015-05-11 18:55:49 +03:00
|
|
|
end
|
2014-07-30 21:43:12 +03:00
|
|
|
end
|