Skip to content

Convert function, method, builtin_*, frame, and generator to Any payload #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::builtins;
use crate::bytecode;
use crate::import::{import, import_module};
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objcode;
use crate::obj::objdict;
use crate::obj::objdict::PyDict;
Expand All @@ -20,8 +21,8 @@ use crate::obj::objlist;
use crate::obj::objstr;
use crate::obj::objtype;
use crate::pyobject::{
DictProtocol, IdProtocol, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult,
TryFromObject, TypeProtocol,
DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2,
PyObjectRef, PyResult, TryFromObject, TypeProtocol,
};
use crate::vm::VirtualMachine;

Expand Down Expand Up @@ -75,6 +76,12 @@ pub struct Frame {
pub lasti: RefCell<usize>, // index of last instruction ran
}

impl PyObjectPayload2 for Frame {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.frame_type()
}
}

// Running a frame can result in one of the below:
pub enum ExecutionResult {
Return(PyObjectRef),
Expand Down Expand Up @@ -592,8 +599,10 @@ impl Frame {
}
bytecode::Instruction::LoadBuildClass => {
let rustfunc = PyObject::new(
PyObjectPayload::RustFunction {
function: Box::new(builtins::builtin_build_class_),
PyObjectPayload::AnyRustValue {
value: Box::new(PyBuiltinFunction::new(Box::new(
builtins::builtin_build_class_,
))),
},
vm.ctx.type_type(),
);
Expand Down
1 change: 1 addition & 0 deletions vm/src/obj/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This package contains the python basic/builtin types

pub mod objbool;
pub mod objbuiltinfunc;
pub mod objbytearray;
pub mod objbytes;
pub mod objcode;
Expand Down
26 changes: 26 additions & 0 deletions vm/src/obj/objbuiltinfunc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fmt;

use crate::pyobject::{PyContext, PyNativeFunc, PyObjectPayload2, PyObjectRef};

pub struct PyBuiltinFunction {
// TODO: shouldn't be public
pub value: PyNativeFunc,
}

impl PyObjectPayload2 for PyBuiltinFunction {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.builtin_function_or_method_type()
}
}

impl fmt::Debug for PyBuiltinFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "builtin function")
}
}

impl PyBuiltinFunction {
pub fn new(value: PyNativeFunc) -> Self {
Self { value }
}
}
10 changes: 2 additions & 8 deletions vm/src/obj/objframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
*/

use crate::frame::Frame;
use crate::pyobject::{
PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
};
use crate::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;

pub fn init(context: &PyContext) {
Expand Down Expand Up @@ -39,9 +37,5 @@ fn frame_fcode(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

pub fn get_value(obj: &PyObjectRef) -> &Frame {
if let PyObjectPayload::Frame { frame } = &obj.payload {
frame
} else {
panic!("Inner error getting int {:?}", obj);
}
&obj.payload::<Frame>().unwrap()
}
53 changes: 49 additions & 4 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
use crate::frame::ScopeRef;
use crate::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyResult, TypeProtocol,
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectPayload2, PyObjectRef, PyResult,
TypeProtocol,
};
use crate::vm::VirtualMachine;

#[derive(Debug)]
pub struct PyFunction {
// TODO: these shouldn't be public
pub code: PyObjectRef,
pub scope: ScopeRef,
pub defaults: PyObjectRef,
}

impl PyFunction {
pub fn new(code: PyObjectRef, scope: ScopeRef, defaults: PyObjectRef) -> Self {
PyFunction {
code,
scope,
defaults,
}
}
}

impl PyObjectPayload2 for PyFunction {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.function_type()
}
}

#[derive(Debug)]
pub struct PyMethod {
// TODO: these shouldn't be public
pub object: PyObjectRef,
pub function: PyObjectRef,
}

impl PyMethod {
pub fn new(object: PyObjectRef, function: PyObjectRef) -> Self {
PyMethod { object, function }
}
}

impl PyObjectPayload2 for PyMethod {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.bound_method_type()
}
}

pub fn init(context: &PyContext) {
let function_type = &context.function_type;
context.set_attr(&function_type, "__get__", context.new_rustfunc(bind_method));
Expand Down Expand Up @@ -79,9 +124,9 @@ fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn function_code(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match args.args[0].payload {
PyObjectPayload::Function { ref code, .. } => Ok(code.clone()),
_ => Err(vm.new_type_error("no code".to_string())),
match args.args[0].payload() {
Some(PyFunction { ref code, .. }) => Ok(code.clone()),
None => Err(vm.new_type_error("no code".to_string())),
}
}

Expand Down
24 changes: 19 additions & 5 deletions vm/src/obj/objgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
* The mythical generator.
*/

use crate::frame::ExecutionResult;
use crate::frame::{ExecutionResult, Frame};
use crate::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectPayload2, PyObjectRef, PyResult,
TypeProtocol,
};
use crate::vm::VirtualMachine;

#[derive(Debug)]
pub struct PyGenerator {
frame: PyObjectRef,
}

impl PyObjectPayload2 for PyGenerator {
fn required_type(ctx: &PyContext) -> PyObjectRef {
ctx.generator_type()
}
}

pub fn init(context: &PyContext) {
let generator_type = &context.generator_type;
context.set_attr(
Expand All @@ -29,7 +41,9 @@ pub fn init(context: &PyContext) {

pub fn new_generator(vm: &mut VirtualMachine, frame: PyObjectRef) -> PyResult {
Ok(PyObject::new(
PyObjectPayload::Generator { frame },
PyObjectPayload::AnyRustValue {
value: Box::new(PyGenerator { frame }),
},
vm.ctx.generator_type.clone(),
))
}
Expand All @@ -55,8 +69,8 @@ fn generator_send(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}

fn send(vm: &mut VirtualMachine, gen: &PyObjectRef, value: &PyObjectRef) -> PyResult {
if let PyObjectPayload::Generator { ref frame } = gen.payload {
if let PyObjectPayload::Frame { ref frame } = frame.payload {
if let Some(PyGenerator { ref frame }) = gen.payload() {
if let Some(frame) = frame.payload::<Frame>() {
frame.push_value(value.clone());
} else {
panic!("Generator frame isn't a frame.");
Expand Down
47 changes: 11 additions & 36 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::bytecode;
use crate::exceptions;
use crate::frame::{Frame, Scope, ScopeRef};
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objbytearray;
use crate::obj::objbytes;
use crate::obj::objcode;
Expand All @@ -24,7 +25,7 @@ use crate::obj::objenumerate;
use crate::obj::objfilter;
use crate::obj::objfloat::{self, PyFloat};
use crate::obj::objframe;
use crate::obj::objfunction;
use crate::obj::objfunction::{self, PyFunction, PyMethod};
use crate::obj::objgenerator;
use crate::obj::objint::{self, PyInt};
use crate::obj::objiter;
Expand Down Expand Up @@ -615,17 +616,17 @@ impl PyContext {
F: IntoPyNativeFunc<T, R>,
{
PyObject::new(
PyObjectPayload::RustFunction {
function: f.into_func(),
PyObjectPayload::AnyRustValue {
value: Box::new(PyBuiltinFunction::new(f.into_func())),
},
self.builtin_function_or_method_type(),
)
}

pub fn new_frame(&self, code: PyObjectRef, scope: ScopeRef) -> PyObjectRef {
PyObject::new(
PyObjectPayload::Frame {
frame: Frame::new(code, scope),
PyObjectPayload::AnyRustValue {
value: Box::new(Frame::new(code, scope)),
},
self.frame_type(),
)
Expand Down Expand Up @@ -657,18 +658,18 @@ impl PyContext {
defaults: PyObjectRef,
) -> PyObjectRef {
PyObject::new(
PyObjectPayload::Function {
code: code_obj,
scope,
defaults,
PyObjectPayload::AnyRustValue {
value: Box::new(PyFunction::new(code_obj, scope, defaults)),
},
self.function_type(),
)
}

pub fn new_bound_method(&self, function: PyObjectRef, object: PyObjectRef) -> PyObjectRef {
PyObject::new(
PyObjectPayload::BoundMethod { function, object },
PyObjectPayload::AnyRustValue {
value: Box::new(PyMethod::new(object, function)),
},
self.bound_method_type(),
)
}
Expand Down Expand Up @@ -1507,27 +1508,9 @@ pub enum PyObjectPayload {
MemoryView {
obj: PyObjectRef,
},
Frame {
frame: Frame,
},
Function {
code: PyObjectRef,
scope: ScopeRef,
defaults: PyObjectRef,
},
Generator {
frame: PyObjectRef,
},
BoundMethod {
function: PyObjectRef,
object: PyObjectRef,
},
WeakRef {
referent: PyObjectWeakRef,
},
RustFunction {
function: PyNativeFunc,
},
AnyRustValue {
value: Box<dyn std::any::Any>,
},
Expand All @@ -1548,14 +1531,6 @@ impl fmt::Debug for PyObjectPayload {
PyObjectPayload::WeakRef { .. } => write!(f, "weakref"),
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
PyObjectPayload::Slice { .. } => write!(f, "slice"),
PyObjectPayload::Function { .. } => write!(f, "function"),
PyObjectPayload::Generator { .. } => write!(f, "generator"),
PyObjectPayload::BoundMethod {
ref function,
ref object,
} => write!(f, "bound-method: {:?} of {:?}", function, object),
PyObjectPayload::RustFunction { .. } => write!(f, "rust function"),
PyObjectPayload::Frame { .. } => write!(f, "frame"),
PyObjectPayload::AnyRustValue { value } => value.fmt(f),
}
}
Expand Down
Loading