Repository navigation
Store Rust stdlib modules on VirtualMachine and lazily-load them #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect here |
||
| 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 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -34,6 +35,7 @@ pub struct VirtualMachine { | |
| frames: Vec<Frame>, | ||
| builtins: PyObjectRef, | ||
| pub sys_module: PyObjectRef, | ||
| pub stdlib_inits: HashMap<String, stdlib::StdlibInitFunc>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| } | ||
|
|
||
|
|
@@ -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, | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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 cacheHashMap<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.There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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!