vm.invoke -> obj.call

This commit is contained in:
Jeong YunWon
2023-03-07 20:06:08 +09:00
parent ed60687f11
commit 5eb775462d
2 changed files with 22 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
use crate::{
function::IntoFuncArgs,
function::{FuncArgs, IntoFuncArgs},
types::GenericMethod,
{AsObject, PyObject, PyResult, VirtualMachine},
};
@@ -14,6 +14,23 @@ impl PyObject {
pub fn is_callable(&self) -> bool {
self.to_callable().is_some()
}
/// PyObject_Call*Arg* series
pub fn call(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult {
self.call_with_args(args.into_args(vm), vm)
}
/// PyObject_Call
pub fn call_with_args(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
vm_trace!("Invoke: {:?} {:?}", callable, args);
let Some(callable) = self.to_callable() else {
return Err(vm.new_type_error(format!(
"'{}' object is not callable",
self.class().name()
)));
};
callable.invoke(args, vm)
}
}
pub struct PyCallable<'a> {

View File

@@ -1,7 +1,7 @@
use super::PyMethod;
use crate::{
builtins::{PyBaseExceptionRef, PyList, PyStr, PyStrInterned},
function::{FuncArgs, IntoFuncArgs},
function::IntoFuncArgs,
identifier,
object::{AsObject, PyObject, PyObjectRef, PyPayload, PyResult},
vm::VirtualMachine,
@@ -151,19 +151,8 @@ impl VirtualMachine {
.invoke(args, self)
}
fn _invoke(&self, obj: &PyObject, args: FuncArgs) -> PyResult {
vm_trace!("Invoke: {:?} {:?}", callable, args);
let Some(callable) = obj.to_callable() else {
return Err(self.new_type_error(format!(
"'{}' object is not callable",
obj.class().name()
)));
};
callable.invoke(args, self)
}
#[inline(always)]
pub fn invoke(&self, func: &impl AsObject, args: impl IntoFuncArgs) -> PyResult {
self._invoke(func.as_object(), args.into_args(self))
// #[deprecated(note = "in favor of `obj.call(args, vm)`")]
pub fn invoke(&self, obj: &impl AsObject, args: impl IntoFuncArgs) -> PyResult {
obj.as_object().call(args, self)
}
}