Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::path::PathBuf;
use self::rustpython_parser::parser;
use super::compile;
use super::pyobject::{DictProtocol, PyObject, PyObjectKind, PyResult};
use super::stdlib;
use super::vm::VirtualMachine;

fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
Expand All @@ -22,8 +21,8 @@ fn import_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
}

// Check for Rust-native modules
if let Some(module) = stdlib::get_modules(vm).get(module) {
return Ok(module.clone());
if let Some(module) = vm.stdlib_inits.get(module) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is a good idea to implement some sort of function which get_or_create's a module based on a name. This function then will check a cache HashMap<String, PyObjectRef> where the object ref is a module ref, and if not present, calls an init function on the module, so it is loaded lazily. On the other hand, we might preload all builtin modules, so that the initialization order is deterministic at all times.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that cache just sys.modules?

(We don't currently put anything in there, see #109.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! I totally forgot about that, you are right!

return Ok(module(&vm.ctx).clone());
}

// TODO: introduce import error:
Expand Down
9 changes: 5 additions & 4 deletions vm/src/stdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod json;
use std::collections::HashMap;

use super::pyobject::PyObjectRef;
use super::vm::VirtualMachine;
use super::pyobject::{PyContext, PyObjectRef};

pub fn get_modules(vm: &VirtualMachine) -> HashMap<String, PyObjectRef> {
pub type StdlibInitFunc = fn(&PyContext) -> PyObjectRef;

pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect here HashMap<String, PyObjectRef> where the object refers to a python module instance.

let mut modules = HashMap::new();
modules.insert("json".to_string(), json::mk_module(vm.context()));
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
modules
}
4 changes: 4 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::pyobject::{
AttributeProtocol, DictProtocol, IdProtocol, ParentProtocol, PyContext, PyFuncArgs, PyObject,
PyObjectKind, PyObjectRef, PyResult,
};
use super::stdlib;
use super::sysmodule;

// use objects::objects;
Expand All @@ -34,6 +35,7 @@ pub struct VirtualMachine {
frames: Vec<Frame>,
builtins: PyObjectRef,
pub sys_module: PyObjectRef,
pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can make the rust parts of the stdlib part of context, so when we have more than 1 vm, the stdlib is shared. Does this make sense?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason why this couldn't live on PyContext instead of VirtualMachine.

pub ctx: PyContext,
}

Expand Down Expand Up @@ -111,10 +113,12 @@ impl VirtualMachine {
let ctx = PyContext::new();
let builtins = builtins::make_module(&ctx);
let sysmod = sysmodule::mk_module(&ctx);
let stdlib_inits = stdlib::get_module_inits();
VirtualMachine {
frames: vec![],
builtins: builtins,
sys_module: sysmod,
stdlib_inits,
ctx: ctx,
}
}
Expand Down