make all instances attr read writers

unfortunately the writers have to have self.var =
otherwise it is just a local var
Also need to make the type explicit for all
Protocol included memory_length on the class for now
This commit is contained in:
Torsten Ruger
2018-08-11 19:15:34 +03:00
parent e6df473647
commit b0aefe31fe
19 changed files with 243 additions and 216 deletions

View File

@ -6,7 +6,7 @@
# word (instance of class Type).
# 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
# every object has a Type. Type objects are immutable and may be reused for a group/class
# of objects.
# The Type of an object may change, but then a new Type is created
# The Type also defines the class of the object
@ -16,8 +16,16 @@ module Parfait
TYPE_INDEX = 0
class Object
attr :type
def self.new *args
def self.memory_size
8
end
def self.type_length
1
end
def self.new( *args )
object = self.allocate
# have to grab the class, because we are in the ruby class not the parfait one
@ -25,35 +33,16 @@ module Parfait
# and have to set the type before we let the object do anything. otherwise boom
object.set_type cl.instance_type
object.send :initialize , *args
object
end
# 0 -based index
def get_internal_word(index)
name = get_type().name_at(index)
return nil unless name
instance_variable_get("@#{name}".to_sym)
end
# 0 -based index
def set_internal_word(index , value)
return set_type(value) if( index == TYPE_INDEX)
raise "not type #{@type.class}" unless @type.is_a?(Type)
name = @type.name_at(index)
#return value unless name
raise "object type (#{@type}) has no name at index #{index} " unless name
instance_variable_set("@#{name}".to_sym, value)
value
end
def == other
self.object_id == other.object_id
end
# 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)
# This is the core of the object system.
# The class of an object is stored in the objects memory
#
# In RubyX we store the class in the Type, and so the Type is the only fixed
# data that every object carries.
@ -64,20 +53,19 @@ module Parfait
end
# private
def set_type(type)
# puts "Type was set for #{self.class}"
raise "not type #{type.class} in #{self}" unless type.is_a?(Type)
@type = type
def set_type(typ)
raise "not type #{typ.class} in #{object_id.to_s(16)}" unless typ.is_a?(Type)
self.type = typ
end
# so we can keep the raise in get_type
def has_type?
! @type.nil?
! type.nil?
end
def get_type()
raise "No type #{self.object_id.to_s(16)}:#{self.class} " unless has_type?
@type
type
end
# return the metaclass
@ -86,7 +74,7 @@ module Parfait
end
def get_instance_variables
@type.names
type.names
end
def get_instance_variable( name )
@ -103,20 +91,20 @@ module Parfait
end
def instance_variable_defined( name )
@type.variable_index(name)
type.variable_index(name)
end
def padded_length
Padding.padded_words( @type.instance_length )
Padding.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
# 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_sym }
get_instance_variables.to_a.collect{ |n| "#{n}".to_sym }
end
# name comes in as a ruby @var name
# name comes in as a ruby var name
def instance_variable_ged name
var = get_instance_variable name.to_s[1 .. -1].to_sym
#puts "getting #{name} #{var}"