Skip to content

Commit 0f4f130

Browse files
committed
Add early sys module
1 parent 908f85b commit 0f4f130

7 files changed

Lines changed: 48 additions & 26 deletions

File tree

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn _run_string(source: &String) {
5555
let code_obj = compile::compile(&mut vm, &source, compile::Mode::Exec).unwrap();
5656
debug!("Code object: {:?}", code_obj.borrow());
5757
let builtins = vm.get_builtin_scope();
58-
let vars = vm.new_scope(Some(builtins)); // Keep track of local variables
58+
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
5959
match vm.run_code_obj(code_obj, vars) {
6060
Ok(_value) => {}
6161
Err(exc) => {
@@ -92,7 +92,7 @@ fn run_shell() {
9292
);
9393
let mut vm = VirtualMachine::new();
9494
let builtins = vm.get_builtin_scope();
95-
let vars = vm.new_scope(Some(builtins)); // Keep track of local variables
95+
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
9696
// Read a single line:
9797
loop {
9898
let mut input = String::new();

vm/src/frame.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::collections::HashMap;
21
use std::fmt;
32

43
use super::bytecode;

vm/src/import.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ use std::io::ErrorKind::NotFound;
1010

1111
use self::rustpython_parser::parser;
1212
use super::compile;
13-
use super::pyobject::{PyObject, PyObjectKind, PyResult};
13+
use super::pyobject::{PyObject, PyObjectKind, PyResult, DictProtocol};
1414
use super::vm::VirtualMachine;
1515

1616
pub fn import(vm: &mut VirtualMachine, name: &String) -> PyResult {
17-
// Time to search for module in any place:
18-
// TODO: handle 'import sys' as special case?
17+
// First, see if we already loaded the module:
18+
let sys_modules = vm.sys_module.get_item(&"modules".to_string());
19+
if sys_modules.contains_key(name) {
20+
return Ok(sys_modules.get_item(name))
21+
}
1922

23+
// Time to search for module in any place:
2024
let filepath = find_source(name).map_err(|e| vm.new_exception(format!("Error: {:?}", e)))?;
2125
let source = parser::read_file(filepath.as_path())
2226
.map_err(|e| vm.new_exception(format!("Error: {:?}", e)))?;
@@ -32,7 +36,7 @@ pub fn import(vm: &mut VirtualMachine, name: &String) -> PyResult {
3236
};
3337

3438
let builtins = vm.get_builtin_scope();
35-
let scope = vm.new_scope(Some(builtins));
39+
let scope = vm.context().new_scope(Some(builtins));
3640

3741
match vm.run_code_obj(code_obj, scope.clone()) {
3842
Ok(value) => {}

vm/src/objbool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
use super::pyobject::{PyObjectKind, PyObjectRef, PyResult};
2+
use super::pyobject::{PyObjectKind, PyObjectRef};
33

44
pub fn boolval(o: PyObjectRef) -> bool {
55
let obj = o.borrow();

vm/src/pyobject.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ impl PyContext {
105105
)
106106
}
107107

108+
pub fn new_scope(&self, parent: Option<PyObjectRef>) -> PyObjectRef {
109+
let locals = self.new_dict();
110+
let scope = Scope {
111+
locals: locals,
112+
parent: parent,
113+
};
114+
PyObject { kind: PyObjectKind::Scope { scope: scope }, typ: None }.into_ref()
115+
}
116+
117+
pub fn new_module(&self, name: &String, scope: PyObjectRef) -> PyObjectRef {
118+
PyObject::new(
119+
PyObjectKind::Module {
120+
name: name.clone(),
121+
dict: scope.clone(),
122+
},
123+
self.type_type.clone(),
124+
)
125+
}
126+
108127
pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef {
109128
PyObject::new(
110129
PyObjectKind::RustFunction { function: function },
@@ -208,6 +227,7 @@ impl DictProtocol for PyObjectRef {
208227
} => {
209228
el.insert(k.to_string(), v);
210229
},
230+
PyObjectKind::Module { ref name, ref mut dict } => dict.set_item(k, v),
211231
PyObjectKind::Scope { ref mut scope } => {
212232
scope.locals.set_item(k, v);
213233
},

vm/src/sysmodule.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
* The magic sys module.
55
*/
66

7-
/*
8-
* TODO:
9-
fn mk_module(rt: &mut Executor) -> PyObjectRef {
10-
let path = rt.new_list();
11-
let obj = rt.new_module("sys");
12-
obj
7+
use super::pyobject::{DictProtocol, PyContext, PyObjectRef};
8+
9+
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
10+
let path = ctx.new_list();
11+
let mut modules = ctx.new_dict();
12+
let sys_name = "sys".to_string();
13+
let mut sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
14+
modules.set_item(&sys_name, sys_mod.clone());
15+
sys_mod.set_item(&"modules".to_string(), modules);
16+
sys_mod.set_item(&"path".to_string(), path);
17+
sys_mod
1318
}
14-
*/

vm/src/vm.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
use std::cell::RefMut;
8-
use std::collections::HashMap;
98
use std::ops::Deref;
109

1110
use super::builtins;
@@ -18,13 +17,16 @@ use super::objtuple;
1817
use super::pyobject::{DictProtocol, PyContext, PyObject, PyObjectKind, PyObjectRef,
1918
PyResult, ParentProtocol, Scope, IdProtocol, PyFuncArgs};
2019

20+
use super::sysmodule;
21+
2122
// use objects::objects;
2223

2324
// Objects are live when they are on stack, or referenced by a name (for now)
2425

2526
pub struct VirtualMachine {
2627
frames: Vec<Frame>,
2728
builtins: PyObjectRef,
29+
pub sys_module: PyObjectRef,
2830
ctx: PyContext,
2931
}
3032

@@ -53,15 +55,6 @@ impl VirtualMachine {
5355
self.ctx.new_dict()
5456
}
5557

56-
pub fn new_scope(&self, parent: Option<PyObjectRef>) -> PyObjectRef {
57-
let locals = self.ctx.new_dict();
58-
let scope = Scope {
59-
locals: locals,
60-
parent: parent,
61-
};
62-
PyObject { kind: PyObjectKind::Scope { scope: scope }, typ: None }.into_ref()
63-
}
64-
6558
pub fn new_exception(&self, msg: String) -> PyObjectRef {
6659
self.new_str(msg)
6760
}
@@ -93,9 +86,11 @@ impl VirtualMachine {
9386
pub fn new() -> VirtualMachine {
9487
let ctx = PyContext::new();
9588
let builtins = builtins::make_module(&ctx);
89+
let sysmod = sysmodule::mk_module(&ctx);
9690
VirtualMachine {
9791
frames: vec![],
9892
builtins: builtins,
93+
sys_module: sysmod,
9994
ctx: ctx,
10095
}
10196
}
@@ -467,7 +462,7 @@ impl VirtualMachine {
467462
match f.kind {
468463
PyObjectKind::RustFunction { ref function } => f.call(self, args),
469464
PyObjectKind::Function { ref code, ref scope } => {
470-
let mut scope = self.new_scope(Some(scope.clone()));
465+
let mut scope = self.ctx.new_scope(Some(scope.clone()));
471466
let code_object = copy_code(code.clone());
472467
for (name, value) in code_object.arg_names.iter().zip(args.args) {
473468
scope.set_item(name, value);

0 commit comments

Comments
 (0)