Skip to content

Commit f7aa1dd

Browse files
committed
Remove executor trait since we can have mutual includes
1 parent 4b2f86d commit f7aa1dd

File tree

13 files changed

+78
-61
lines changed

13 files changed

+78
-61
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern crate rustpython_vm;
99

1010
use clap::{App, Arg};
1111
use rustpython_parser::parser;
12-
use rustpython_vm::{VirtualMachine, Executor};
12+
use rustpython_vm::VirtualMachine;
1313
use rustpython_vm::compile;
1414
use rustpython_vm::eval::eval;
1515
use std::io;

vm/src/builtins.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use std::io::{self, Write};
44

55
use super::compile;
66
use super::pyobject::DictProtocol;
7-
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult, Scope, IdProtocol};
7+
use super::pyobject::{PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult, Scope, IdProtocol};
8+
use super::vm::VirtualMachine;
89
use super::objbool;
910

1011

11-
fn get_locals(rt: &mut Executor) -> PyObjectRef {
12+
fn get_locals(rt: &mut VirtualMachine) -> PyObjectRef {
1213
let mut d = rt.new_dict();
1314
// TODO: implement dict_iter_items?
1415
let locals = rt.get_locals();
@@ -23,16 +24,16 @@ fn get_locals(rt: &mut Executor) -> PyObjectRef {
2324
d
2425
}
2526

26-
fn dir_locals(rt: &mut Executor) -> PyObjectRef {
27+
fn dir_locals(rt: &mut VirtualMachine) -> PyObjectRef {
2728
get_locals(rt)
2829
}
2930

30-
fn dir_object(rt: &mut Executor, obj: PyObjectRef) -> PyObjectRef {
31+
fn dir_object(rt: &mut VirtualMachine, obj: PyObjectRef) -> PyObjectRef {
3132
let d = rt.new_dict();
3233
d
3334
}
3435

35-
pub fn builtin_dir(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
36+
pub fn builtin_dir(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
3637
if args.is_empty() {
3738
Ok(dir_locals(rt))
3839
} else {
@@ -41,15 +42,15 @@ pub fn builtin_dir(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
4142
}
4243
}
4344

44-
pub fn builtin_id(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
45+
pub fn builtin_id(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
4546
if args.len() != 1 {
4647
return Err(rt.new_exception("Expected only one argument".to_string()))
4748
}
4849

4950
Ok(rt.context().new_int(args[0].get_id() as i32))
5051
}
5152

52-
pub fn builtin_print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
53+
pub fn builtin_print(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
5354
trace!("print called with {:?}", args);
5455
for a in args {
5556
print!("{} ", a.borrow().str());
@@ -59,7 +60,7 @@ pub fn builtin_print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
5960
Ok(rt.get_none())
6061
}
6162

62-
pub fn builtin_compile(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
63+
pub fn builtin_compile(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
6364
if args.len() < 1 {
6465
return Err(rt.new_exception("Expected more arguments".to_string()))
6566
}
@@ -73,11 +74,36 @@ pub fn builtin_compile(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
7374
}
7475
}
7576

76-
pub fn locals(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
77+
pub fn builtin_eval(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
78+
if args.len() > 3 {
79+
return Err(rt.new_exception("Expected at maximum of 3 arguments".to_string()))
80+
} else if args.len() > 2 {
81+
// TODO: handle optional global and locals
82+
} else {
83+
return Err(rt.new_exception("Expected at least one argument".to_string()))
84+
}
85+
let source = args[0].clone();
86+
let _globals = args[1].clone();
87+
let locals = args[2].clone();
88+
89+
let code_obj = source; // if source.borrow().kind
90+
91+
// Construct new scope:
92+
let scope_inner = Scope {
93+
locals: locals,
94+
parent: None,
95+
};
96+
let scope = PyObject { kind: PyObjectKind::Scope { scope: scope_inner }, typ: None }.into_ref();
97+
98+
// Run the source:
99+
rt.run_code_obj(code_obj, scope)
100+
}
101+
102+
pub fn locals(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
77103
Ok(rt.get_locals())
78104
}
79105

80-
pub fn len(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
106+
pub fn len(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
81107
if args.len() != 1 {
82108
panic!("len(s) expects exactly one parameter");
83109
}
@@ -105,6 +131,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
105131
dict.insert(String::from("dir"), ctx.new_rustfunc(builtin_dir));
106132
dict.insert(String::from("locals"), ctx.new_rustfunc(locals));
107133
dict.insert(String::from("compile"), ctx.new_rustfunc(builtin_compile));
134+
dict.insert(String::from("eval"), ctx.new_rustfunc(builtin_eval));
108135
dict.insert("len".to_string(), ctx.new_rustfunc(len));
109136
let d2 = PyObject::new(PyObjectKind::Dict { elements: dict }, ctx.type_type.clone());
110137
let scope = PyObject::new(PyObjectKind::Scope { scope: Scope { locals: d2, parent: None} }, ctx.type_type.clone());
@@ -118,10 +145,10 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
118145
obj
119146
}
120147

121-
fn builtin_any(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
148+
fn builtin_any(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
122149
Ok(rt.new_bool(args.into_iter().any(|e| objbool::boolval(e))))
123150
}
124151

125-
fn builtin_all(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
152+
fn builtin_all(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> PyResult {
126153
Ok(rt.new_bool(args.into_iter().all(|e| objbool::boolval(e))))
127154
}

vm/src/compile.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ extern crate rustpython_parser;
66

77
use self::rustpython_parser::{ast, parser};
88
use super::bytecode::{self, CodeObject, Instruction};
9-
use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef};
9+
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef};
10+
use super::vm::VirtualMachine;
1011

1112
struct Compiler {
1213
code_object_stack: Vec<CodeObject>,
1314
nxt_label: usize,
1415
}
1516

16-
pub fn compile(rt: &mut Executor, source: &String, mode: Mode) -> Result<PyObjectRef, String> {
17+
pub fn compile(rt: &mut VirtualMachine, source: &String, mode: Mode) -> Result<PyObjectRef, String> {
1718
let mut compiler = Compiler::new();
1819
compiler.push_code_object(CodeObject::new(Vec::new()));
1920
match mode {

vm/src/eval.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate rustpython_parser;
22

33
use super::compile;
4-
use super::pyobject::{Executor, PyObjectRef, PyResult};
4+
use super::pyobject::{PyObjectRef, PyResult};
55
use super::vm::VirtualMachine;
66

77
pub fn eval(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> PyResult {
@@ -18,7 +18,6 @@ pub fn eval(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> PyR
1818

1919
#[cfg(test)]
2020
mod tests {
21-
use super::Executor;
2221
use super::VirtualMachine;
2322
use super::eval;
2423

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::io::ErrorKind::NotFound;
1010

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

1616
pub fn import(vm: &mut VirtualMachine, name: &String) -> PyResult {

vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ pub mod pyobject;
2222
mod sysmodule;
2323
mod vm;
2424

25-
pub use self::pyobject::Executor;
25+
// pub use self::pyobject::Executor;
2626
pub use self::vm::VirtualMachine;

vm/src/objdict.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use super::pyobject::{Executor, PyObjectRef, PyResult};
1+
use super::pyobject::{PyObjectRef, PyResult};
2+
use super::vm::VirtualMachine;
23

3-
pub fn set_item(rt: &mut Executor, d: PyObjectRef, idx: PyObjectRef, obj: PyObjectRef) -> PyResult {
4+
pub fn set_item(rt: &mut VirtualMachine, d: PyObjectRef, idx: PyObjectRef, obj: PyObjectRef) -> PyResult {
45
Ok(rt.get_none())
56
}
67

vm/src/objint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef};
1+
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef};
2+
use super::vm::VirtualMachine;
23
use std::collections::HashMap;
34

4-
fn str(rt: &mut Executor, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
5+
fn str(rt: &mut VirtualMachine, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
56
Ok(rt.new_str("todo".to_string()))
67
}
78

vm/src/objlist.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef, PyResult};
1+
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult};
2+
use super::vm::VirtualMachine;
23

34
fn get_pos(l: &Vec<PyObjectRef>, p: i32) -> usize {
45
if p < 0 {
@@ -8,7 +9,7 @@ fn get_pos(l: &Vec<PyObjectRef>, p: i32) -> usize {
89
}
910
}
1011

11-
pub fn get_item(rt: &mut Executor, l: &Vec<PyObjectRef>, b: PyObjectRef) -> PyResult {
12+
pub fn get_item(rt: &mut VirtualMachine, l: &Vec<PyObjectRef>, b: PyObjectRef) -> PyResult {
1213
match &(b.borrow()).kind {
1314
PyObjectKind::Integer { value } => {
1415
let pos_index = get_pos(l, *value);
@@ -51,7 +52,7 @@ pub fn get_item(rt: &mut Executor, l: &Vec<PyObjectRef>, b: PyObjectRef) -> PyRe
5152

5253
// set_item:
5354
pub fn set_item(
54-
rt: &mut Executor,
55+
rt: &mut VirtualMachine,
5556
l: &mut Vec<PyObjectRef>,
5657
idx: PyObjectRef,
5758
obj: PyObjectRef,
@@ -69,7 +70,7 @@ pub fn set_item(
6970
}
7071
}
7172

72-
pub fn append(rt: &mut Executor, l: PyObjectRef, other: PyObjectRef) -> PyResult {
73+
pub fn append(rt: &mut VirtualMachine, l: PyObjectRef, other: PyObjectRef) -> PyResult {
7374
Ok(rt.get_none())
7475
}
7576

vm/src/objstr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::pyobject::{Executor, PyObjectKind, PyObjectRef, PyResult};
1+
use super::pyobject::{PyObjectKind, PyObjectRef, PyResult};
2+
use super::vm::VirtualMachine;
23

34
fn str_pos(s: &String, p: i32) -> usize {
45
if p < 0 {
@@ -10,7 +11,7 @@ fn str_pos(s: &String, p: i32) -> usize {
1011
}
1112
}
1213

13-
pub fn subscript(rt: &mut Executor, value: &String, b: PyObjectRef) -> PyResult {
14+
pub fn subscript(rt: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyResult {
1415
// let value = a
1516
match &(*b.borrow()).kind {
1617
&PyObjectKind::Integer { value: ref pos } => {

0 commit comments

Comments
 (0)