@@ -2,55 +2,144 @@ use crate::vm::{Value, Actor};
22use crate :: alloc:: Alloc ;
33use crate :: host:: HostFn ;
44
5- #[ derive( Clone , Default ) ]
65pub struct Array
76{
8- pub elems : Vec < Value > ,
7+ pub elems : * mut [ Value ] ,
8+ pub len : usize ,
99}
1010
1111impl Array
1212{
13- pub fn with_capacity ( cap : u32 ) -> Self
13+ pub fn with_capacity ( capacity : usize , alloc : & mut Alloc ) -> Result < Self , ( ) >
1414 {
15- Self {
16- elems : Vec :: with_capacity ( cap as usize )
15+ let table = alloc. alloc_table ( capacity) ?;
16+ Ok ( Array { elems : table, len : 0 } )
17+ }
18+
19+ pub fn clone ( & self , alloc : & mut Alloc ) -> Result < Self , ( ) >
20+ {
21+ let table = alloc. alloc_table ( self . len ) ?;
22+ let mut arr = Array { elems : table, len : self . len } ;
23+ arr. items_mut ( ) . copy_from_slice ( self . items ( ) ) ;
24+ Ok ( arr)
25+ }
26+
27+ pub fn items ( & self ) -> & [ Value ] {
28+ unsafe { & ( * self . elems ) [ ..self . len ] }
29+ }
30+
31+ pub fn items_mut ( & mut self ) -> & mut [ Value ] {
32+ unsafe { & mut ( * self . elems ) [ ..self . len ] }
33+ }
34+
35+ pub fn push ( & mut self , val : Value , alloc : & mut Alloc ) -> Result < ( ) , ( ) >
36+ {
37+ if self . len == self . elems . len ( ) {
38+ let new_len = self . len * 2 + 1 ;
39+ let new_elems = alloc. alloc_table ( new_len) ?;
40+ unsafe {
41+ ( & mut * new_elems) [ ..self . len ] . copy_from_slice ( & ( * self . elems ) [ ..self . len ] ) ;
42+ self . elems = new_elems;
43+ }
44+ }
45+ unsafe {
46+ ( & mut * self . elems ) [ self . len ] = val;
47+ self . len += 1 ;
48+ }
49+ Ok ( ( ) )
50+ }
51+
52+ pub fn insert ( & mut self , idx : usize , val : Value , alloc : & mut Alloc ) -> Result < ( ) , ( ) >
53+ {
54+ if self . len == self . elems . len ( ) {
55+ let new_len = self . len * 2 + 1 ;
56+ let new_elems = alloc. alloc_table ( new_len) ?;
57+ unsafe {
58+ ( & mut * new_elems) . copy_from_slice ( & ( * self . elems ) [ ..self . len ] ) ;
59+ self . elems = new_elems;
60+ }
1761 }
62+ unsafe {
63+ ( & mut * self . elems ) . copy_within ( idx..self . len , idx + 1 ) ;
64+ ( & mut * self . elems ) [ idx] = val;
65+ self . len += 1 ;
66+ }
67+
68+ Ok ( ( ) )
1869 }
1970
20- pub fn push ( & mut self , val : Value )
71+ pub fn remove ( & mut self , idx : usize ) -> Value
2172 {
22- self . elems . push ( val) ;
73+ if idx >= self . len {
74+ return Value :: Nil ;
75+ }
76+
77+ let removed = unsafe { ( & mut * self . elems ) [ idx] } ;
78+ unsafe {
79+ ( & mut * self . elems ) . copy_within ( idx + 1 ..self . len , idx) ;
80+ }
81+
82+ self . len -= 1 ;
83+ removed
84+ }
85+
86+ pub fn extend ( & mut self , other : & Array , alloc : & mut Alloc ) -> Result < ( ) , ( ) > {
87+ let other_elems = other. elems ;
88+ if self . len + other_elems. len ( ) > self . elems . len ( ) {
89+ let new_len = self . len + other_elems. len ( ) ;
90+ let new_elems = alloc. alloc_table ( new_len) ?;
91+ unsafe {
92+ ( & mut * new_elems) [ ..self . len ] . copy_from_slice ( & ( * self . elems ) [ ..self . len ] ) ;
93+ ( & mut * new_elems) [ self . len ..] . copy_from_slice ( & ( * other_elems) [ ..other_elems. len ( ) ] ) ;
94+ self . elems = new_elems;
95+ }
96+ } else {
97+ unsafe {
98+ ( & mut * self . elems ) [ self . len ..] . copy_from_slice ( & ( * other_elems) [ ..other_elems. len ( ) ] ) ;
99+ }
100+ }
101+ self . len += other_elems. len ( ) ;
102+
103+ Ok ( ( ) )
23104 }
24105
25106 pub fn pop ( & mut self ) -> Value
26107 {
27- self . elems . pop ( ) . unwrap ( )
108+ if self . len == 0 {
109+ return Value :: Nil ;
110+ }
111+
112+ self . len -= 1 ;
113+ unsafe { ( * self . elems ) [ self . len ] }
28114 }
29115
30116 pub fn get ( & self , idx : usize ) -> Value
31117 {
32- self . elems [ idx]
118+ unsafe { ( * self . elems ) [ idx] }
33119 }
34120
35121 pub fn set ( & mut self , idx : usize , val : Value )
36122 {
37- self . elems [ idx] = val;
123+ unsafe { ( * self . elems ) [ idx] = val } ;
124+ }
125+
126+ pub fn len ( & self ) -> usize {
127+ self . len
38128 }
39129}
40130
41131pub fn array_with_size ( actor : & mut Actor , _self : Value , num_elems : Value , fill_val : Value ) -> Result < Value , String >
42132{
43133 let num_elems = num_elems. unwrap_usize ( ) ;
44- let mut elems = Vec :: with_capacity ( num_elems) ;
45- elems. resize ( num_elems, fill_val) ;
46- let arr = Array { elems } ;
47- let p_arr = actor. alloc . alloc ( arr) . unwrap ( ) ;
48- Ok ( Value :: Array ( p_arr) )
134+ let mut elems = actor. alloc . alloc_table ( num_elems) . unwrap ( ) ;
135+ unsafe { ( & mut * elems) . fill ( fill_val) ; }
136+ let arr = Array { elems, len : num_elems } ;
137+ Ok ( Value :: Array ( actor. alloc . alloc ( arr) . unwrap ( ) ) )
49138}
50139
51140pub fn array_push ( actor : & mut Actor , mut array : Value , val : Value ) -> Result < Value , String >
52141{
53- array. unwrap_arr ( ) . push ( val) ;
142+ array. unwrap_arr ( ) . push ( val, & mut actor . alloc ) . unwrap ( ) ;
54143 Ok ( Value :: Nil )
55144}
56145
@@ -62,19 +151,19 @@ pub fn array_pop(actor: &mut Actor, mut array: Value) -> Result<Value, String>
62151pub fn array_remove ( actor : & mut Actor , mut array : Value , idx : Value ) -> Result < Value , String >
63152{
64153 let idx = idx. unwrap_usize ( ) ;
65- Ok ( array. unwrap_arr ( ) . elems . remove ( idx) )
154+ Ok ( array. unwrap_arr ( ) . remove ( idx) )
66155}
67156
68157pub fn array_insert ( actor : & mut Actor , mut array : Value , idx : Value , val : Value ) -> Result < Value , String >
69158{
70159 let idx = idx. unwrap_usize ( ) ;
71- array. unwrap_arr ( ) . elems . insert ( idx, val) ;
160+ array. unwrap_arr ( ) . insert ( idx, val, & mut actor . alloc ) . unwrap ( ) ;
72161 Ok ( Value :: Nil )
73162}
74163
75- pub fn array_append ( _actor : & mut Actor , mut self_array : Value , mut other_array : Value ) -> Result < Value , String >
164+ pub fn array_append ( actor : & mut Actor , mut self_array : Value , mut other_array : Value ) -> Result < Value , String >
76165{
77- let other_elems = other_array. unwrap_arr ( ) . elems . clone ( ) ;
78- self_array. unwrap_arr ( ) . elems . extend ( other_elems) ;
166+ let other_elems = other_array. unwrap_arr ( ) ;
167+ self_array. unwrap_arr ( ) . extend ( other_elems, & mut actor . alloc ) . unwrap ( ) ;
79168 Ok ( Value :: Nil )
80- }
169+ }
0 commit comments