@@ -18,7 +18,6 @@ use crate::frame::{Scope, ScopeRef};
18
18
use crate :: pyobject:: {
19
19
AttributeProtocol , IdProtocol , PyContext , PyFuncArgs , PyObjectRef , PyResult , TypeProtocol ,
20
20
} ;
21
- use std:: rc:: Rc ;
22
21
23
22
#[ cfg( not( target_arch = "wasm32" ) ) ]
24
23
use crate :: stdlib:: io:: io_open;
@@ -192,7 +191,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
192
191
args,
193
192
required = [ ( source, None ) ] ,
194
193
optional = [
195
- ( _globals , Some ( vm. ctx. dict_type( ) ) ) ,
194
+ ( globals , Some ( vm. ctx. dict_type( ) ) ) ,
196
195
( locals, Some ( vm. ctx. dict_type( ) ) )
197
196
]
198
197
) ;
@@ -215,7 +214,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
215
214
return Err ( vm. new_type_error ( "code argument must be str or code object" . to_string ( ) ) ) ;
216
215
} ;
217
216
218
- let scope = make_scope ( vm, locals) ;
217
+ let scope = make_scope ( vm, globals , locals) ;
219
218
220
219
// Run the source:
221
220
vm. run_code_obj ( code_obj. clone ( ) , scope)
@@ -229,7 +228,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
229
228
args,
230
229
required = [ ( source, None ) ] ,
231
230
optional = [
232
- ( _globals , Some ( vm. ctx. dict_type( ) ) ) ,
231
+ ( globals , Some ( vm. ctx. dict_type( ) ) ) ,
233
232
( locals, Some ( vm. ctx. dict_type( ) ) )
234
233
]
235
234
) ;
@@ -252,26 +251,28 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
252
251
return Err ( vm. new_type_error ( "source argument must be str or code object" . to_string ( ) ) ) ;
253
252
} ;
254
253
255
- let scope = make_scope ( vm, locals) ;
254
+ let scope = make_scope ( vm, globals , locals) ;
256
255
257
256
// Run the code:
258
257
vm. run_code_obj ( code_obj, scope)
259
258
}
260
259
261
- fn make_scope ( vm : & mut VirtualMachine , locals : Option < & PyObjectRef > ) -> ScopeRef {
262
- // handle optional global and locals
263
- let locals = if let Some ( locals) = locals {
264
- locals. clone ( )
265
- } else {
266
- vm. new_dict ( )
260
+ fn make_scope (
261
+ vm : & mut VirtualMachine ,
262
+ globals : Option < & PyObjectRef > ,
263
+ locals : Option < & PyObjectRef > ,
264
+ ) -> ScopeRef {
265
+ let current_scope = vm. current_scope ( ) ;
266
+ let parent = match globals {
267
+ Some ( dict) => Some ( Scope :: new ( dict. clone ( ) , Some ( vm. get_builtin_scope ( ) ) ) ) ,
268
+ None => current_scope. parent . clone ( ) ,
269
+ } ;
270
+ let locals = match locals {
271
+ Some ( dict) => dict. clone ( ) ,
272
+ None => current_scope. locals . clone ( ) ,
267
273
} ;
268
274
269
- // TODO: handle optional globals
270
- // Construct new scope:
271
- Rc :: new ( Scope {
272
- locals,
273
- parent : None ,
274
- } )
275
+ Scope :: new ( locals, parent)
275
276
}
276
277
277
278
fn builtin_format ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
0 commit comments