removing the fake memory form object

just in word and list now
This commit is contained in:
Torsten Ruger 2016-12-29 18:49:03 +02:00
parent 6214040888
commit 25f44949e4
4 changed files with 68 additions and 41 deletions

View File

@ -13,7 +13,11 @@ module Parfait
module Indexed # marker module
def self.included(base)
base.extend(OffsetMethods)
base.attribute :indexed_length
end
def initialize( )
super()
@memory = []
end
# include? means non nil index
@ -165,6 +169,18 @@ module Parfait
ret
end
# 1 -based index
def get_internal_word(index)
@memory[index]
end
# 1 -based index
def set_internal_word(index , value)
raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? )
@memory[index] = value
value
end
module OffsetMethods
# generate all methods that depend on the (memory) offset
# These are get/set shrink_to/grow_to

View File

@ -17,6 +17,13 @@ module Parfait
include Indexed
self.offset(1)
def self.attributes
[:indexed_length]
end
def indexed_length
get_length()
end
def initialize( )
super()
end

View File

@ -21,7 +21,6 @@ module Parfait
# At compile time we fake memory by using a global array for pages
def self.new *args
object = self.allocate
object.compile_time_init if object.respond_to?(:compile_time_init)
# 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)
@ -36,33 +35,23 @@ module Parfait
include Padding
include Positioned
def compile_time_init
@memory = Array.new(16)
self # for chaining
end
# 1 -based index
def get_internal_word(index)
@memory[index]
name = get_type().name_at(index)
return nil unless name
eval "@#{name}"
end
# 1 -based index
def set_internal_word(index , value)
raise "failed init for #{self.class}" unless @memory
raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? )
@memory[index] = value
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"
value
end
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
def == other
self.object_id == other.object_id
end
@ -81,20 +70,18 @@ module Parfait
# private
def set_type(type)
# puts "Type was set for #{self.class}"
raise "Nil type" unless type
set_internal_word(TYPE_INDEX , type)
raise "not type #{type.class}" unless type.is_a?(Type)
@type = type
end
# so we can keep the raise in get_type
def has_type?
! get_internal_word(TYPE_INDEX).nil?
! @type.nil?
end
def get_type()
l = get_internal_word(TYPE_INDEX)
#puts "get type for #{self.class} returns #{l.class}"
raise "No type #{self.object_id.to_s(16)}:#{self.class} " unless l
return l
raise "No type #{self.object_id.to_s(16)}:#{self.class} " unless has_type?
@type
end
# return the metaclass
@ -103,28 +90,28 @@ module Parfait
end
def get_instance_variables
get_type().instance_names
@type.names
end
def get_instance_variable name
def get_instance_variable( name )
index = instance_variable_defined(name)
#puts "getting #{name} at #{index}"
return nil if index == nil
return get_internal_word(index)
end
def set_instance_variable name , value
def set_instance_variable( name , value )
index = instance_variable_defined(name)
return nil if index == nil
return set_internal_word(index , value)
end
def instance_variable_defined name
get_type().variable_index(name)
def instance_variable_defined( name )
@type.variable_index(name)
end
def padded_length
padded_words( get_type().instance_length )
padded_words( @type.instance_length )
end
# parfait versions are deliberately called different, so we "relay"

View File

@ -13,7 +13,10 @@ module Parfait
# Object length is measured in non-type cells though
class Word < Object
attribute :char_length
def self.attributes
[:char_length]
end
attr_reader :char_length
#semi "indexed" methods for interpreter
def self.get_length_index
@ -27,13 +30,27 @@ module Parfait
# Register provides methods to create Parfait objects from ruby
def initialize len
super()
self.char_length = 0
@char_length = 0
@memory = []
raise "Must init with int, not #{len.class}" unless len.kind_of? Fixnum
raise "Must init with positive, not #{len}" if len < 0
set_length( len , 32 ) unless len == 0 #32 beeing ascii space
#puts "type #{self.get_type} #{self.object_id.to_s(16)}"
end
# 1 -based index
def get_internal_word(index)
@memory[index]
end
# 1 -based index
def set_internal_word(index , value)
raise "Word[#{index}] = nil" if( value.nil? )
@memory[index] = value
value
end
# return a copy of self
def copy
cop = Word.new( self.length )
@ -47,7 +64,7 @@ module Parfait
# return the number of characters
def length()
obj_len = self.char_length
obj_len = @char_length
return obj_len
end
@ -75,9 +92,9 @@ module Parfait
#
def set_length(len , fill_char)
return if len <= 0
old = self.char_length
old = @char_length
return if old >= len
self.char_length = len
@char_length = len
check_length
fill_from_with( old + 1 , fill_char )
end
@ -168,7 +185,7 @@ module Parfait
def to_string
string = ""
index = 1
while( index <= self.char_length)
while( index <= @char_length)
char = get_char(index)
string += char ? char.chr : "*"
index = index + 1
@ -182,12 +199,12 @@ module Parfait
end
def padded_length
padded( 4 * get_type().instance_length + self.char_length )
padded( 4 * get_type().instance_length + @char_length )
end
private
def check_length
raise "Length out of bounds #{self.char_length}" if self.char_length > 1000
raise "Length out of bounds #{@char_length}" if @char_length > 1000
end
end