mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Create functions with type of function class.
This commit is contained in:
1 parent
cb3bd23cb1
commit
43011d3a9c
2 files changed
+19
-17
No files matched your search
+18
-10
@@ -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())
|
||||
|
||||
+1
-7
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user