Skip to content

Commit 9404c7c

Browse files
Merge pull request RustPython#548 from rickyhan/new_vm_fns
Move set_item to DictProtocol
2 parents d5976e2 + ec93c55 commit 9404c7c

File tree

9 files changed

+41
-15
lines changed

9 files changed

+41
-15
lines changed

parser/src/parser.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ mod tests {
306306

307307
#[test]
308308
fn test_parse_class() {
309-
let source = String::from("class Foo(A, B):\n def __init__(self):\n pass\n def method_with_default(self, arg='default'):\n pass\n");
309+
let source = String::from(
310+
"class Foo(A, B):\n def __init__(self):\n pass\n def method_with_default(self, arg='default'):\n pass\n",
311+
);
310312
assert_eq!(
311313
parse_statement(&source),
312314
Ok(ast::LocatedStatement {

vm/src/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ impl Compiler {
13891389
// Low level helper functions:
13901390
fn emit(&mut self, instruction: Instruction) {
13911391
let location = self.current_source_location.clone();
1392-
let mut cur_code_obj = self.current_code_object();
1392+
let cur_code_obj = self.current_code_object();
13931393
cur_code_obj.instructions.push(instruction);
13941394
cur_code_obj.locations.push(location);
13951395
// TODO: insert source filename

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn import_module(
6060
return Ok(module);
6161
}
6262
let module = import_uncached_module(vm, current_path, module_name)?;
63-
vm.ctx.set_item(&sys_modules, module_name, module.clone());
63+
sys_modules.set_item(&vm.ctx, module_name, module.clone());
6464
Ok(module)
6565
}
6666

vm/src/obj/objobject.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::objstr;
22
use super::objtype;
33
use crate::pyobject::{
4-
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
5-
PyResult, TypeProtocol,
4+
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload,
5+
PyObjectRef, PyResult, TypeProtocol,
66
};
77
use crate::vm::VirtualMachine;
88
use std::cell::RefCell;
@@ -182,7 +182,7 @@ fn object_dict(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
182182
PyObjectPayload::Class { ref dict, .. } | PyObjectPayload::Instance { ref dict, .. } => {
183183
let new_dict = vm.new_dict();
184184
for (attr, value) in dict.borrow().iter() {
185-
vm.ctx.set_item(&new_dict, &attr, value.clone());
185+
new_dict.set_item(&vm.ctx, &attr, value.clone());
186186
}
187187
Ok(new_dict)
188188
}

vm/src/pyobject.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ pub trait DictProtocol {
833833
fn contains_key(&self, k: &str) -> bool;
834834
fn get_item(&self, k: &str) -> Option<PyObjectRef>;
835835
fn get_key_value_pairs(&self) -> Vec<(PyObjectRef, PyObjectRef)>;
836+
fn set_item(&self, ctx: &PyContext, key: &str, v: PyObjectRef);
836837
}
837838

838839
impl DictProtocol for PyObjectRef {
@@ -851,8 +852,9 @@ impl DictProtocol for PyObjectRef {
851852
PyObjectPayload::Dict { ref elements } => {
852853
objdict::content_get_key_str(&elements.borrow(), k)
853854
}
855+
PyObjectPayload::Module { ref dict, .. } => dict.get_item(k),
854856
PyObjectPayload::Scope { ref scope } => scope.borrow().locals.get_item(k),
855-
_ => panic!("TODO"),
857+
ref k => panic!("TODO {:?}", k),
856858
}
857859
}
858860

@@ -864,6 +866,23 @@ impl DictProtocol for PyObjectRef {
864866
_ => panic!("TODO"),
865867
}
866868
}
869+
870+
// Item set/get:
871+
fn set_item(&self, ctx: &PyContext, key: &str, v: PyObjectRef) {
872+
match &self.payload {
873+
PyObjectPayload::Dict { elements } => {
874+
let key = ctx.new_str(key.to_string());
875+
objdict::set_item_in_content(&mut elements.borrow_mut(), &key, &v);
876+
}
877+
PyObjectPayload::Module { dict, .. } => {
878+
dict.set_item(ctx, key, v);
879+
}
880+
PyObjectPayload::Scope { scope, .. } => {
881+
scope.borrow().locals.set_item(ctx, key, v);
882+
}
883+
ref k => panic!("TODO {:?}", k),
884+
};
885+
}
867886
}
868887

869888
pub trait BufferProtocol {

vm/src/stdlib/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'de> Visitor<'de> for PyObjectDeserializer<'de> {
171171
PyObjectPayload::String { ref value } => value.clone(),
172172
_ => unimplemented!("map keys must be strings"),
173173
};
174-
self.vm.ctx.set_item(&dict, &key, value);
174+
dict.set_item(&self.vm.ctx, &key, value);
175175
}
176176
Ok(dict)
177177
}

vm/src/sysmodule.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
1+
use crate::pyobject::{DictProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
22
use crate::vm::VirtualMachine;
33
use std::rc::Rc;
44
use std::{env, mem};
@@ -132,7 +132,7 @@ settrace() -- set the global debug tracing function
132132
"_getframe" => ctx.new_rustfunc(getframe),
133133
});
134134

135-
ctx.set_item(&modules, sys_name, sys_mod.clone());
135+
modules.set_item(&ctx, sys_name, sys_mod.clone());
136136
ctx.set_attr(&sys_mod, "modules", modules);
137137

138138
sys_mod

vm/src/vm.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::pyobject::{
2626
};
2727
use crate::stdlib;
2828
use crate::sysmodule;
29+
use num_bigint::ToBigInt;
2930

3031
// use objects::objects;
3132

@@ -53,7 +54,7 @@ impl VirtualMachine {
5354

5455
// Add builtins as builtins module:
5556
let modules = sysmod.get_attr("modules").unwrap();
56-
ctx.set_item(&modules, "builtins", builtins.clone());
57+
modules.set_item(&ctx, "builtins", builtins.clone());
5758

5859
let stdlib_inits = stdlib::get_module_inits();
5960
VirtualMachine {
@@ -76,6 +77,11 @@ impl VirtualMachine {
7677
self.ctx.new_str(s)
7778
}
7879

80+
/// Create a new python int object.
81+
pub fn new_int<T: ToBigInt>(&self, i: T) -> PyObjectRef {
82+
self.ctx.new_int(i)
83+
}
84+
7985
/// Create a new python bool object.
8086
pub fn new_bool(&self, b: bool) -> PyObjectRef {
8187
self.ctx.new_bool(b)
@@ -395,7 +401,7 @@ impl VirtualMachine {
395401

396402
self.ctx.set_attr(scope, &name, value);
397403
} else if let Some(d) = &kwargs {
398-
self.ctx.set_item(d, &name, value);
404+
d.set_item(&self.ctx, &name, value);
399405
} else {
400406
return Err(
401407
self.new_type_error(format!("Got an unexpected keyword argument '{}'", name))

wasm/lib/src/convert.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::vm_class::{AccessibleVM, WASMVirtualMachine};
22
use js_sys::{Array, ArrayBuffer, Object, Reflect, Uint8Array};
33
use rustpython_vm::obj::{objbytes, objtype};
4-
use rustpython_vm::pyobject::{self, PyFuncArgs, PyObjectRef, PyResult};
4+
use rustpython_vm::pyobject::{self, DictProtocol, PyFuncArgs, PyObjectRef, PyResult};
55
use rustpython_vm::VirtualMachine;
66
use wasm_bindgen::{closure::Closure, prelude::*, JsCast};
77

@@ -134,8 +134,7 @@ pub fn js_to_py(vm: &mut VirtualMachine, js_val: JsValue) -> PyObjectRef {
134134
for pair in object_entries(&Object::from(js_val)) {
135135
let (key, val) = pair.expect("iteration over object to not fail");
136136
let py_val = js_to_py(vm, val);
137-
vm.ctx
138-
.set_item(&dict, &String::from(js_sys::JsString::from(key)), py_val);
137+
dict.set_item(&vm.ctx, &String::from(js_sys::JsString::from(key)), py_val);
139138
}
140139
dict
141140
}

0 commit comments

Comments
 (0)