From 09602a2ec622d168cccebf1b53b69770abfe3268 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Thu, 27 Dec 2018 09:18:00 -0600 Subject: [PATCH] Change RustPyFunc from a fn pointer to a Fn trait --- vm/src/frame.rs | 2 +- vm/src/pyobject.rs | 33 ++++++++++++++++++++++++++------- vm/src/vm.rs | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 19fa17bd7f0..c2180702c3d 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -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(), ); diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 6e21c269e9a..4e9eeaf3ccf 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -452,9 +452,24 @@ impl PyContext { ) } - pub fn new_rustfunc(&self, function: RustPyFunc) -> PyObjectRef { + pub fn new_rustfunc 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 PyResult>, + ) -> PyObjectRef { + PyObject::new( + PyObjectKind::RustFunction { function }, self.function_type(), ) } @@ -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 PyResult>( + &self, + function: F, + ) -> PyObjectRef { let fget = self.new_rustfunc(function); let py_obj = PyObject::new( PyObjectKind::Instance { @@ -505,7 +523,10 @@ impl PyContext { ) } - pub fn new_member_descriptor(&self, function: RustPyFunc) -> PyObjectRef { + pub fn new_member_descriptor 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()) @@ -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 @@ -850,7 +869,7 @@ pub enum PyObjectKind { dict: PyObjectRef, }, RustFunction { - function: RustPyFunc, + function: Box PyResult>, }, } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index c2c7b561e70..9ef29f3fe5d 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -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,