2015-04-08 19:24:50 +02:00
|
|
|
# to be precise, this should be an ObjectReference, as the Reference is a Value
|
|
|
|
# but we don't want to make that distinction all the time , so we don't.
|
2014-08-05 14:55:24 +02:00
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
# that does lead to the fact that we have Reference functions on the Object though
|
2014-08-05 14:55:24 +02:00
|
|
|
|
2015-05-17 13:40:02 +02:00
|
|
|
# Objects are arranged or layed out (in memory) according to their Layout
|
|
|
|
# every object has a Layout. Layout objects are immutalbe and may be resued for a group/class
|
|
|
|
# off objects.
|
|
|
|
# The Layout of an object may change, but then a new Layout is created
|
|
|
|
# The Layout also defines the class of the object
|
|
|
|
# The Layout is **always** the first entry (index 1) in an object, but the type word is index 0
|
|
|
|
|
2015-05-11 17:55:49 +02:00
|
|
|
module Parfait
|
2015-10-26 13:33:36 +01:00
|
|
|
LAYOUT_INDEX = 1
|
2015-04-08 19:24:50 +02:00
|
|
|
|
2015-10-26 13:33:36 +01:00
|
|
|
class Object < Value
|
2015-05-17 13:40:02 +02:00
|
|
|
|
2015-07-20 12:01:15 +02:00
|
|
|
def self.new *args
|
|
|
|
object = self.allocate
|
2015-07-21 14:40:25 +02:00
|
|
|
#HACK, but used to do the adapter in the init, bu that is too late now
|
|
|
|
object.fake_init if object.respond_to?(:fake_init) # at compile, not run-time
|
|
|
|
# have to grab the class, because we are in the ruby class not the parfait one
|
|
|
|
cl = Space.object_space.get_class_by_name( self.name.split("::").last.to_sym)
|
|
|
|
# and have to set the layout before we let the object do anything. otherwise boom
|
|
|
|
object.set_layout cl.object_layout
|
|
|
|
|
2015-07-20 12:01:15 +02:00
|
|
|
object.send :initialize , *args
|
2015-05-18 11:35:01 +02:00
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2015-11-18 10:55:29 +01:00
|
|
|
# Objects memory functions. Object memory is 1 based
|
|
|
|
# but we implement it with ruby array (0 based) and don't use 0
|
|
|
|
# These are the same functions that Builtin implements for run-time
|
|
|
|
include Padding
|
|
|
|
include Positioned
|
|
|
|
|
|
|
|
def fake_init
|
|
|
|
@memory = Array.new(16)
|
|
|
|
@position = nil
|
|
|
|
self # for chaining
|
|
|
|
end
|
|
|
|
|
|
|
|
# 1 -based index
|
|
|
|
def get_internal(index)
|
|
|
|
@memory[index]
|
|
|
|
end
|
|
|
|
# 1 -based index
|
|
|
|
def set_internal(index , value)
|
|
|
|
raise "failed init for #{self.class}" unless @memory
|
|
|
|
raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? )
|
|
|
|
@memory[index] = value
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2015-07-21 14:40:25 +02:00
|
|
|
def self.attributes names
|
|
|
|
names.each{|name| attribute(name) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.attribute name
|
|
|
|
define_method(name) { get_instance_variable(name) }
|
|
|
|
define_method("#{name}=".to_sym) { |value| set_instance_variable(name , value) }
|
|
|
|
end
|
|
|
|
|
2015-06-03 09:01:59 +02:00
|
|
|
def == other
|
|
|
|
self.object_id == other.object_id
|
|
|
|
end
|
|
|
|
|
2015-05-11 17:55:49 +02: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)
|
|
|
|
#
|
|
|
|
# In Salama we store the class in the Layout, and so the Layout is the only fixed
|
|
|
|
# data that every object carries.
|
|
|
|
def get_class()
|
2015-05-16 11:53:10 +02:00
|
|
|
l = get_layout()
|
2015-07-21 14:40:25 +02:00
|
|
|
#puts "Layout #{l.class} in #{self.class} , #{self}"
|
|
|
|
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
|
|
|
|
def set_layout(layout)
|
2015-07-18 10:52:30 +02:00
|
|
|
# puts "Layout was set for #{self.class}"
|
2015-05-31 10:07:49 +02:00
|
|
|
raise "Nil layout" unless layout
|
2015-11-07 16:40:59 +01:00
|
|
|
set_internal(LAYOUT_INDEX , layout)
|
2015-05-22 21:51:36 +02:00
|
|
|
end
|
|
|
|
|
2015-05-31 12:02:29 +02:00
|
|
|
# so we can keep the raise in get_layout
|
|
|
|
def has_layout?
|
2015-11-07 16:40:59 +01:00
|
|
|
! get_internal(LAYOUT_INDEX).nil?
|
2015-05-31 12:02:29 +02:00
|
|
|
end
|
|
|
|
|
2015-05-16 11:53:10 +02:00
|
|
|
def get_layout()
|
2015-11-07 16:40:59 +01:00
|
|
|
l = get_internal(LAYOUT_INDEX)
|
2015-07-21 14:40:25 +02:00
|
|
|
#puts "get layout for #{self.class} returns #{l.class}"
|
2015-11-08 22:58:54 +01:00
|
|
|
raise "No layout #{self.object_id.to_s(16)}:#{self.class} " unless l
|
2015-05-25 17:48:35 +02:00
|
|
|
return l
|
2015-05-16 11:53:10 +02:00
|
|
|
end
|
|
|
|
|
2015-11-06 23:12:17 +01:00
|
|
|
# return the metaclass
|
|
|
|
def meta
|
|
|
|
MetaClass.new self
|
|
|
|
end
|
|
|
|
|
2015-05-20 09:57:20 +02:00
|
|
|
def get_instance_variables
|
2015-10-26 11:58:38 +01:00
|
|
|
get_layout().instance_names
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 09:57:20 +02:00
|
|
|
def get_instance_variable name
|
2015-05-14 18:53:56 +02:00
|
|
|
index = instance_variable_defined(name)
|
2015-07-21 14:40:25 +02:00
|
|
|
#puts "getting #{name} at #{index}"
|
2015-05-14 18:53:56 +02:00
|
|
|
return nil if index == nil
|
2015-11-07 16:40:59 +01:00
|
|
|
return get_internal(index)
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2015-05-20 09:57:20 +02:00
|
|
|
def set_instance_variable name , value
|
2015-05-14 18:53:56 +02:00
|
|
|
index = instance_variable_defined(name)
|
|
|
|
return nil if index == nil
|
2015-11-07 16:40:59 +01:00
|
|
|
return set_internal(index , value)
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def instance_variable_defined name
|
2015-07-21 14:40:25 +02:00
|
|
|
get_layout().variable_index(name)
|
2015-05-14 18:53:56 +02:00
|
|
|
end
|
|
|
|
|
2015-11-04 09:33:10 +01:00
|
|
|
def padded_length
|
|
|
|
padded_words( get_layout().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"
|
|
|
|
# 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
|
|
|
|
# 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 17:55:49 +02:00
|
|
|
end
|
2014-07-30 20:43:12 +02:00
|
|
|
end
|