From 885aa765d61688c0c546e8c6f4ea4be56986bdb7 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 26 Oct 2015 14:33:36 +0200 Subject: [PATCH] still fixing index bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lib/parfait/behaviour.rb | 5 +++++ lib/parfait/class.rb | 7 +++---- lib/parfait/indexed.rb | 9 +++++---- lib/parfait/layout.rb | 8 ++++++-- lib/parfait/list.rb | 2 +- lib/parfait/object.rb | 5 ++--- lib/register/boot.rb | 2 +- test/parfait/test_layout.rb | 4 ++-- test/parfait/test_list.rb | 8 ++++++++ test/parfait/test_meta.rb | 9 +++------ 10 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lib/parfait/behaviour.rb b/lib/parfait/behaviour.rb index 46696b01..18a3b4ea 100644 --- a/lib/parfait/behaviour.rb +++ b/lib/parfait/behaviour.rb @@ -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| diff --git a/lib/parfait/class.rb b/lib/parfait/class.rb index 60564535..0348606b 100644 --- a/lib/parfait/class.rb +++ b/lib/parfait/class.rb @@ -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 diff --git a/lib/parfait/indexed.rb b/lib/parfait/indexed.rb index e39197a6..d62bdb76 100644 --- a/lib/parfait/indexed.rb +++ b/lib/parfait/indexed.rb @@ -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 diff --git a/lib/parfait/layout.rb b/lib/parfait/layout.rb index 8f323f72..ac0bd5ef 100644 --- a/lib/parfait/layout.rb +++ b/lib/parfait/layout.rb @@ -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() diff --git a/lib/parfait/list.rb b/lib/parfait/list.rb index 270c2043..41790e20 100644 --- a/lib/parfait/list.rb +++ b/lib/parfait/list.rb @@ -15,7 +15,7 @@ require_relative "indexed" module Parfait class List < Object include Indexed - self.offset(0) + self.offset(1) def initialize( ) super() diff --git a/lib/parfait/object.rb b/lib/parfait/object.rb index b42f4e2f..ff2a7040 100644 --- a/lib/parfait/object.rb +++ b/lib/parfait/object.rb @@ -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 diff --git a/lib/register/boot.rb b/lib/register/boot.rb index 086ad866..bfd0eec8 100644 --- a/lib/register/boot.rb +++ b/lib/register/boot.rb @@ -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 ] , diff --git a/test/parfait/test_layout.rb b/test/parfait/test_layout.rb index 7726db34..14deafff 100644 --- a/test/parfait/test_layout.rb +++ b/test/parfait/test_layout.rb @@ -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 diff --git a/test/parfait/test_list.rb b/test/parfait/test_list.rb index 32c6bba9..d021acf8 100644 --- a/test/parfait/test_list.rb +++ b/test/parfait/test_list.rb @@ -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) diff --git a/test/parfait/test_meta.rb b/test/parfait/test_meta.rb index b9650eee..fb5ecc88 100644 --- a/test/parfait/test_meta.rb +++ b/test/parfait/test_meta.rb @@ -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