@@ -2243,18 +2243,44 @@ impl KotoVm {
22432243 if * index >= 0.0 && u_index < list_len {
22442244 list_data[ u_index] = value. clone ( ) ;
22452245 } else {
2246- return runtime_error ! ( "Index ' {index}' not in List " ) ;
2246+ return runtime_error ! ( "Invalid index ( {index}) " ) ;
22472247 }
22482248 }
22492249 Range ( range) => {
22502250 for i in range. indices ( list_len) {
22512251 list_data[ i] = value. clone ( ) ;
22522252 }
22532253 }
2254- unexpected => return unexpected_type ( "index " , unexpected) ,
2254+ unexpected => return unexpected_type ( "Number or Range " , unexpected) ,
22552255 }
22562256 Ok ( ( ) )
22572257 }
2258+ Map ( map) => match index_value {
2259+ Number ( index) => {
2260+ let mut map_data = map. data_mut ( ) ;
2261+ let map_len = map_data. len ( ) ;
2262+ let u_index = usize:: from ( index) ;
2263+ if * index >= 0.0 && u_index < map_len {
2264+ match value {
2265+ Tuple ( new_entry) if new_entry. len ( ) == 2 => {
2266+ let key = ValueKey :: try_from ( new_entry[ 0 ] . clone ( ) ) ?;
2267+ // There's no API on IndexMap for replacing an entry,
2268+ // so use swap_remove_index to remove the old entry,
2269+ // then insert the new entry at the end of the map,
2270+ // followed by swap_indices to swap the new entry back into position.
2271+ map_data. swap_remove_index ( u_index) ;
2272+ map_data. insert ( key, new_entry[ 1 ] . clone ( ) ) ;
2273+ map_data. swap_indices ( u_index, map_len - 1 ) ;
2274+ Ok ( ( ) )
2275+ }
2276+ unexpected => unexpected_type ( "Tuple with 2 elements" , unexpected) ,
2277+ }
2278+ } else {
2279+ runtime_error ! ( "Invalid index ({index})" )
2280+ }
2281+ }
2282+ unexpected => unexpected_type ( "Number" , unexpected) ,
2283+ } ,
22582284 Object ( o) => o. try_borrow_mut ( ) ?. index_mut ( index_value, value) ,
22592285 unexpected => unexpected_type ( "a mutable indexable value" , & unexpected) ,
22602286 }
0 commit comments