Skip to content

Commit 66ff3f0

Browse files
committed
Add type field to PyObject
1 parent cafc9ed commit 66ff3f0

File tree

5 files changed

+149
-79
lines changed

5 files changed

+149
-79
lines changed

vm/src/builtins.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// use std::ops::Deref;
22
use std::io::{self, Write};
33

4-
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult};
4+
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult, PyContext, Executor};
55

66
/*
77
* Original impl:
@@ -37,15 +37,15 @@ pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
3737
}
3838
*/
3939

40-
pub fn print(args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
40+
pub fn print(rt: &mut Executor, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
4141
// println!("Woot: {:?}", args);
4242
trace!("print called with {:?}", args);
4343
for a in args {
4444
print!("{} ", a.borrow_mut().str());
4545
}
4646
println!();
4747
io::stdout().flush().unwrap();
48-
Ok(PyObject::new(PyObjectKind::None))
48+
Ok(rt.get_none())
4949
}
5050

5151
/*
@@ -64,28 +64,34 @@ pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
6464
}
6565
*/
6666

67-
pub fn make_module() -> PyObjectRef {
67+
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
6868
// scope[String::from("print")] = print;
69-
let obj = PyObject::new(PyObjectKind::Module);
69+
let obj = PyObject::new(PyObjectKind::Module, ctx.type_type.clone());
7070
obj.borrow_mut().dict.insert(
7171
String::from("print"),
72-
PyObject::new(PyObjectKind::RustFunction { function: print }),
72+
PyObject::new(PyObjectKind::RustFunction { function: print }, ctx.type_type.clone()),
73+
);
74+
obj.borrow_mut().dict.insert(
75+
String::from("type"),
76+
ctx.type_type.clone(),
7377
);
7478
obj.borrow_mut().dict.insert(
7579
String::from("all"),
76-
PyObject::new(PyObjectKind::RustFunction { function: all }),
80+
PyObject::new(PyObjectKind::RustFunction { function: all }, ctx.type_type.clone()),
7781
);
7882
obj.borrow_mut().dict.insert(
7983
String::from("any"),
80-
PyObject::new(PyObjectKind::RustFunction { function: any }),
84+
PyObject::new(PyObjectKind::RustFunction { function: any }, ctx.type_type.clone()),
8185
);
8286
obj
8387
}
8488

85-
fn any(args: Vec<PyObjectRef>) -> PyResult {
86-
Ok(PyObject::new_bool(true))
89+
fn any(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
90+
// TODO
91+
Ok(rt.new_bool(true))
8792
}
8893

89-
fn all(args: Vec<PyObjectRef>) -> PyResult {
90-
Ok(PyObject::new_bool(true))
94+
fn all(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
95+
// TODO
96+
Ok(rt.new_bool(true))
9197
}

vm/src/objint.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef};
1+
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, Executor};
22

3-
fn str(args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
4-
Ok(PyObject::new(PyObjectKind::String {
5-
value: "todo".to_string(),
6-
}))
3+
fn str(rt: &mut Executor, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
4+
Ok(rt.new_str("todo".to_string()))
75
}
86

97
fn add() {}
@@ -14,11 +12,11 @@ fn set_attr(a: &mut PyObjectRef, name: String, b: PyObjectRef) {
1412
}
1513
*/
1614

17-
pub fn create_type() -> PyObjectRef {
18-
let mut typ = PyObject::new(PyObjectKind::Type);
15+
pub fn create_type(type_type: PyObjectRef) -> PyObjectRef {
16+
let typ = PyObject::new(PyObjectKind::Type, type_type.clone());
1917
typ.borrow_mut().dict.insert(
2018
"__str__".to_string(),
21-
PyObject::new(PyObjectKind::RustFunction { function: str }),
19+
PyObject::new(PyObjectKind::RustFunction { function: str }, type_type.clone()),
2220
);
2321
typ
2422
}

vm/src/objtype.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
// use std::rc::Rc;
2+
// use std::cell::RefCell;
3+
14
/*
25
* The magical type type
36
*/
47

58
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef};
69

710
pub fn create_type() -> PyObjectRef {
8-
let mut typ = PyObject::new(PyObjectKind::Type);
11+
let typ = PyObject::default().into_ref();
12+
(*typ.borrow_mut()).kind = PyObjectKind::Type;
13+
(*typ.borrow_mut()).typ = Some(typ.clone());
914
// typ.borrow_mut().dict.insert("__str__".to_string(), PyObject::new(PyObjectKind::RustFunction { function: str }));
1015
typ
1116
}
17+

vm/src/pyobject.rs

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::rc::Rc;
2-
// use std::fmt;
2+
use std::fmt;
33
use super::bytecode;
44
use super::objint;
5+
use super::objtype;
56
use std::cell::RefCell;
67
use std::collections::HashMap;
78
use std::ops::{Add, Mul, Sub};
8-
// use super::objtype;
99

1010
/* Python objects and references.
1111
@@ -36,35 +36,66 @@ impl fmt::Display for PyObjectRef {
3636
}
3737
}*/
3838

39-
pub struct Context {
40-
int_type: PyObjectRef,
39+
#[derive(Debug)]
40+
pub struct PyContext {
41+
pub type_type: PyObjectRef,
42+
pub int_type: PyObjectRef,
4143
}
4244

4345
// Basic objects:
44-
impl Context {
45-
fn new() -> Context {
46-
let type_type = objint::create_type();
47-
let int_type = objint::create_type();
46+
impl PyContext {
47+
pub fn new() -> PyContext {
48+
let type_type = objtype::create_type();
49+
let int_type = objint::create_type(type_type.clone());
4850
// let str_type = objstr::make_type();
49-
Context {
50-
// type_type: type_type,
51+
PyContext {
52+
type_type: type_type,
5153
int_type: int_type,
5254
}
5355
}
56+
57+
pub fn new_int(&self, i: i32) -> PyObjectRef {
58+
PyObject::new(PyObjectKind::Integer { value: i }, self.type_type.clone())
59+
}
60+
61+
pub fn new_str(&self, s: String) -> PyObjectRef {
62+
PyObject::new(PyObjectKind::String { value: s }, self.type_type.clone())
63+
}
64+
65+
pub fn new_bool(&self, b: bool) -> PyObjectRef {
66+
PyObject::new(PyObjectKind::Boolean { value: b }, self.type_type.clone())
67+
}
5468
}
5569

5670
pub trait Executor {
57-
fn call(&self, PyObjectRef) -> PyResult;
71+
fn call(&mut self, PyObjectRef) -> PyResult;
72+
fn new_str(&self, s: String) -> PyObjectRef;
73+
fn new_bool(&self, b: bool) -> PyObjectRef;
74+
fn get_none(&self) -> PyObjectRef;
75+
fn get_type(&self) -> PyObjectRef;
76+
fn context(&self) -> &PyContext;
5877
}
5978

6079
#[derive(Debug)]
6180
pub struct PyObject {
6281
pub kind: PyObjectKind,
63-
// typ: PyObjectRef,
82+
pub typ: Option<PyObjectRef>,
6483
pub dict: HashMap<String, PyObjectRef>, // __dict__ member
6584
}
6685

67-
#[derive(Debug)]
86+
impl Default for PyObject {
87+
fn default() -> PyObject {
88+
PyObject {
89+
kind: PyObjectKind::None,
90+
typ: None,
91+
dict: HashMap::new(),
92+
}
93+
}
94+
}
95+
96+
type RustPyFunc = fn(rt: &mut Executor, Vec<PyObjectRef>) -> PyResult;
97+
98+
// #[derive(Debug)]
6899
pub enum PyObjectKind {
69100
String {
70101
value: String,
@@ -105,22 +136,28 @@ pub enum PyObjectKind {
105136
None,
106137
Type,
107138
RustFunction {
108-
function: fn(Vec<PyObjectRef>) -> PyResult,
139+
function: RustPyFunc,
109140
},
110141
}
111142

143+
impl fmt::Debug for PyObjectKind {
144+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145+
write!(f, "Some kind of python obj")
146+
}
147+
}
148+
112149
impl PyObject {
113-
pub fn new(kind: PyObjectKind) -> PyObjectRef {
150+
pub fn new(kind: PyObjectKind, typ: PyObjectRef) -> PyObjectRef {
114151
PyObject {
115152
kind: kind,
116-
// typ: PyO
153+
typ: Some(typ),
117154
dict: HashMap::new(),
118155
}.into_ref()
119156
}
120157

121-
pub fn call(&self, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
158+
pub fn call(&self, rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
122159
match self.kind {
123-
PyObjectKind::RustFunction { ref function } => function(args),
160+
PyObjectKind::RustFunction { ref function } => function(rt, args),
124161
_ => {
125162
println!("Not impl {:?}", self);
126163
panic!("Not impl");
@@ -190,17 +227,6 @@ impl PyObject {
190227
Rc::new(RefCell::new(self))
191228
}
192229

193-
pub fn new_int(i: i32) -> PyObjectRef {
194-
PyObject::new(PyObjectKind::Integer { value: i })
195-
}
196-
197-
pub fn new_str(s: String) -> PyObjectRef {
198-
PyObject::new(PyObjectKind::String { value: s })
199-
}
200-
201-
pub fn new_bool(b: bool) -> PyObjectRef {
202-
PyObject::new(PyObjectKind::Boolean { value: b })
203-
}
204230
}
205231

206232
impl<'a> Add<&'a PyObject> for &'a PyObject {
@@ -319,12 +345,13 @@ impl PartialEq for PyObject {
319345

320346
#[cfg(test)]
321347
mod tests {
322-
use super::{PyObject, PyObjectKind, PyObjectRef};
348+
use super::{PyObjectKind, PyContext};
323349

324350
#[test]
325351
fn test_add_py_integers() {
326-
let a = PyObject::new_int(33);
327-
let b = PyObject::new_int(12);
352+
let ctx = PyContext::new();
353+
let a = ctx.new_int(33);
354+
let b = ctx.new_int(12);
328355
let c = &*a.borrow() + &*b.borrow();
329356
match c {
330357
PyObjectKind::Integer { value } => assert_eq!(value, 45),
@@ -334,8 +361,9 @@ mod tests {
334361

335362
#[test]
336363
fn test_multiply_str() {
337-
let a = PyObject::new_str(String::from("Hello "));
338-
let b = PyObject::new_int(4);
364+
let ctx = PyContext::new();
365+
let a = ctx.new_str(String::from("Hello "));
366+
let b = ctx.new_int(4);
339367
let c = &*a.borrow() * &*b.borrow();
340368
match c {
341369
PyObjectKind::String { value } => {
@@ -345,4 +373,8 @@ mod tests {
345373
}
346374
}
347375

376+
#[test]
377+
fn test_type_type() {
378+
let ctx = PyContext::new();
379+
}
348380
}

0 commit comments

Comments
 (0)