Skip to content

Commit 4c5b584

Browse files
Merge pull request #244 from coolreader18/boxed-rustfunc
Change RustPyFunc from a fn pointer to a Fn trait
2 parents 79a3b13 + 09602a2 commit 4c5b584

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl Frame {
560560
bytecode::Instruction::LoadBuildClass => {
561561
let rustfunc = PyObject::new(
562562
PyObjectKind::RustFunction {
563-
function: builtins::builtin_build_class_,
563+
function: Box::new(builtins::builtin_build_class_),
564564
},
565565
vm.ctx.type_type(),
566566
);

vm/src/pyobject.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,24 @@ impl PyContext {
465465
)
466466
}
467467

468-
pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef {
468+
pub fn new_rustfunc<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
469+
&self,
470+
function: F,
471+
) -> PyObjectRef {
469472
PyObject::new(
470-
PyObjectKind::RustFunction { function: function },
473+
PyObjectKind::RustFunction {
474+
function: Box::new(function),
475+
},
476+
self.function_type(),
477+
)
478+
}
479+
480+
pub fn new_rustfunc_from_box(
481+
&self,
482+
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
483+
) -> PyObjectRef {
484+
PyObject::new(
485+
PyObjectKind::RustFunction { function },
471486
self.function_type(),
472487
)
473488
}
@@ -476,7 +491,10 @@ impl PyContext {
476491
PyObject::new(PyObjectKind::Frame { frame: frame }, self.frame_type())
477492
}
478493

479-
pub fn new_property(&self, function: RustPyFunc) -> PyObjectRef {
494+
pub fn new_property<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
495+
&self,
496+
function: F,
497+
) -> PyObjectRef {
480498
let fget = self.new_rustfunc(function);
481499
let py_obj = PyObject::new(
482500
PyObjectKind::Instance {
@@ -518,7 +536,10 @@ impl PyContext {
518536
)
519537
}
520538

521-
pub fn new_member_descriptor(&self, function: RustPyFunc) -> PyObjectRef {
539+
pub fn new_member_descriptor<F: 'static + Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>(
540+
&self,
541+
function: F,
542+
) -> PyObjectRef {
522543
let dict = self.new_dict();
523544
self.set_item(&dict, "function", self.new_rustfunc(function));
524545
self.new_instance(dict, self.member_descriptor_type())
@@ -791,8 +812,6 @@ impl PyFuncArgs {
791812
}
792813
}
793814

794-
type RustPyFunc = fn(vm: &mut VirtualMachine, PyFuncArgs) -> PyResult;
795-
796815
/// Rather than determining the type of a python object, this enum is more
797816
/// a holder for the rust payload of a python object. It is more a carrier
798817
/// of rust data for a particular python object. Determine the python type
@@ -869,7 +888,7 @@ pub enum PyObjectKind {
869888
dict: PyObjectRef,
870889
},
871890
RustFunction {
872-
function: RustPyFunc,
891+
function: Box<Fn(&mut VirtualMachine, PyFuncArgs) -> PyResult>,
873892
},
874893
}
875894

vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl VirtualMachine {
239239
pub fn invoke(&mut self, func_ref: PyObjectRef, args: PyFuncArgs) -> PyResult {
240240
trace!("Invoke: {:?} {:?}", func_ref, args);
241241
match func_ref.borrow().kind {
242-
PyObjectKind::RustFunction { function } => function(self, args),
242+
PyObjectKind::RustFunction { ref function } => function(self, args),
243243
PyObjectKind::Function {
244244
ref code,
245245
ref scope,

0 commit comments

Comments
 (0)