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

Commit 0463259

Browse files
author
Shing Lyu
committed
Finished migrating to Rc
1 parent ec13d61 commit 0463259

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

RustPython/src/builtins.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
use NativeType;
2+
use std::rc::Rc;
3+
use std::ops::Deref;
24

3-
pub fn print(args: Vec<NativeType>) -> NativeType {
5+
pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
46
for elem in args {
57
// TODO: figure out how python's print vectors
6-
match elem {
7-
NativeType::NoneType => println!("None"),
8-
NativeType::Boolean(b)=> {
9-
if b {
8+
match elem.deref() {
9+
&NativeType::NoneType => println!("None"),
10+
&NativeType::Boolean(ref b)=> {
11+
if *b {
1012
println!("True");
1113
} else {
1214
println!("False");
1315
}
1416
},
15-
NativeType::Int(x) => println!("{}", x),
16-
NativeType::Float(x) => println!("{}", x),
17-
NativeType::Str(x) => println!("{}", x),
18-
NativeType::Unicode(x) => println!("{}", x),
17+
&NativeType::Int(ref x) => println!("{}", x),
18+
&NativeType::Float(ref x) => println!("{}", x),
19+
&NativeType::Str(ref x) => println!("{}", x),
20+
&NativeType::Unicode(ref x) => println!("{}", x),
1921
_ => panic!("Print for {:?} not implemented yet", elem),
2022
/*
2123
List(Vec<NativeType>),
@@ -31,11 +33,11 @@ pub fn print(args: Vec<NativeType>) -> NativeType {
3133
NativeType::NoneType
3234
}
3335

34-
pub fn len(args: Vec<NativeType>) -> NativeType {
36+
pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
3537
if args.len() != 1 {
3638
panic!("len(s) expects exactly one parameter");
3739
}
38-
let len = match &args[0] {
40+
let len = match args[0].deref() {
3941
&NativeType::List(ref l) => l.len(),
4042
&NativeType::Tuple(ref t) => t.len(),
4143
&NativeType::Str(ref s) => s.len(),

RustPython/src/main.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub enum NativeType{
3131
Function(Function),
3232
Slice(Option<i32>, Option<i32>, Option<i32>), // start, stop, step
3333
#[serde(skip_serializing, skip_deserializing)]
34-
NativeFunction(fn(Vec<NativeType>) -> NativeType ),
34+
NativeFunction(fn(Vec<Rc<NativeType>>) -> NativeType ),
3535
}
3636

3737
const CMP_OP: &'static [&'static str] = &[">",
@@ -572,15 +572,14 @@ impl VirtualMachine {
572572
None
573573
},
574574

575-
/*
576575
("BINARY_SUBSCR", None) => {
577576
let curr_frame = self.curr_frame();
578577
let tos = curr_frame.stack.pop().unwrap();
579578
let tos1 = curr_frame.stack.pop().unwrap();
580-
match (&tos1, &tos) {
579+
match (tos1.deref(), tos.deref()) {
581580
(&NativeType::List(ref l), &NativeType::Int(ref index)) => {
582581
let pos_index = (index + l.len() as i32) % l.len() as i32;
583-
curr_frame.stack.push(l[pos_index as usize].clone())
582+
curr_frame.stack.push(Rc::new(l[pos_index as usize].clone()))
584583
},
585584
(&NativeType::List(ref l), &NativeType::Slice(ref opt_start, ref opt_stop, ref opt_step)) => {
586585
let start = match opt_start {
@@ -597,12 +596,12 @@ impl VirtualMachine {
597596
_ => unimplemented!(),
598597
};
599598
// TODO: we could potentially avoid this copy and use slice
600-
curr_frame.stack.push(NativeType::List(l[start..stop].to_vec()));
599+
curr_frame.stack.push(Rc::new(NativeType::List(l[start..stop].to_vec())));
601600
},
602-
(&NativeType::Tuple(ref t), &NativeType::Int(ref index)) => curr_frame.stack.push(t[*index as usize].clone()),
601+
(&NativeType::Tuple(ref t), &NativeType::Int(ref index)) => curr_frame.stack.push(Rc::new(t[*index as usize].clone())),
603602
(&NativeType::Str(ref s), &NativeType::Int(ref index)) => {
604603
let idx = (index + s.len() as i32) % s.len() as i32;
605-
curr_frame.stack.push(NativeType::Str(s.chars().nth(idx as usize).unwrap().to_string()));
604+
curr_frame.stack.push(Rc::new(NativeType::Str(s.chars().nth(idx as usize).unwrap().to_string())));
606605
},
607606
(&NativeType::Str(ref s), &NativeType::Slice(ref opt_start, ref opt_stop, ref opt_step)) => {
608607
let start = match opt_start {
@@ -618,7 +617,7 @@ impl VirtualMachine {
618617
&None => 1 as usize,
619618
_ => unimplemented!(),
620619
};
621-
curr_frame.stack.push(NativeType::Str(s[start..stop].to_string()));
620+
curr_frame.stack.push(Rc::new(NativeType::Str(s[start..stop].to_string())));
622621
},
623622
// TODO: implement other Slice possibilities
624623
_ => panic!("TypeError: indexing type {:?} with index {:?} is not supported (yet?)", tos1, tos)
@@ -636,9 +635,9 @@ impl VirtualMachine {
636635
("UNARY_NEGATIVE", None) => {
637636
let curr_frame = self.curr_frame();
638637
let v = curr_frame.stack.pop().unwrap();
639-
match v {
640-
NativeType::Int(v1i) => {
641-
curr_frame.stack.push(NativeType::Int(-v1i));
638+
match v.deref() {
639+
&NativeType::Int(v1i) => {
640+
curr_frame.stack.push(Rc::new(NativeType::Int(-v1i)));
642641
},
643642
_ => panic!("TypeError in UINARY_NEGATIVE")
644643
}
@@ -665,14 +664,14 @@ impl VirtualMachine {
665664
// https://docs.python.org/3.4/library/dis.html#opcode-MAKE_FUNCTION
666665
let curr_frame = self.curr_frame();
667666
let qualified_name = curr_frame.stack.pop().unwrap();
668-
let code_obj = match curr_frame.stack.pop().unwrap() {
669-
NativeType::Code(code) => code,
667+
let code_obj = match curr_frame.stack.pop().unwrap().deref() {
668+
&NativeType::Code(ref code) => code.clone(),
670669
_ => panic!("Second item on the stack should be a code object")
671670
};
672671
// pop argc arguments
673672
// argument: name, args, globals
674673
let func = Function::new(code_obj);
675-
curr_frame.stack.push(NativeType::Function(func));
674+
curr_frame.stack.push(Rc::new(NativeType::Function(func)));
676675
None
677676
},
678677
("CALL_FUNCTION", Some(argc)) => {
@@ -686,9 +685,9 @@ impl VirtualMachine {
686685
for _ in 0..kw_count {
687686
let native_val = curr_frame.stack.pop().unwrap();
688687
let native_key = curr_frame.stack.pop().unwrap();
689-
if let (val, NativeType::Str(key)) = (native_val, native_key) {
688+
if let (ref val, &NativeType::Str(ref key)) = (native_val, native_key.deref()) {
690689

691-
kw_args.insert(key, val);
690+
kw_args.insert(key.clone(), val.clone());
692691
}
693692
else {
694693
panic!("Incorrect type found while building keyword argument list")
@@ -704,8 +703,8 @@ impl VirtualMachine {
704703
};
705704

706705
let func = {
707-
match self.curr_frame().stack.pop().unwrap() {
708-
NativeType::Function(func) => {
706+
match self.curr_frame().stack.pop().unwrap().deref() {
707+
&NativeType::Function(ref func) => {
709708
// pop argc arguments
710709
// argument: name, args, globals
711710
// build the callargs hashmap
@@ -716,29 +715,27 @@ impl VirtualMachine {
716715
}
717716
// merge callargs with kw_args
718717
let return_value = {
719-
let frame = self.make_frame(func.code, callargs, Some(locals));
718+
let frame = self.make_frame(func.code.clone(), callargs, Some(locals));
720719
self.run_frame(frame)
721720
};
722-
self.curr_frame().stack.push(return_value);
721+
self.curr_frame().stack.push(Rc::new(return_value));
723722
},
724-
NativeType::NativeFunction(func) => {
723+
&NativeType::NativeFunction(func) => {
725724
pos_args.reverse();
726725
let return_value = func(pos_args);
727-
self.curr_frame().stack.push(return_value);
726+
self.curr_frame().stack.push(Rc::new(return_value));
728727
},
729728
_ => panic!("The item on the stack should be a code object")
730729
}
731730
};
732731
None
733732
},
734-
*/
735733
("RETURN_VALUE", None) => {
736734
// Hmmm... what is this used?
737735
// I believe we need to push this to the next frame
738736
self.curr_frame().return_value = (*self.curr_frame().stack.pop().unwrap()).clone();
739737
Some("return".to_string())
740738
},
741-
/*
742739
("SETUP_LOOP", Some(delta)) => {
743740
let curr_frame = self.curr_frame();
744741
let curr_offset = curr_frame.get_bytecode_offset().unwrap();
@@ -756,7 +753,6 @@ impl VirtualMachine {
756753
// Skip
757754
None
758755
},
759-
*/
760756
(name, _) => {
761757
panic!("Unrecongnizable op code: {}", name);
762758
}

0 commit comments

Comments
 (0)