Skip to content

Commit e657633

Browse files
committed
Move set_item to context struct.
1 parent 4095e0c commit e657633

File tree

6 files changed

+40
-42
lines changed

6 files changed

+40
-42
lines changed

vm/src/builtins.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use super::obj::objiter;
1414
use super::obj::objstr;
1515
use super::obj::objtype;
1616
use super::pyobject::{
17-
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind,
18-
PyObjectRef, PyResult, Scope, TypeProtocol,
17+
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
18+
PyResult, Scope, TypeProtocol,
1919
};
2020
use super::vm::VirtualMachine;
2121
use num_bigint::ToBigInt;
@@ -730,8 +730,9 @@ fn builtin_zip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
730730
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
731731
let mod_name = "__builtins__".to_string();
732732
let py_mod = ctx.new_module(&mod_name, ctx.new_scope(None));
733+
733734
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
734-
py_mod.set_item("__name__", ctx.new_str(String::from("__main__")));
735+
ctx.set_attr(&py_mod, "__name__", ctx.new_str(String::from("__main__")));
735736

736737
ctx.set_item(&py_mod, "abs", ctx.new_rustfunc(builtin_abs));
737738
ctx.set_attr(&py_mod, "all", ctx.new_rustfunc(builtin_all));

vm/src/frame.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Frame {
183183
ref symbol,
184184
} => self.import(vm, name, symbol),
185185
bytecode::Instruction::LoadName { ref name } => self.load_name(vm, name),
186-
bytecode::Instruction::StoreName { ref name } => self.store_name(name),
186+
bytecode::Instruction::StoreName { ref name } => self.store_name(vm, name),
187187
bytecode::Instruction::DeleteName { ref name } => self.delete_name(vm, name),
188188
bytecode::Instruction::StoreSubscript => self.execute_store_subscript(vm),
189189
bytecode::Instruction::DeleteSubscript => self.execute_delete_subscript(vm),
@@ -796,9 +796,9 @@ impl Frame {
796796
vm.call_method(context_manager, "__exit__", args)
797797
}
798798

799-
fn store_name(&mut self, name: &str) -> FrameResult {
799+
fn store_name(&mut self, vm: &mut VirtualMachine, name: &str) -> FrameResult {
800800
let obj = self.pop_value();
801-
self.locals.set_item(name, obj);
801+
vm.ctx.set_item(&self.locals, name, obj);
802802
Ok(None)
803803
}
804804

vm/src/import.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ fn import_uncached_module(
4141

4242
let builtins = vm.get_builtin_scope();
4343
let scope = vm.ctx.new_scope(Some(builtins));
44-
scope.set_item(&"__name__".to_string(), vm.new_str(module.to_string()));
44+
vm.ctx
45+
.set_item(&scope, "__name__", vm.new_str(module.to_string()));
4546
vm.run_code_obj(code_obj, scope.clone())?;
4647
Ok(vm.ctx.new_module(module, scope))
4748
}
@@ -57,7 +58,7 @@ pub fn import_module(
5758
return Ok(module);
5859
}
5960
let module = import_uncached_module(vm, current_path, module_name)?;
60-
sys_modules.set_item(module_name, module.clone());
61+
vm.ctx.set_item(&sys_modules, module_name, module.clone());
6162
Ok(module)
6263
}
6364

vm/src/pyobject.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl PyContext {
507507

508508
pub fn new_member_descriptor(&self, function: RustPyFunc) -> PyObjectRef {
509509
let dict = self.new_dict();
510-
dict.set_item(&String::from("function"), self.new_rustfunc(function));
510+
self.set_item(&dict, "function", self.new_rustfunc(function));
511511
self.new_instance(dict, self.member_descriptor_type())
512512
}
513513

@@ -517,7 +517,22 @@ impl PyContext {
517517

518518
// Item set/get:
519519
pub fn set_item(&self, obj: &PyObjectRef, key: &str, v: PyObjectRef) {
520-
obj.set_item(key, v);
520+
match obj.borrow_mut().kind {
521+
PyObjectKind::Dict {
522+
elements: ref mut el,
523+
} => {
524+
// objdict::set_item_in_elements(elements, key, v);
525+
el.insert(key.to_string(), v);
526+
}
527+
PyObjectKind::Module {
528+
name: _,
529+
ref mut dict,
530+
} => self.set_item(dict, key, v),
531+
PyObjectKind::Scope { ref mut scope } => {
532+
self.set_item(&scope.locals, key, v);
533+
}
534+
ref k => panic!("TODO {:?}", k),
535+
};
521536
}
522537

523538
pub fn get_attr(&self, obj: &PyObjectRef, attr_name: &str) -> Option<PyObjectRef> {
@@ -529,13 +544,13 @@ impl PyContext {
529544

530545
pub fn set_attr(&self, obj: &PyObjectRef, attr_name: &str, value: PyObjectRef) {
531546
match obj.borrow().kind {
532-
PyObjectKind::Module { name: _, ref dict } => dict.set_item(attr_name, value),
533-
PyObjectKind::Instance { ref dict } => dict.set_item(attr_name, value),
547+
PyObjectKind::Module { name: _, ref dict } => self.set_item(dict, attr_name, value),
548+
PyObjectKind::Instance { ref dict } => self.set_item(dict, attr_name, value),
534549
PyObjectKind::Class {
535550
name: _,
536551
ref dict,
537552
mro: _,
538-
} => dict.set_item(attr_name, value),
553+
} => self.set_item(dict, attr_name, value),
539554
ref kind => unimplemented!("set_attr unimplemented for: {:?}", kind),
540555
};
541556
}
@@ -668,7 +683,6 @@ impl AttributeProtocol for PyObjectRef {
668683
pub trait DictProtocol {
669684
fn contains_key(&self, k: &str) -> bool;
670685
fn get_item(&self, k: &str) -> Option<PyObjectRef>;
671-
fn set_item(&self, k: &str, v: PyObjectRef);
672686
}
673687

674688
impl DictProtocol for PyObjectRef {
@@ -692,25 +706,6 @@ impl DictProtocol for PyObjectRef {
692706
_ => panic!("TODO"),
693707
}
694708
}
695-
696-
fn set_item(&self, k: &str, v: PyObjectRef) {
697-
match self.borrow_mut().kind {
698-
PyObjectKind::Dict {
699-
elements: ref mut el,
700-
} => {
701-
// objdict::set_item_in_elements(elements, key, v);
702-
el.insert(k.to_string(), v);
703-
}
704-
PyObjectKind::Module {
705-
name: _,
706-
ref mut dict,
707-
} => dict.set_item(k, v),
708-
PyObjectKind::Scope { ref mut scope } => {
709-
scope.locals.set_item(k, v);
710-
}
711-
ref k => panic!("TODO {:?}", k),
712-
};
713-
}
714709
}
715710

716711
impl fmt::Debug for PyObject {

vm/src/stdlib/json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde_json;
77

88
use super::super::obj::{objbool, objdict, objfloat, objint, objsequence, objstr, objtype};
99
use super::super::pyobject::{
10-
DictProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
10+
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
1111
};
1212
use super::super::VirtualMachine;
1313
use num_bigint::ToBigInt;
@@ -170,7 +170,7 @@ impl<'de> serde::de::DeserializeSeed<'de> for PyObjectDeserializer<'de> {
170170
PyObjectKind::String { ref value } => value.clone(),
171171
_ => unimplemented!("map keys must be strings"),
172172
};
173-
dict.set_item(&key, value);
173+
self.ctx.set_item(&dict, &key, value);
174174
}
175175
Ok(dict)
176176
}

vm/src/vm.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl VirtualMachine {
4949

5050
// Add builtins as builtins module:
5151
let modules = sysmod.get_attr("modules").unwrap();
52-
modules.set_item("builtins", builtins.clone());
52+
ctx.set_item(&modules, "builtins", builtins.clone());
5353

5454
let stdlib_inits = stdlib::get_module_inits();
5555
VirtualMachine {
@@ -301,7 +301,7 @@ impl VirtualMachine {
301301
for i in 0..n {
302302
let arg_name = &code_object.arg_names[i];
303303
let arg = &args.args[i];
304-
scope.set_item(arg_name, arg.clone());
304+
self.ctx.set_item(scope, arg_name, arg.clone());
305305
}
306306

307307
// Pack other positional arguments in to *args:
@@ -315,7 +315,7 @@ impl VirtualMachine {
315315

316316
// If we have a name (not '*' only) then store it:
317317
if let Some(vararg_name) = vararg {
318-
scope.set_item(vararg_name, vararg_value);
318+
self.ctx.set_item(scope, vararg_name, vararg_value);
319319
}
320320
} else {
321321
// Check the number of positional arguments
@@ -333,7 +333,7 @@ impl VirtualMachine {
333333

334334
// Store when we have a name:
335335
if let Some(kwargs_name) = kwargs {
336-
scope.set_item(&kwargs_name, d.clone());
336+
self.ctx.set_item(scope, &kwargs_name, d.clone());
337337
}
338338

339339
Some(d)
@@ -352,9 +352,9 @@ impl VirtualMachine {
352352
);
353353
}
354354

355-
scope.set_item(&name, value);
355+
self.ctx.set_item(scope, &name, value);
356356
} else if let Some(d) = &kwargs {
357-
d.set_item(&name, value);
357+
self.ctx.set_item(d, &name, value);
358358
} else {
359359
return Err(
360360
self.new_type_error(format!("Got an unexpected keyword argument '{}'", name))
@@ -395,7 +395,8 @@ impl VirtualMachine {
395395
for i in required_args..nexpected_args {
396396
let arg_name = &code_object.arg_names[i];
397397
if !scope.contains_key(arg_name) {
398-
scope.set_item(arg_name, available_defaults[default_index].clone());
398+
self.ctx
399+
.set_item(scope, arg_name, available_defaults[default_index].clone());
399400
}
400401
default_index += 1;
401402
}

0 commit comments

Comments
 (0)