Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
method
  • Loading branch information
OddCoincidence committed Mar 9, 2019
commit 5a74121c76a02e146bf5d6daf28f1f6dbe7752f3
19 changes: 19 additions & 0 deletions vm/src/obj/objfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ impl PyObjectPayload2 for PyFunction {
}
}

#[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
14 changes: 4 additions & 10 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::obj::objenumerate;
use crate::obj::objfilter;
use crate::obj::objfloat::{self, PyFloat};
use crate::obj::objframe;
use crate::obj::objfunction::{self, PyFunction};
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 @@ -666,7 +666,9 @@ impl PyContext {

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 @@ -1511,10 +1513,6 @@ pub enum PyObjectPayload {
Generator {
frame: PyObjectRef,
},
BoundMethod {
function: PyObjectRef,
object: PyObjectRef,
},
WeakRef {
referent: PyObjectWeakRef,
},
Expand Down Expand Up @@ -1542,10 +1540,6 @@ impl fmt::Debug for PyObjectPayload {
PyObjectPayload::Iterator { .. } => write!(f, "iterator"),
PyObjectPayload::Slice { .. } => write!(f, "slice"),
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
13 changes: 8 additions & 5 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::frame::{Scope, ScopeRef};
use crate::obj::objbool;
use crate::obj::objcode;
use crate::obj::objframe;
use crate::obj::objfunction::PyFunction;
use crate::obj::objfunction::{PyFunction, PyMethod};
use crate::obj::objgenerator;
use crate::obj::objiter;
use crate::obj::objlist::PyList;
Expand Down Expand Up @@ -298,12 +298,15 @@ impl VirtualMachine {
{
return self.invoke_python_function(code, scope, defaults, args);
}
if let Some(PyMethod {
ref function,
ref object,
}) = func_ref.payload()
{
return self.invoke(function.clone(), args.insert(object.clone()));
}
match func_ref.payload {
PyObjectPayload::RustFunction { ref function } => function(self, args),
PyObjectPayload::BoundMethod {
ref function,
ref object,
} => self.invoke(function.clone(), args.insert(object.clone())),
ref payload => {
// TODO: is it safe to just invoke __call__ otherwise?
trace!("invoke __call__ for: {:?}", payload);
Expand Down