66
77extern crate rustpython_parser;
88
9+ use std:: cell:: { Ref , RefCell } ;
910use std:: collections:: hash_map:: HashMap ;
1011use std:: collections:: hash_set:: HashSet ;
1112use std:: rc:: Rc ;
@@ -46,7 +47,7 @@ pub struct VirtualMachine {
4647 pub sys_module : PyObjectRef ,
4748 pub stdlib_inits : HashMap < String , stdlib:: StdlibInitFunc > ,
4849 pub ctx : PyContext ,
49- pub frames : Vec < PyObjectRef > ,
50+ pub frames : RefCell < Vec < PyObjectRef > > ,
5051 pub wasm_id : Option < String > ,
5152}
5253
@@ -69,7 +70,7 @@ impl VirtualMachine {
6970 sys_module : sysmod,
7071 stdlib_inits,
7172 ctx,
72- frames : vec ! [ ] ,
73+ frames : RefCell :: new ( vec ! [ ] ) ,
7374 wasm_id : None ,
7475 }
7576 }
@@ -87,21 +88,24 @@ impl VirtualMachine {
8788 }
8889
8990 pub fn run_frame ( & mut self , frame : PyObjectRef ) -> PyResult < ExecutionResult > {
90- self . frames . push ( frame. clone ( ) ) ;
91+ self . frames . borrow_mut ( ) . push ( frame. clone ( ) ) ;
9192 let frame = objframe:: get_value ( & frame) ;
9293 let result = frame. run ( self ) ;
93- self . frames . pop ( ) ;
94+ self . frames . borrow_mut ( ) . pop ( ) ;
9495 result
9596 }
9697
97- pub fn current_frame ( & self ) -> & Frame {
98- let current_frame = & self . frames [ self . frames . len ( ) - 1 ] ;
99- objframe:: get_value ( current_frame)
98+ pub fn current_frame ( & self ) -> Ref < Frame > {
99+ Ref :: map ( self . frames . borrow ( ) , |frames| {
100+ let index = frames. len ( ) - 1 ;
101+ let current_frame = & frames[ index] ;
102+ objframe:: get_value ( current_frame)
103+ } )
100104 }
101105
102- pub fn current_scope ( & self ) -> & Scope {
106+ pub fn current_scope ( & self ) -> Ref < Scope > {
103107 let frame = self . current_frame ( ) ;
104- & frame. scope
108+ Ref :: map ( frame, |f| & f . scope )
105109 }
106110
107111 pub fn class ( & mut self , module : & str , class : & str ) -> PyObjectRef {
0 commit comments