Skip to content
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
2 changes: 1 addition & 1 deletion vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl Frame {
bytecode::Instruction::LoadBuildClass => {
let rustfunc = PyObject::new(
PyObjectKind::RustFunction {
function: builtins::builtin_build_class_,
function: Box::new(builtins::builtin_build_class_),
},
vm.ctx.type_type(),
);
Expand Down
33 changes: 26 additions & 7 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,24 @@ impl PyContext {
)
}

pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef {
pub fn new_rustfunc<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
&self,
function: F,
) -> PyObjectRef {
PyObject::new(
PyObjectKind::RustFunction { function: function },
PyObjectKind::RustFunction {
function: Box::new(function),
},
self.function_type(),
)
}

pub fn new_rustfunc_from_box(
&self,
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
) -> PyObjectRef {
PyObject::new(
PyObjectKind::RustFunction { function },
self.function_type(),
)
}
Expand All @@ -463,7 +478,10 @@ impl PyContext {
PyObject::new(PyObjectKind::Frame { frame: frame }, self.frame_type())
}

pub fn new_property(&self, function: RustPyFunc) -> PyObjectRef {
pub fn new_property<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
&self,
function: F,
) -> PyObjectRef {
let fget = self.new_rustfunc(function);
let py_obj = PyObject::new(
PyObjectKind::Instance {
Expand Down Expand Up @@ -505,7 +523,10 @@ impl PyContext {
)
}

pub fn new_member_descriptor(&self, function: RustPyFunc) -> PyObjectRef {
pub fn new_member_descriptor<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
&self,
function: F,
) -> PyObjectRef {
let dict = self.new_dict();
self.set_item(&dict, "function", self.new_rustfunc(function));
self.new_instance(dict, self.member_descriptor_type())
Expand Down Expand Up @@ -772,8 +793,6 @@ impl PyFuncArgs {
}
}

type RustPyFunc = fn(vm: &mut VirtualMachine, PyFuncArgs) -> PyResult;

/// Rather than determining the type of a python object, this enum is more
/// a holder for the rust payload of a python object. It is more a carrier
/// of rust data for a particular python object. Determine the python type
Expand Down Expand Up @@ -850,7 +869,7 @@ pub enum PyObjectKind {
dict: PyObjectRef,
},
RustFunction {
function: RustPyFunc,
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
},
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl VirtualMachine {
pub fn invoke(&mut self, func_ref: PyObjectRef, args: PyFuncArgs) -> PyResult {
trace!("Invoke: {:?} {:?}", func_ref, args);
match func_ref.borrow().kind {
PyObjectKind::RustFunction { function } => function(self, args),
PyObjectKind::RustFunction { ref function } => function(self, args),
PyObjectKind::Function {
ref code,
ref scope,
Expand Down