1
- use std:: cell:: RefCell ;
1
+ use std:: cell:: { Cell , RefCell } ;
2
2
use std:: fmt;
3
3
4
4
use crate :: builtins;
@@ -85,7 +85,7 @@ pub struct Frame {
85
85
stack : RefCell < Vec < PyObjectRef > > , // The main data frame of the stack machine
86
86
blocks : RefCell < Vec < Block > > , // Block frames, for controlling loops and exceptions
87
87
pub scope : Scope , // Variables
88
- lasti : RefCell < usize > , // index of last instruction ran
88
+ lasti : Cell < usize > , // index of last instruction ran
89
89
}
90
90
91
91
impl PyValue for Frame {
@@ -105,27 +105,15 @@ pub type FrameResult = PyResult<Option<ExecutionResult>>;
105
105
106
106
impl Frame {
107
107
pub fn new ( code : PyCodeRef , scope : Scope ) -> Frame {
108
- //populate the globals and locals
109
- //TODO: This is wrong, check https://github.com/nedbat/byterun/blob/31e6c4a8212c35b5157919abff43a7daa0f377c6/byterun/pyvm2.py#L95
110
- /*
111
- let globals = match globals {
112
- Some(g) => g,
113
- None => HashMap::new(),
114
- };
115
- */
116
- // let locals = globals;
117
- // locals.extend(callargs);
118
108
let code = code. code . clone ( ) ;
119
- let max_stack_size = 32 ; // code.max_stack_size;
109
+ let max_stack_size = code. max_stack_size ;
120
110
121
111
Frame {
122
112
code,
123
113
stack : RefCell :: new ( Vec :: with_capacity ( max_stack_size) ) ,
124
114
blocks : RefCell :: new ( vec ! [ ] ) ,
125
- // save the callargs as locals
126
- // globals: locals.clone(),
127
115
scope,
128
- lasti : RefCell :: new ( 0 ) ,
116
+ lasti : Cell :: new ( 0 ) ,
129
117
}
130
118
}
131
119
@@ -191,8 +179,8 @@ impl Frame {
191
179
}
192
180
193
181
pub fn fetch_instruction ( & self ) -> & bytecode:: Instruction {
194
- let ins2 = & self . code . instructions [ * self . lasti . borrow ( ) ] ;
195
- * self . lasti . borrow_mut ( ) += 1 ;
182
+ let ins2 = & self . code . instructions [ self . get_lasti ( ) ] ;
183
+ self . lasti . set ( self . lasti . get ( ) + 1 ) ;
196
184
ins2
197
185
}
198
186
@@ -951,7 +939,7 @@ impl Frame {
951
939
match next_obj {
952
940
Some ( value) => {
953
941
// Set back program counter:
954
- * self . lasti . borrow_mut ( ) -= 1 ;
942
+ self . lasti . set ( self . get_lasti ( ) - 1 ) ;
955
943
Ok ( Some ( ExecutionResult :: Yield ( value) ) )
956
944
}
957
945
None => Ok ( None ) ,
@@ -1024,7 +1012,7 @@ impl Frame {
1024
1012
}
1025
1013
1026
1014
pub fn get_lasti ( & self ) -> usize {
1027
- * self . lasti . borrow ( )
1015
+ self . lasti . get ( )
1028
1016
}
1029
1017
1030
1018
fn execute_make_function (
@@ -1225,7 +1213,7 @@ impl Frame {
1225
1213
}
1226
1214
1227
1215
pub fn get_lineno ( & self ) -> bytecode:: Location {
1228
- self . code . locations [ * self . lasti . borrow ( ) ] . clone ( )
1216
+ self . code . locations [ self . get_lasti ( ) ] . clone ( )
1229
1217
}
1230
1218
1231
1219
fn push_block ( & self , typ : BlockType ) {
@@ -1251,7 +1239,6 @@ impl Frame {
1251
1239
1252
1240
pub fn push_value ( & self , obj : PyObjectRef ) {
1253
1241
self . stack . borrow_mut ( ) . push ( obj) ;
1254
- // println!("Stack size: {}, capa: {}", self.stack.borrow().len(), self.stack.borrow().capacity());
1255
1242
}
1256
1243
1257
1244
fn pop_value ( & self ) -> PyObjectRef {
0 commit comments