adding an instance for the length of lists

This commit is contained in:
Torsten Ruger 2015-10-25 20:44:03 +02:00
parent c1ee67038c
commit df62b75c6f
3 changed files with 13 additions and 10 deletions

View File

@ -14,6 +14,7 @@ module Parfait
module Indexed # marker module
def self.included(base)
base.extend(Methods)
base.attribute :indexed_length
end
# include? means non nil index
@ -143,11 +144,11 @@ module Parfait
offset
end
define_method :get_length do
internal_object_length - 1 - offset
define_method :get_length do
r = internal_object_get( offset + 1) #one for layout
r.nil? ? 0 : r
end
# set the value at index.
# Lists start from index 1
define_method :set do | index , value|
@ -174,14 +175,16 @@ module Parfait
raise "Only positive lenths, #{len}" if len < 0
old_length = self.get_length
return if old_length >= len
internal_object_grow(len + 1 + offset)
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
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_shrink(len + 1 + offset)
internal_object_set( offset + 1 , len) #one for layout
end
end

View File

@ -114,7 +114,7 @@ module Register
# and all instance variable names. Really have to find a better way
def layout_names
{ :Word => [] ,
:List => [] ,
:List => [:indexed_length] ,
# Assumtion is that name is the last of message
:Message => [:next_message , :receiver , :frame , :return_address , :return_value,
:caller , :name ],
@ -125,7 +125,7 @@ module Register
:BinaryCode => [],
:Space => [:classes , :first_message ],
:Frame => [:next_frame ],
:Layout => [:object_class] ,
:Layout => [:object_class,:indexed_length] ,
# TODO fix layouts for inherited classes. Currently only :Class and the
# instances are copied (shame on you)
:Class => [:object_layout , :name , :instance_methods , :super_class_name , :meta_class],

View File

@ -15,7 +15,6 @@ class TestList < MiniTest::Test
end
def test_old_layout_push
list = Register.machine.space.classes.keys
list.push(1)
assert_equal Parfait::Layout , list.get_layout.class
end
def test_new_layout
@ -54,7 +53,8 @@ class TestList < MiniTest::Test
assert_equal nil , @list.get(3)
end
def test_one_set1
assert_equal 1 , @list.set(1,1)
assert_equal 2 , @list.set(1,2)
assert_equal 1 , @list.internal_object_get(1)
end
def test_set1_len
@list.set(1,1)
@ -73,7 +73,7 @@ class TestList < MiniTest::Test
end
def test_one_get1
test_one_set1
assert_equal 1 , @list.get(1)
assert_equal 2 , @list.get(1)
end
def test_one_get2
test_one_set2