still fixing index bugs

the indexed_length got written wrong
which is why the layout methods didn’t work
Now all indexes are 1 based, even fake men, where we just ignore 0
This commit is contained in:
Torsten Ruger 2015-10-26 14:33:36 +02:00
parent 9f4952b5ac
commit 885aa765d6
10 changed files with 36 additions and 23 deletions

View File

@ -12,6 +12,11 @@ module Parfait
base.attribute :instance_methods
end
def initialize
super()
self.instance_methods = List.new
end
def method_names
names = List.new
self.instance_methods.each do |method|

View File

@ -22,10 +22,10 @@ module Parfait
def initialize name , superclass
super()
self.name = name
self.instance_methods = List.new
self.super_class_name = superclass
# the layout for this class (class = object of type Class) carries the class
# as an instance. The relation is from an object through the Layout to it's class
# TODO the object layout should copy the stuff from superclass
self.object_layout = Layout.new(self)
end
@ -34,10 +34,9 @@ module Parfait
end
def meta
m = self.meta_class
return m if m
self.meta_class = MetaClass.new(self)
get_layout
end
def add_instance_name name
self.object_layout.push name
end

View File

@ -147,7 +147,7 @@ module Parfait
end
define_method :get_length do
r = internal_object_get( offset + 1) #one for layout
r = internal_object_get( offset ) #one for layout
r.nil? ? 0 : r
end
@ -158,7 +158,7 @@ module Parfait
if index > self.get_length
grow_to(index)
end
# internally 1 is reserved for the layout
# start one higher than offset, which is where the length is
internal_object_set( index + 1 + offset, value)
end
@ -169,6 +169,7 @@ module Parfait
if index > self.get_length
return nil
else
# start one higher than offset, which is where the length is
return internal_object_get(index + offset + 1)
end
end
@ -179,14 +180,14 @@ module Parfait
return if old_length >= len
# raise "bounds error at #{len}" if( len + offset > 16 )
# be nice to use the indexed_length , but that relies on booted space
internal_object_set( offset + 1 , len) #one for layout
internal_object_set( offset , len) #one for layout
end
define_method :shrink_to do | len|
raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length
return if old_length <= len
internal_object_set( offset + 1 , len) #one for layout
internal_object_set( offset , len)
end
end

View File

@ -14,18 +14,22 @@
# But as we want every Object to have a class, the Layout carries that class.
# So the layout of layout has an entry "object_class"
# But Objects must also be able to carry methods themselves (ruby calls singleton_methods)
# and those too are stored in the Layout (both layout and class include behaviour)
# In other words, the Layout is a list of names that describe
# the values stored in an actual object.
# The object is an List of values of length n and
# the Layout is an List of names of length n
# the Layout is an List of names of length n , plus class reference and methods reference
# Together they turn the object into a hash like structure
module Parfait
class Layout < Object
attribute :object_class
include Behaviour
include Indexed
self.offset(1)
self.offset(3)
def initialize( object_class )
super()

View File

@ -15,7 +15,7 @@ require_relative "indexed"
module Parfait
class List < Object
include Indexed
self.offset(0)
self.offset(1)
def initialize( )
super()

View File

@ -11,10 +11,9 @@
# The Layout is **always** the first entry (index 1) in an object, but the type word is index 0
module Parfait
class Object < Value
LAYOUT_INDEX = 1
LAYOUT_INDEX = 0
CLASS_INDEX = 1 #only used in class, but keep constants together
class Object < Value
def self.new *args
object = self.allocate

View File

@ -125,7 +125,7 @@ module Register
:BinaryCode => [],
:Space => [:classes , :first_message ],
:Frame => [:next_frame ],
:Layout => [:object_class,:indexed_length] ,
:Layout => [:object_class,:instance_methods,:indexed_length] ,
:Class => [:object_layout , :name , :instance_methods , :super_class_name ],
:Dictionary => [:keys , :values ] ,
:Method => [:name , :source , :instructions , :binary ,:arguments , :for_class, :locals ] ,

View File

@ -21,7 +21,7 @@ class TestLayout < MiniTest::Test
end
def test_layout_index
assert_equal @mess.get_layout , @mess.internal_object_get(0) , "mess"
assert_equal @mess.get_layout , @mess.internal_object_get(Parfait::LAYOUT_INDEX) , "mess"
end
def test_inspect
@ -73,6 +73,6 @@ class TestLayout < MiniTest::Test
def test_remove_me
layout = @mess.get_layout
assert_equal layout , @mess.internal_object_get(0)
assert_equal layout , @mess.internal_object_get(1)
end
end

View File

@ -37,10 +37,18 @@ class TestList < MiniTest::Test
assert_equal 0 , @list.get_length
assert_equal nil , @list.indexed_length
end
def test_offset
assert_equal 2 , @list.get_offset
end
def test_indexed_index
# 1 layout , 2 indexed_length
assert_equal 2 , @list.get_layout.variable_index(:indexed_length)
end
def test_length1
@list.push :one
assert_equal 1 , @list.get_length
assert_equal 1 , @list.indexed_length
assert_equal 1 , @list.internal_object_get(Parfait::LAYOUT_INDEX + 1)
end
def test_list_inspect
@list.set(1,1)

View File

@ -5,6 +5,7 @@ class TestMeta < MiniTest::Test
def setup
@space = Register.machine.boot.space
@try = @space.create_class(:Try , :Object).meta
puts @try.class
end
def foo_method for_class = :Try
@ -12,17 +13,13 @@ class TestMeta < MiniTest::Test
::Parfait::Method.new @space.get_class_by_name(for_class) , :foo , args
end
def pest_meta
def test_meta
assert @try
end
def pest_meta_object
def test_meta_object
assert @space.get_class_by_name(:Object).meta
end
def pest_new_superclass
assert_equal Parfait::MetaClass , @try.super_class.class
assert_equal :MetaObject , @try.super_class.name
end
def pest_new_methods
assert_equal @try.method_names.class, @try.instance_methods.class
assert_equal @try.method_names.get_length , @try.instance_methods.get_length