Skip to content

Commit 6ad528c

Browse files
committed
Reintroduce new_scope for the common case of an empty scope.
1 parent 0e23e70 commit 6ad528c

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: String) -> Py
8282
vm.new_exception(syntax_error, err.to_string())
8383
})?;
8484
// trace!("Code object: {:?}", code_obj.borrow());
85-
let vars = Scope::new(None, vm.ctx.new_dict()); // Keep track of local variables
85+
let vars = vm.ctx.new_scope(); // Keep track of local variables
8686
vm.run_code_obj(code_obj, vars)
8787
}
8888

@@ -164,7 +164,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
164164
"Welcome to the magnificent Rust Python {} interpreter",
165165
crate_version!()
166166
);
167-
let vars = Scope::new(None, vm.ctx.new_dict());
167+
let vars = vm.ctx.new_scope();
168168

169169
// Read a single line:
170170
let mut input = String::new();

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod tests {
3434
fn test_print_42() {
3535
let source = String::from("print('Hello world')\n");
3636
let mut vm = VirtualMachine::new();
37-
let vars = Scope::new(None, vm.ctx.new_dict());
37+
let vars = vm.ctx.new_scope();
3838
let _result = eval(&mut vm, &source, vars, "<unittest>");
3939

4040
// TODO: check result?

vm/src/pyobject.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ impl PyContext {
593593
objtype::new(self.type_type(), name, vec![base], PyAttributes::new()).unwrap()
594594
}
595595

596+
pub fn new_scope(&self) -> Scope {
597+
Scope::new(None, self.new_dict())
598+
}
599+
596600
pub fn new_module(&self, name: &str, dict: PyObjectRef) -> PyObjectRef {
597601
PyObject::new(
598602
PyObjectPayload::AnyRustValue {

wasm/lib/src/vm_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) struct StoredVirtualMachine {
2828
impl StoredVirtualMachine {
2929
fn new(id: String, inject_browser_module: bool) -> StoredVirtualMachine {
3030
let mut vm = VirtualMachine::new();
31-
let scope = Scope::new(None, vm.ctx.new_dict());
31+
let scope = vm.ctx.new_scope();
3232
if inject_browser_module {
3333
setup_browser_module(&mut vm);
3434
}

0 commit comments

Comments
 (0)