Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit ad6779a

Browse files
author
Shing Lyu
committed
Implemented list STORE_SUBSCR using RefCell
1 parent e865c91 commit ad6779a

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

RustPython/src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
3838
panic!("len(s) expects exactly one parameter");
3939
}
4040
let len = match args[0].deref() {
41-
&NativeType::List(ref l) => l.len(),
41+
&NativeType::List(ref l) => l.borrow().len(),
4242
&NativeType::Tuple(ref t) => t.len(),
4343
&NativeType::Str(ref s) => s.len(),
4444
_ => panic!("TypeError: object of this type has no len()")

RustPython/src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate serde_json;
88

99
//extern crate eval; use eval::eval::*;
1010
use std::collections::HashMap;
11+
use std::cell::RefCell;
1112
use std::env;
1213
use std::fs::File;
1314
use std::io::prelude::*;
@@ -24,7 +25,8 @@ pub enum NativeType{
2425
Float(f64),
2526
Str(String),
2627
Unicode(String),
27-
List(Vec<NativeType>),
28+
#[serde(skip_serializing, skip_deserializing)]
29+
List(RefCell<Vec<NativeType>>),
2830
Tuple(Vec<NativeType>),
2931
Iter(Vec<NativeType>), // TODO: use Iterator instead
3032
Code(PyCodeObject),
@@ -271,7 +273,7 @@ impl VirtualMachine {
271273
vec.push((*curr_frame.stack.pop().unwrap()).clone());
272274
}
273275
vec.reverse();
274-
curr_frame.stack.push(Rc::new(NativeType::List(vec)));
276+
curr_frame.stack.push(Rc::new(NativeType::List(RefCell::new(vec))));
275277
None
276278
},
277279

@@ -305,7 +307,7 @@ impl VirtualMachine {
305307
let iter = match *tos {
306308
//TODO: is this clone right?
307309
// Return a Iterator instead vvv
308-
NativeType::List(ref vec) => NativeType::Iter(vec.clone()),
310+
NativeType::List(ref vec) => NativeType::Iter(vec.borrow().clone()),
309311
_ => panic!("TypeError: object is not iterable")
310312
};
311313
curr_frame.stack.push(Rc::new(iter));
@@ -448,11 +450,9 @@ impl VirtualMachine {
448450
let tos1 = curr_frame.stack.pop().unwrap();
449451
let tos2 = curr_frame.stack.pop().unwrap();
450452
match (tos1.deref(), tos.deref()) {
451-
/* TODO: support list assignment
452-
(&NativeType::List(mut refl), &NativeType::Int(index)) => {
453-
refl[index as usize] = (*tos2).clone();
453+
(&NativeType::List(ref refl), &NativeType::Int(index)) => {
454+
refl.borrow_mut()[index as usize] = (*tos2).clone();
454455
},
455-
*/
456456
(&NativeType::Str(_), &NativeType::Int(_)) => {
457457
// TODO: raise TypeError: 'str' object does not support item assignment
458458
panic!("TypeError: 'str' object does not support item assignment")
@@ -486,7 +486,7 @@ impl VirtualMachine {
486486
(&NativeType::List(ref l1), &NativeType::List(ref l2)) => {
487487
let mut new_l = l2.clone();
488488
// TODO: remove unnessary copy
489-
new_l.append(&mut l1.clone());
489+
new_l.borrow_mut().append(&mut l1.borrow().clone());
490490
curr_frame.stack.push(Rc::new(NativeType::List(new_l)));
491491

492492
}
@@ -583,25 +583,25 @@ impl VirtualMachine {
583583
debug!("tos: {:?}, tos1: {:?}", tos, tos1);
584584
match (tos1.deref(), tos.deref()) {
585585
(&NativeType::List(ref l), &NativeType::Int(ref index)) => {
586-
let pos_index = (index + l.len() as i32) % l.len() as i32;
587-
curr_frame.stack.push(Rc::new(l[pos_index as usize].clone()))
586+
let pos_index = (index + l.borrow().len() as i32) % l.borrow().len() as i32;
587+
curr_frame.stack.push(Rc::new(l.borrow()[pos_index as usize].clone()))
588588
},
589589
(&NativeType::List(ref l), &NativeType::Slice(ref opt_start, ref opt_stop, ref opt_step)) => {
590590
let start = match opt_start {
591-
&Some(start) => ((start + l.len() as i32) % l.len() as i32) as usize,
591+
&Some(start) => ((start + l.borrow().len() as i32) % l.borrow().len() as i32) as usize,
592592
&None => 0,
593593
};
594594
let stop = match opt_stop {
595-
&Some(stop) => ((stop + l.len() as i32) % l.len() as i32) as usize,
596-
&None => l.len() as usize,
595+
&Some(stop) => ((stop + l.borrow().len() as i32) % l.borrow().len() as i32) as usize,
596+
&None => l.borrow().len() as usize,
597597
};
598598
let step = match opt_step {
599599
//Some(step) => step as usize,
600600
&None => 1 as usize,
601601
_ => unimplemented!(),
602602
};
603603
// TODO: we could potentially avoid this copy and use slice
604-
curr_frame.stack.push(Rc::new(NativeType::List(l[start..stop].to_vec())));
604+
curr_frame.stack.push(Rc::new(NativeType::List(RefCell::new(l.borrow()[start..stop].to_vec()))));
605605
},
606606
(&NativeType::Tuple(ref t), &NativeType::Int(ref index)) => curr_frame.stack.push(Rc::new(t[*index as usize].clone())),
607607
(&NativeType::Str(ref s), &NativeType::Int(ref index)) => {

0 commit comments

Comments
 (0)