Skip to content

Commit 159daf0

Browse files
committed
Change scope from using set_item to set_attr.
1 parent 163a131 commit 159daf0

File tree

8 files changed

+54
-51
lines changed

8 files changed

+54
-51
lines changed

vm/src/builtins.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
213213
return Err(vm.new_type_error("code argument must be str or code object".to_string()));
214214
};
215215

216-
let locals = if let Some(locals) = locals {
217-
locals.clone()
218-
} else {
219-
vm.new_dict()
220-
};
221-
222-
// TODO: handle optional globals
223-
// Construct new scope:
224-
let scope_inner = Scope {
225-
locals,
226-
parent: None,
227-
};
228-
let scope = PyObject {
229-
payload: PyObjectPayload::Scope { scope: scope_inner },
230-
typ: None,
231-
}
232-
.into_ref();
216+
let scope = make_scope(vm, locals);
233217

234218
// Run the source:
235219
vm.run_code_obj(code_obj.clone(), scope)
@@ -266,28 +250,32 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
266250
return Err(vm.new_type_error("source argument must be str or code object".to_string()));
267251
};
268252

253+
let scope = make_scope(vm, locals);
254+
255+
// Run the code:
256+
vm.run_code_obj(code_obj, scope)
257+
}
258+
259+
fn make_scope(vm: &mut VirtualMachine, locals: Option<&PyObjectRef>) -> PyObjectRef {
269260
// handle optional global and locals
270261
let locals = if let Some(locals) = locals {
271262
locals.clone()
272263
} else {
273264
vm.new_dict()
274265
};
275266

276-
// TODO: use globals
277-
267+
// TODO: handle optional globals
278268
// Construct new scope:
279269
let scope_inner = Scope {
280270
locals,
281271
parent: None,
282272
};
283-
let scope = PyObject {
273+
274+
PyObject {
284275
payload: PyObjectPayload::Scope { scope: scope_inner },
285276
typ: None,
286277
}
287-
.into_ref();
288-
289-
// Run the code:
290-
vm.run_code_obj(code_obj, scope)
278+
.into_ref()
291279
}
292280

293281
fn builtin_format(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ impl Frame {
702702

703703
for (k, v) in obj.get_key_value_pairs().iter() {
704704
vm.ctx
705-
.set_item(&self.locals, &objstr::get_value(k), v.clone());
705+
.set_attr(&self.locals, &objstr::get_value(k), v.clone());
706706
}
707707
Ok(None)
708708
}
@@ -829,7 +829,7 @@ impl Frame {
829829

830830
fn store_name(&mut self, vm: &mut VirtualMachine, name: &str) -> FrameResult {
831831
let obj = self.pop_value();
832-
vm.ctx.set_item(&self.locals, name, obj);
832+
vm.ctx.set_attr(&self.locals, name, obj);
833833
Ok(None)
834834
}
835835

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn import_uncached_module(
4444
let builtins = vm.get_builtin_scope();
4545
let scope = vm.ctx.new_scope(Some(builtins));
4646
vm.ctx
47-
.set_item(&scope, "__name__", vm.new_str(module.to_string()));
47+
.set_attr(&scope, "__name__", vm.new_str(module.to_string()));
4848
vm.run_code_obj(code_obj, scope.clone())?;
4949
Ok(vm.ctx.new_module(module, scope))
5050
}

vm/src/obj/objdict.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::super::pyobject::{
2-
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
2+
PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult,
3+
TypeProtocol,
34
};
45
use super::super::vm::{ReprGuard, VirtualMachine};
56
use super::objiter;
@@ -114,6 +115,25 @@ pub fn content_contains_key_str(elements: &DictContentType, key: &str) -> bool {
114115
elements.get(key).is_some()
115116
}
116117

118+
/// Take a python dictionary and convert it to attributes.
119+
pub fn py_dict_to_attributes(dict: &PyObjectRef) -> PyAttributes {
120+
let mut attrs = PyAttributes::new();
121+
for (key, value) in get_key_value_pairs(dict) {
122+
let key = objstr::get_value(&key);
123+
attrs.insert(key, value);
124+
}
125+
attrs
126+
}
127+
128+
pub fn attributes_to_py_dict(vm: &mut VirtualMachine, attributes: PyAttributes) -> PyResult {
129+
let dict = vm.ctx.new_dict();
130+
for (key, value) in attributes {
131+
let key = vm.ctx.new_str(key);
132+
set_item(&dict, vm, &key, &value);
133+
}
134+
Ok(dict)
135+
}
136+
117137
// Python dict methods:
118138

119139
fn dict_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/obj/objtype.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,17 @@ pub fn type_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
126126
let mut bases = vm.extract_elements(bases)?;
127127
bases.push(vm.context().object());
128128
let name = objstr::get_value(name);
129-
new(typ.clone(), &name, bases, py_dict_to_attributes(dict))
129+
new(
130+
typ.clone(),
131+
&name,
132+
bases,
133+
objdict::py_dict_to_attributes(dict),
134+
)
130135
} else {
131136
Err(vm.new_type_error(format!(": type_new: {:?}", args)))
132137
}
133138
}
134139

135-
/// Take a python dictionary and convert it to attributes.
136-
fn py_dict_to_attributes(dict: &PyObjectRef) -> PyAttributes {
137-
let mut attrs = PyAttributes::new();
138-
for (key, value) in objdict::get_key_value_pairs(dict) {
139-
let key = objstr::get_value(&key);
140-
attrs.insert(key, value);
141-
}
142-
attrs
143-
}
144-
145140
pub fn type_call(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
146141
debug!("type_call: {:?}", args);
147142
let cls = args.shift();

vm/src/pyobject.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ pub struct PyContext {
158158
*/
159159
#[derive(Debug)]
160160
pub struct Scope {
161-
pub locals: PyObjectRef, // Variables
161+
pub locals: PyObjectRef, // Variables
162+
// TODO: pub locals: RefCell<PyAttributes>, // Variables
162163
pub parent: Option<PyObjectRef>, // Parent scope
163164
}
164165

@@ -645,10 +646,6 @@ impl PyContext {
645646
let key = self.new_str(key.to_string());
646647
objdict::set_item_in_content(elements, &key, &v);
647648
}
648-
PyObjectPayload::Module { ref mut dict, .. } => self.set_item(dict, key, v),
649-
PyObjectPayload::Scope { ref mut scope } => {
650-
self.set_item(&scope.locals, key, v);
651-
}
652649
ref k => panic!("TODO {:?}", k),
653650
};
654651
}
@@ -662,10 +659,13 @@ impl PyContext {
662659

663660
pub fn set_attr(&self, obj: &PyObjectRef, attr_name: &str, value: PyObjectRef) {
664661
match obj.borrow().payload {
665-
PyObjectPayload::Module { ref dict, .. } => self.set_item(dict, attr_name, value),
662+
PyObjectPayload::Module { ref dict, .. } => self.set_attr(dict, attr_name, value),
666663
PyObjectPayload::Instance { ref dict } | PyObjectPayload::Class { ref dict, .. } => {
667664
dict.borrow_mut().insert(attr_name.to_string(), value);
668665
}
666+
PyObjectPayload::Scope { ref scope } => {
667+
self.set_item(&scope.locals, attr_name, value);
668+
}
669669
ref payload => unimplemented!("set_attr unimplemented for: {:?}", payload),
670670
};
671671
}

vm/src/stdlib/tokenize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
2929
let py_mod = ctx.new_module("tokenize", ctx.new_scope(None));
3030

3131
// Number theory functions:
32-
ctx.set_item(&py_mod, "tokenize", ctx.new_rustfunc(tokenize_tokenize));
32+
ctx.set_attr(&py_mod, "tokenize", ctx.new_rustfunc(tokenize_tokenize));
3333

3434
py_mod
3535
}

vm/src/vm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl VirtualMachine {
340340
for i in 0..n {
341341
let arg_name = &code_object.arg_names[i];
342342
let arg = &args.args[i];
343-
self.ctx.set_item(scope, arg_name, arg.clone());
343+
self.ctx.set_attr(scope, arg_name, arg.clone());
344344
}
345345

346346
// Pack other positional arguments in to *args:
@@ -354,7 +354,7 @@ impl VirtualMachine {
354354

355355
// If we have a name (not '*' only) then store it:
356356
if let Some(vararg_name) = vararg {
357-
self.ctx.set_item(scope, vararg_name, vararg_value);
357+
self.ctx.set_attr(scope, vararg_name, vararg_value);
358358
}
359359
} else {
360360
// Check the number of positional arguments
@@ -372,7 +372,7 @@ impl VirtualMachine {
372372

373373
// Store when we have a name:
374374
if let Some(kwargs_name) = kwargs {
375-
self.ctx.set_item(scope, &kwargs_name, d.clone());
375+
self.ctx.set_attr(scope, &kwargs_name, d.clone());
376376
}
377377

378378
Some(d)
@@ -391,7 +391,7 @@ impl VirtualMachine {
391391
);
392392
}
393393

394-
self.ctx.set_item(scope, &name, value);
394+
self.ctx.set_attr(scope, &name, value);
395395
} else if let Some(d) = &kwargs {
396396
self.ctx.set_item(d, &name, value);
397397
} else {
@@ -434,7 +434,7 @@ impl VirtualMachine {
434434
let arg_name = &code_object.arg_names[i];
435435
if !scope.contains_key(arg_name) {
436436
self.ctx
437-
.set_item(scope, arg_name, available_defaults[default_index].clone());
437+
.set_attr(scope, arg_name, available_defaults[default_index].clone());
438438
}
439439
}
440440
};

0 commit comments

Comments
 (0)