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 base.attribute :instance_methods
end end
def initialize
super()
self.instance_methods = List.new
end
def method_names def method_names
names = List.new names = List.new
self.instance_methods.each do |method| self.instance_methods.each do |method|

View File

@ -22,10 +22,10 @@ module Parfait
def initialize name , superclass def initialize name , superclass
super() super()
self.name = name self.name = name
self.instance_methods = List.new
self.super_class_name = superclass self.super_class_name = superclass
# the layout for this class (class = object of type Class) carries the class # 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 # 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) self.object_layout = Layout.new(self)
end end
@ -34,10 +34,9 @@ module Parfait
end end
def meta def meta
m = self.meta_class get_layout
return m if m
self.meta_class = MetaClass.new(self)
end end
def add_instance_name name def add_instance_name name
self.object_layout.push name self.object_layout.push name
end end

View File

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

View File

@ -14,18 +14,22 @@
# But as we want every Object to have a class, the Layout carries that class. # 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" # 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 # In other words, the Layout is a list of names that describe
# the values stored in an actual object. # the values stored in an actual object.
# The object is an List of values of length n and # 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 # Together they turn the object into a hash like structure
module Parfait module Parfait
class Layout < Object class Layout < Object
attribute :object_class attribute :object_class
include Behaviour
include Indexed include Indexed
self.offset(1) self.offset(3)
def initialize( object_class ) def initialize( object_class )
super() super()

View File

@ -15,7 +15,7 @@ require_relative "indexed"
module Parfait module Parfait
class List < Object class List < Object
include Indexed include Indexed
self.offset(0) self.offset(1)
def initialize( ) def initialize( )
super() 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 # The Layout is **always** the first entry (index 1) in an object, but the type word is index 0
module Parfait module Parfait
class Object < Value LAYOUT_INDEX = 1
LAYOUT_INDEX = 0 class Object < Value
CLASS_INDEX = 1 #only used in class, but keep constants together
def self.new *args def self.new *args
object = self.allocate object = self.allocate

View File

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

View File

@ -21,7 +21,7 @@ class TestLayout < MiniTest::Test
end end
def test_layout_index 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 end
def test_inspect def test_inspect
@ -73,6 +73,6 @@ class TestLayout < MiniTest::Test
def test_remove_me def test_remove_me
layout = @mess.get_layout layout = @mess.get_layout
assert_equal layout , @mess.internal_object_get(0) assert_equal layout , @mess.internal_object_get(1)
end end
end end

View File

@ -37,10 +37,18 @@ class TestList < MiniTest::Test
assert_equal 0 , @list.get_length assert_equal 0 , @list.get_length
assert_equal nil , @list.indexed_length assert_equal nil , @list.indexed_length
end 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 def test_length1
@list.push :one @list.push :one
assert_equal 1 , @list.get_length assert_equal 1 , @list.get_length
assert_equal 1 , @list.indexed_length assert_equal 1 , @list.indexed_length
assert_equal 1 , @list.internal_object_get(Parfait::LAYOUT_INDEX + 1)
end end
def test_list_inspect def test_list_inspect
@list.set(1,1) @list.set(1,1)

View File

@ -5,6 +5,7 @@ class TestMeta < MiniTest::Test
def setup def setup
@space = Register.machine.boot.space @space = Register.machine.boot.space
@try = @space.create_class(:Try , :Object).meta @try = @space.create_class(:Try , :Object).meta
puts @try.class
end end
def foo_method for_class = :Try 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 ::Parfait::Method.new @space.get_class_by_name(for_class) , :foo , args
end end
def pest_meta def test_meta
assert @try assert @try
end end
def pest_meta_object def test_meta_object
assert @space.get_class_by_name(:Object).meta assert @space.get_class_by_name(:Object).meta
end 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 def pest_new_methods
assert_equal @try.method_names.class, @try.instance_methods.class assert_equal @try.method_names.class, @try.instance_methods.class
assert_equal @try.method_names.get_length , @try.instance_methods.get_length assert_equal @try.method_names.get_length , @try.instance_methods.get_length