6
6
7
7
extern crate rustpython_parser;
8
8
9
+ use std:: cell:: { Ref , RefCell } ;
9
10
use std:: collections:: hash_map:: HashMap ;
10
11
use std:: collections:: hash_set:: HashSet ;
11
12
use std:: rc:: Rc ;
@@ -46,7 +47,7 @@ pub struct VirtualMachine {
46
47
pub sys_module : PyObjectRef ,
47
48
pub stdlib_inits : HashMap < String , stdlib:: StdlibInitFunc > ,
48
49
pub ctx : PyContext ,
49
- pub frames : Vec < PyObjectRef > ,
50
+ pub frames : RefCell < Vec < PyObjectRef > > ,
50
51
pub wasm_id : Option < String > ,
51
52
}
52
53
@@ -69,7 +70,7 @@ impl VirtualMachine {
69
70
sys_module : sysmod,
70
71
stdlib_inits,
71
72
ctx,
72
- frames : vec ! [ ] ,
73
+ frames : RefCell :: new ( vec ! [ ] ) ,
73
74
wasm_id : None ,
74
75
}
75
76
}
@@ -87,21 +88,24 @@ impl VirtualMachine {
87
88
}
88
89
89
90
pub fn run_frame ( & mut self , frame : PyObjectRef ) -> PyResult < ExecutionResult > {
90
- self . frames . push ( frame. clone ( ) ) ;
91
+ self . frames . borrow_mut ( ) . push ( frame. clone ( ) ) ;
91
92
let frame = objframe:: get_value ( & frame) ;
92
93
let result = frame. run ( self ) ;
93
- self . frames . pop ( ) ;
94
+ self . frames . borrow_mut ( ) . pop ( ) ;
94
95
result
95
96
}
96
97
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
+ } )
100
104
}
101
105
102
- pub fn current_scope ( & self ) -> & Scope {
106
+ pub fn current_scope ( & self ) -> Ref < Scope > {
103
107
let frame = self . current_frame ( ) ;
104
- & frame. scope
108
+ Ref :: map ( frame, |f| & f . scope )
105
109
}
106
110
107
111
pub fn class ( & mut self , module : & str , class : & str ) -> PyObjectRef {
0 commit comments