From 43011d3a9c7e313303e464ef55a19029ee56e1fc Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Sun, 12 Aug 2018 09:45:04 +0100 Subject: [PATCH] Create functions with type of function class. --- vm/src/pyobject.rs | 28 ++++++++++++++++++---------- vm/src/vm.rs | 8 +------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 892f10a39..ee0c147bb 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -1,4 +1,5 @@ use super::bytecode; +use super::objfunction; use super::objint; use super::objtype; use super::vm::VirtualMachine; @@ -45,6 +46,7 @@ pub struct PyContext { pub list_type: PyObjectRef, pub tuple_type: PyObjectRef, pub dict_type: PyObjectRef, + pub function_type: PyObjectRef, } /* @@ -61,18 +63,14 @@ pub struct Scope { impl PyContext { pub fn new() -> PyContext { let type_type = objtype::create_type(); - let int_type = objint::create_type(type_type.clone()); - // TODO: How to represent builtin types? - let list_type = type_type.clone(); - let tuple_type = type_type.clone(); - let dict_type = type_type.clone(); - // let str_type = objstr::make_type(); + PyContext { + int_type: objint::create_type(type_type.clone()), + list_type: type_type.clone(), + tuple_type: type_type.clone(), + dict_type: type_type.clone(), + function_type: objfunction::create_type(type_type.clone()), type_type: type_type, - int_type: int_type, - list_type: list_type, - tuple_type: tuple_type, - dict_type: dict_type, } } @@ -154,6 +152,16 @@ impl PyContext { ) } + pub fn new_function(&self, code_obj: PyObjectRef, scope: PyObjectRef) -> PyObjectRef { + PyObject::new( + PyObjectKind::Function { + code: code_obj, + scope: scope, + }, + self.function_type.clone(), + ) + } + /* TODO: something like this? pub fn new_instance(&self, name: String) -> PyObjectRef { PyObject::new(PyObjectKind::Class { name: name }, self.type_type.clone()) diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 8891c9b87..82eb68af7 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -692,13 +692,7 @@ impl VirtualMachine { // pop argc arguments // argument: name, args, globals let scope = self.current_frame().locals.clone(); - let obj = PyObject::new( - PyObjectKind::Function { - code: code_obj, - scope: scope, - }, - self.get_type(), - ); + let obj = self.ctx.new_function(code_obj, scope); self.push_value(obj); None }