11pub mod instructions;
22pub mod frame;
33
4- use super :: objects:: { Code , ObjectStore , ObjectRef , ObjectContent , PrimitiveObjects , Object } ;
5- use super :: varstack:: { VarStack , VectorVarStack } ;
4+ use super :: objects:: { ObjectRef , ObjectContent , Object } ;
5+ use super :: varstack:: VarStack ;
66use self :: instructions:: { CmpOperator , Instruction } ;
77use self :: frame:: { Block , Frame } ;
88use std:: fmt;
99use std:: collections:: HashMap ;
1010use std:: io:: Read ;
1111use std:: cell:: RefCell ;
1212use std:: rc:: Rc ;
13- use std:: iter:: FromIterator ;
1413use super :: marshal;
1514use super :: state:: { State , PyResult , unwind, raise, return_value} ;
1615use super :: sandbox:: EnvProxy ;
@@ -141,11 +140,11 @@ fn call_function<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame
141140 let frame = call_stack. last_mut ( ) . unwrap ( ) ;
142141 frame. var_stack . push ( func_ref. new_instance ( & mut state. store , args, kwargs) )
143142 } ,
144- ObjectContent :: Function ( ref func_module , ref code_ref, ref defaults) => {
143+ ObjectContent :: Function ( ref _func_module , ref code_ref, ref defaults) => {
145144 let code = state. store . deref ( code_ref) . content . clone ( ) ;
146145 if let ObjectContent :: Code ( code) = code {
147146
148- let mut locals = Rc :: new ( RefCell :: new ( defaults. clone ( ) ) ) ;
147+ let mut locals = defaults. clone ( ) ;
149148
150149 if let Some ( starargs_name) = code. get_varargs_name ( ) { // If it has a *args argument
151150 if code. argcount > args. len ( ) {
@@ -155,7 +154,7 @@ fn call_function<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame
155154 let obj_ref = state. store . allocate ( state. primitive_objects . new_tuple ( to_vararg) ) ;
156155
157156 // Bind *args
158- assert_eq ! ( None , locals. borrow_mut ( ) . insert( starargs_name. clone( ) , obj_ref) ) ;
157+ assert_eq ! ( None , locals. insert( starargs_name. clone( ) , obj_ref) ) ;
159158 }
160159 else if code. argcount != args. len ( ) { // If it has no *args argument
161160 panic ! ( format!( "{}() takes {} arguments, but {} was/were given." , code. name, code. argcount, args. len( ) ) )
@@ -165,7 +164,6 @@ fn call_function<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame
165164 let mut remaining_kwargs = vec ! [ ] ; // arguments that will go to **kwargs
166165 {
167166 let explicit_keywords = code. keywords ( ) ;
168- let mut locals = locals. borrow_mut ( ) ;
169167 for ( key, value) in kwargs. into_iter ( ) {
170168 let key_str = match state. store . deref ( & key) . content {
171169 ObjectContent :: String ( ref s) => s,
@@ -182,7 +180,7 @@ fn call_function<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame
182180
183181 if let Some ( starkwargs_name) = code. get_varkwargs_name ( ) { // If it has a **kwargs argument
184182 let obj_ref = state. store . allocate ( state. primitive_objects . new_dict ( remaining_kwargs) ) ;
185- locals. borrow_mut ( ) . insert ( starkwargs_name. clone ( ) , obj_ref) ;
183+ locals. insert ( starkwargs_name. clone ( ) , obj_ref) ;
186184 }
187185 else { // If it has no **kwargs argument
188186 if remaining_kwargs. len ( ) != 0 {
@@ -192,13 +190,12 @@ fn call_function<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame
192190
193191 // Bind positional arguments
194192 {
195- let mut locals = locals. borrow_mut ( ) ;
196193 for ( argname, argvalue) in code. varnames . iter ( ) . zip ( args) {
197194 locals. insert ( argname. clone ( ) , argvalue) ;
198195 } ;
199196 }
200197
201- let new_frame = Frame :: new ( func_ref. clone ( ) , * code, locals) ;
198+ let new_frame = Frame :: new ( func_ref. clone ( ) , * code, Rc :: new ( RefCell :: new ( locals) ) ) ;
202199 call_stack. push ( new_frame) ;
203200 }
204201 else {
@@ -350,9 +347,9 @@ fn run_code<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame>) ->
350347 Instruction :: PopExcept => {
351348 let frame = call_stack. last_mut ( ) . unwrap ( ) ;
352349 let mut three_last = frame. var_stack . pop_all_and_get_n_last ( 3 ) . unwrap ( ) ; // TODO: check
353- let exc_type = three_last. pop ( ) ;
354- let exc_value = three_last. pop ( ) ;
355- let exc_traceback = three_last. pop ( ) ;
350+ let _exc_type = three_last. pop ( ) ;
351+ let _exc_value = three_last. pop ( ) ;
352+ let _exc_traceback = three_last. pop ( ) ;
356353 // TODO: do something with exc_*
357354 pop_stack ! ( state, frame. block_stack) ;
358355 } ,
@@ -409,7 +406,7 @@ fn run_code<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame>) ->
409406 }
410407 Instruction :: BuildTuple ( size) => {
411408 let frame = call_stack. last_mut ( ) . unwrap ( ) ;
412- let mut content = py_unwrap ! ( state, frame. var_stack. pop_many( size) , ProcessorError :: StackTooSmall ) ;
409+ let content = py_unwrap ! ( state, frame. var_stack. pop_many( size) , ProcessorError :: StackTooSmall ) ;
413410 let tuple = state. primitive_objects . new_tuple ( content) ;
414411 frame. var_stack . push ( state. store . allocate ( tuple) ) ;
415412 }
@@ -515,9 +512,9 @@ fn run_code<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame>) ->
515512 Instruction :: CallFunction ( nb_args, nb_kwargs) => {
516513 // See “Call constructs” at:
517514 // http://security.coverity.com/blog/2014/Nov/understanding-python-bytecode.html
518- let mut kwargs;
519- let mut args;
520- let mut func;
515+ let kwargs;
516+ let args;
517+ let func;
521518 {
522519 let frame = call_stack. last_mut ( ) . unwrap ( ) ;
523520 kwargs = py_unwrap ! ( state, frame. var_stack. pop_n_pairs( nb_kwargs) , ProcessorError :: StackTooSmall ) ;
@@ -571,13 +568,13 @@ fn call_module_code<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Fr
571568 } ;
572569 let module_obj = state. store . allocate ( state. primitive_objects . new_module ( module_name. clone ( ) , code_ref) ) ;
573570 state. modules . insert ( module_name. clone ( ) , Rc :: new ( RefCell :: new ( HashMap :: new ( ) ) ) ) ;
574- let mut call_stack = vec ! [ Frame :: new( module_obj, * code, state. modules. get( & module_name) . unwrap( ) . clone( ) ) ] ;
575- let res = run_code ( state, & mut call_stack) ;
571+ call_stack. push ( Frame :: new ( module_obj, * code, state. modules . get ( & module_name) . unwrap ( ) . clone ( ) ) ) ;
572+ let res = run_code ( state, call_stack) ;
576573 res // Do not raise exceptions before the pop()
577574}
578575
579576/// Get the code of a module from its name
580- pub fn get_module_code < EP : EnvProxy > ( state : & mut State < EP > , call_stack : & mut Vec < Frame > , module_name : String ) -> PyResult {
577+ pub fn get_module_code < EP : EnvProxy > ( state : & mut State < EP > , module_name : String ) -> PyResult {
581578 // Load the code
582579 let mut module_bytecode = state. envproxy . open_module ( module_name. clone ( ) ) ;
583580 let mut buf = [ 0 ; 12 ] ;
@@ -587,20 +584,14 @@ pub fn get_module_code<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec
587584 }
588585 match marshal:: read_object ( & mut module_bytecode, & mut state. store , & state. primitive_objects ) {
589586 Err ( e) => state. raise_processor_error ( ProcessorError :: UnmarshalError ( e) ) ,
590- Ok ( module_code_ref) => {
591- let module_code = match state. store . deref ( & module_code_ref) . content {
592- ObjectContent :: Code ( ref code) => code. clone ( ) ,
593- ref o => return state. raise_processor_error ( ProcessorError :: NotACodeObject ( format ! ( "module code {:?}" , o) ) ) ,
594- } ;
595- PyResult :: Return ( state. store . allocate ( state. primitive_objects . new_module ( module_name. clone ( ) , module_code_ref) ) )
596- }
587+ Ok ( module_code_ref) => PyResult :: Return ( state. store . allocate ( state. primitive_objects . new_module ( module_name. clone ( ) , module_code_ref) ) ) ,
597588 }
598589}
599590
600591/// Entry point to run code. Loads builtins in the code's namespace and then run it.
601592pub fn call_main_code < EP : EnvProxy > ( state : & mut State < EP > , code_ref : ObjectRef ) -> PyResult {
602593 let mut call_stack = Vec :: new ( ) ;
603- let builtins_code_ref = py_try ! ( get_module_code( state, & mut call_stack , "builtins" . to_string( ) ) ) ;
594+ let builtins_code_ref = py_try ! ( get_module_code( state, "builtins" . to_string( ) ) ) ;
604595 py_try ! ( call_module_code( state, & mut call_stack, "builtins" . to_string( ) , builtins_code_ref) ) ;
605596
606597 let mut call_stack = Vec :: new ( ) ;
0 commit comments