Skip to content

Commit 9ed051e

Browse files
committed
Module Initialisation takes VirtualMachine rather than PyContext.
1 parent 7438b06 commit 9ed051e

27 files changed

+151
-119
lines changed

vm/src/builtins.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::obj::objtype::{self, PyClassRef};
2020
use crate::frame::Scope;
2121
use crate::function::{Args, OptionalArg, PyFuncArgs};
2222
use crate::pyobject::{
23-
DictProtocol, IdProtocol, PyContext, PyIterable, PyObjectRef, PyResult, PyValue, TryFromObject,
23+
DictProtocol, IdProtocol, PyIterable, PyObjectRef, PyResult, PyValue, TryFromObject,
2424
TypeProtocol,
2525
};
2626
use crate::vm::VirtualMachine;
@@ -668,13 +668,15 @@ fn builtin_import(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
668668

669669
// builtin_vars
670670

671-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
671+
pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) {
672+
let ctx = &vm.ctx;
673+
672674
#[cfg(target_arch = "wasm32")]
673-
let open = ctx.none();
675+
let open = vm.ctx.none();
674676
#[cfg(not(target_arch = "wasm32"))]
675-
let open = ctx.new_rustfunc(io_open);
677+
let open = vm.ctx.new_rustfunc(io_open);
676678

677-
py_module!(ctx, "__builtins__", {
679+
extend_module!(vm, module, {
678680
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
679681
"__name__" => ctx.new_str(String::from("__main__")),
680682

@@ -765,7 +767,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
765767
"ZeroDivisionError" => ctx.exceptions.zero_division_error.clone(),
766768
"KeyError" => ctx.exceptions.key_error.clone(),
767769
"OSError" => ctx.exceptions.os_error.clone(),
768-
})
770+
});
769771
}
770772

771773
pub fn builtin_build_class_(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {

vm/src/dictdatatype.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use num_traits::ToPrimitive;
99
/// And: http://code.activestate.com/recipes/578375/
1010
use std::collections::HashMap;
1111

12+
#[derive(Default)]
1213
pub struct Dict {
1314
size: usize,
1415
indices: HashMap<usize, usize>,
@@ -88,6 +89,12 @@ impl Dict {
8889
}
8990
}
9091

92+
pub fn clear(&mut self) {
93+
self.entries.clear();
94+
self.indices.clear();
95+
self.size = 0
96+
}
97+
9198
/// Delete a key
9299
pub fn delete(&mut self, vm: &VirtualMachine, key: &PyObjectRef) -> PyResult<()> {
93100
if let LookupResult::Existing(index) = self.lookup(vm, key)? {
@@ -175,12 +182,8 @@ fn calc_hash(vm: &VirtualMachine, key: &PyObjectRef) -> PyResult<usize> {
175182
}
176183

177184
/// Invoke __eq__ on two keys
178-
fn do_eq(
179-
vm: &VirtualMachine,
180-
key1: &PyObjectRef,
181-
key2: &PyObjectRef,
182-
) -> Result<bool, PyObjectRef> {
183-
let result = vm._eq(key1, key2.clone())?;
185+
fn do_eq(vm: &VirtualMachine, key1: &PyObjectRef, key2: &PyObjectRef) -> Result<bool, PyObjectRef> {
186+
let result = vm._eq(key1.clone(), key2.clone())?;
184187
Ok(objbool::get_value(&result))
185188
}
186189

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::vm::VirtualMachine;
1515
fn import_uncached_module(vm: &VirtualMachine, current_path: PathBuf, module: &str) -> PyResult {
1616
// Check for Rust-native modules
1717
if let Some(module) = vm.stdlib_inits.borrow().get(module) {
18-
return Ok(module(&vm.ctx).clone());
18+
return Ok(module(vm).clone());
1919
}
2020

2121
let notfound_error = vm.context().exceptions.module_not_found_error.clone();

vm/src/macros.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,24 @@ macro_rules! no_kwargs {
114114

115115
#[macro_export]
116116
macro_rules! py_module {
117-
( $ctx:expr, $module_name:expr, { $($name:expr => $value:expr),* $(,)* }) => {{
118-
let mut attributes = $crate::pyobject::PyAttributes::new();
117+
( $vm:expr, $module_name:expr, { $($name:expr => $value:expr),* $(,)* }) => {{
118+
let module = $vm.ctx.new_module($module_name, $vm.ctx.new_dict());
119119
$(
120-
let value: PyObjectRef = $value.into();
121-
attributes.insert($name.to_string(), value);
120+
$vm.set_attr(&module, $name, $value).unwrap();
122121
)*
123-
let module_dict = $crate::obj::objdict::PyDictRef::from_attributes($ctx, attributes);
124-
$ctx.new_module($module_name, module_dict)
122+
module
125123
}};
126124
}
127125

126+
#[macro_export]
127+
macro_rules! extend_module {
128+
( $vm:expr, $module:expr, { $($name:expr => $value:expr),* $(,)* }) => {
129+
$(
130+
$vm.set_attr(&$module, $name, $value).unwrap();
131+
)*
132+
}
133+
}
134+
128135
#[macro_export]
129136
macro_rules! py_class {
130137
( $ctx:expr, $class_name:expr, $class_base:expr, { $($name:expr => $value:expr),* $(,)* }) => {

vm/src/obj/objdict.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,6 @@ impl PyDictRef {
215215
}
216216
}
217217

218-
// Used during module initialisation when vm isn't available.
219-
pub fn from_attributes(ctx: &PyContext, attributes: PyAttributes) -> PyDictRef {
220-
let dict = ctx.new_dict();
221-
for (key_str, value) in attributes.into_iter() {
222-
dict.unsafe_str_insert(&key_str, value, ctx);
223-
}
224-
dict
225-
}
226-
227-
// Pub needed for some nasty edge cases.
228-
// It will be unsafe if there are entries in the dictionary that compare equal.
229-
pub fn unsafe_str_insert(&self, key: &str, value: PyObjectRef, ctx: &PyContext) {
230-
self.entries
231-
.borrow_mut()
232-
.insert(key.to_string(), (ctx.new_str(key.to_string()), value));
233-
}
234-
235218
/// Take a python dictionary and convert it to attributes.
236219
pub fn to_attributes(self) -> PyAttributes {
237220
let mut attrs = PyAttributes::new();

vm/src/stdlib/ast.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustpython_parser::{ast, parser};
1212
use crate::function::PyFuncArgs;
1313
use crate::obj::objstr;
1414
use crate::obj::objtype::PyClassRef;
15-
use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol};
15+
use crate::pyobject::{PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol};
1616
use crate::vm::VirtualMachine;
1717

1818
#[derive(Debug)]
@@ -656,8 +656,10 @@ fn ast_parse(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
656656
Ok(ast_node)
657657
}
658658

659-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
660-
py_module!(ctx, "ast", {
659+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
660+
let ctx = &vm.ctx;
661+
662+
py_module!(vm, "ast", {
661663
"parse" => ctx.new_rustfunc(ast_parse),
662664
"Module" => py_class!(ctx, "_ast.Module", ctx.object(), {}),
663665
"FunctionDef" => py_class!(ctx, "_ast.FunctionDef", ctx.object(), {}),

vm/src/stdlib/dis.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::obj::objcode::PyCodeRef;
2-
use crate::pyobject::{PyContext, PyObjectRef, PyResult, TryFromObject};
2+
use crate::pyobject::{PyObjectRef, PyResult, TryFromObject};
33
use crate::vm::VirtualMachine;
44

55
fn dis_dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
@@ -17,8 +17,10 @@ fn dis_disassemble(co: PyObjectRef, vm: &VirtualMachine) -> PyResult {
1717
Ok(vm.get_none())
1818
}
1919

20-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
21-
py_module!(ctx, "dis", {
20+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
21+
let ctx = &vm.ctx;
22+
23+
py_module!(vm, "dis", {
2224
"dis" => ctx.new_rustfunc(dis_dis),
2325
"disassemble" => ctx.new_rustfunc(dis_disassemble)
2426
})

vm/src/stdlib/io.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use crate::obj::objbytes;
2020
use crate::obj::objint;
2121
use crate::obj::objstr;
2222
use crate::obj::objtype::PyClassRef;
23-
use crate::pyobject::{
24-
BufferProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
25-
};
23+
use crate::pyobject::{BufferProtocol, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
2624
use crate::vm::VirtualMachine;
2725

2826
fn compute_c_flag(mode: &str) -> u16 {
@@ -365,7 +363,9 @@ pub fn io_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
365363
}
366364
}
367365

368-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
366+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
367+
let ctx = &vm.ctx;
368+
369369
//IOBase the abstract base class of the IO Module
370370
let io_base = py_class!(ctx, "IOBase", ctx.object(), {
371371
"__enter__" => ctx.new_rustfunc(io_base_cm_enter),
@@ -421,7 +421,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
421421
"getvalue" => ctx.new_rustfunc(bytes_io_getvalue)
422422
});
423423

424-
py_module!(ctx, "io", {
424+
py_module!(vm, "io", {
425425
"open" => ctx.new_rustfunc(io_open),
426426
"IOBase" => io_base,
427427
"RawIOBase" => raw_io_base,

vm/src/stdlib/json.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use crate::obj::{
1212
objtype,
1313
};
1414
use crate::pyobject::{
15-
create_type, DictProtocol, IdProtocol, ItemProtocol, PyContext, PyObjectRef, PyResult,
16-
TypeProtocol,
15+
create_type, DictProtocol, IdProtocol, ItemProtocol, PyObjectRef, PyResult, TypeProtocol,
1716
};
1817
use crate::VirtualMachine;
1918
use num_traits::cast::ToPrimitive;
@@ -232,15 +231,17 @@ fn json_loads(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
232231
de_pyobject(vm, &objstr::get_value(&string))
233232
}
234233

235-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
234+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
235+
let ctx = &vm.ctx;
236+
236237
// TODO: Make this a proper type with a constructor
237238
let json_decode_error = create_type(
238239
"JSONDecodeError",
239240
&ctx.type_type,
240241
&ctx.exceptions.exception_type,
241242
);
242243

243-
py_module!(ctx, "json", {
244+
py_module!(vm, "json", {
244245
"dumps" => ctx.new_rustfunc(json_dumps),
245246
"loads" => ctx.new_rustfunc(json_loads),
246247
"JSONDecodeError" => json_decode_error

vm/src/stdlib/keyword.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustpython_parser::lexer;
66

77
use crate::function::PyFuncArgs;
88
use crate::obj::objstr;
9-
use crate::pyobject::{PyContext, PyObjectRef, PyResult, TypeProtocol};
9+
use crate::pyobject::{PyObjectRef, PyResult, TypeProtocol};
1010
use crate::vm::VirtualMachine;
1111

1212
fn keyword_iskeyword(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -18,15 +18,17 @@ fn keyword_iskeyword(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
1818
Ok(value)
1919
}
2020

21-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
21+
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
22+
let ctx = &vm.ctx;
23+
2224
let keyword_kwlist = ctx.new_list(
2325
lexer::get_keywords()
2426
.keys()
2527
.map(|k| ctx.new_str(k.to_string()))
2628
.collect(),
2729
);
2830

29-
py_module!(ctx, "keyword", {
31+
py_module!(vm, "keyword", {
3032
"iskeyword" => ctx.new_rustfunc(keyword_iskeyword),
3133
"kwlist" => keyword_kwlist
3234
})

0 commit comments

Comments
 (0)