Skip to content

Commit 81f4f95

Browse files
committed
impl SlotCall for PyClass
1 parent 8bf3a19 commit 81f4f95

6 files changed

Lines changed: 118 additions & 120 deletions

File tree

vm/src/obj/objbuiltinfunc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::function::{OptionalArg, PyFuncArgs, PyNativeFunc};
55
use crate::obj::objstr::PyStringRef;
66
use crate::obj::objtype::PyClassRef;
77
use crate::pyobject::{
8-
PyClassImpl, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
8+
PyClassImpl, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
99
};
1010
use crate::slots::{SlotCall, SlotDescriptor};
1111
use crate::vm::VirtualMachine;
@@ -107,8 +107,8 @@ impl PyBuiltinFunction {
107107
}
108108

109109
impl SlotCall for PyBuiltinFunction {
110-
fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
111-
(self.value.func)(vm, args)
110+
fn call(zelf: PyRef<Self>, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
111+
(zelf.value.func)(vm, args)
112112
}
113113
}
114114

@@ -187,8 +187,8 @@ impl SlotDescriptor for PyBuiltinMethod {
187187
}
188188

189189
impl SlotCall for PyBuiltinMethod {
190-
fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
191-
(self.value.func)(vm, args)
190+
fn call(zelf: PyRef<Self>, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
191+
(zelf.value.func)(vm, args)
192192
}
193193
}
194194

vm/src/obj/objfunction.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ pub struct PyFunction {
4040
kw_only_defaults: Option<PyDictRef>,
4141
}
4242

43-
impl SlotDescriptor for PyFunction {
44-
fn descr_get(
45-
vm: &VirtualMachine,
46-
zelf: PyObjectRef,
47-
obj: Option<PyObjectRef>,
48-
cls: OptionalArg<PyObjectRef>,
49-
) -> PyResult {
50-
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
51-
if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
52-
Ok(zelf.into_object())
53-
} else {
54-
Ok(vm.ctx.new_bound_method(zelf.into_object(), obj))
55-
}
56-
}
57-
}
58-
5943
impl PyFunction {
6044
pub fn new(
6145
code: PyCodeRef,
@@ -285,14 +269,8 @@ impl PyValue for PyFunction {
285269
}
286270
}
287271

288-
#[pyimpl(with(SlotDescriptor), flags(HAS_DICT))]
272+
#[pyimpl(with(SlotDescriptor, SlotCall), flags(HAS_DICT))]
289273
impl PyFunction {
290-
#[pyslot]
291-
#[pymethod(magic)]
292-
fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
293-
self.invoke(args, vm)
294-
}
295-
296274
#[pyproperty(magic)]
297275
fn code(&self) -> PyCodeRef {
298276
self.code.clone()
@@ -326,6 +304,28 @@ impl PyFunction {
326304
}
327305
}
328306

307+
impl SlotDescriptor for PyFunction {
308+
fn descr_get(
309+
vm: &VirtualMachine,
310+
zelf: PyObjectRef,
311+
obj: Option<PyObjectRef>,
312+
cls: OptionalArg<PyObjectRef>,
313+
) -> PyResult {
314+
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
315+
if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
316+
Ok(zelf.into_object())
317+
} else {
318+
Ok(vm.ctx.new_bound_method(zelf.into_object(), obj))
319+
}
320+
}
321+
}
322+
323+
impl SlotCall for PyFunction {
324+
fn call(zelf: PyRef<Self>, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
325+
zelf.invoke(args, vm)
326+
}
327+
}
328+
329329
#[pyclass(module = false, name = "method")]
330330
#[derive(Debug)]
331331
pub struct PyBoundMethod {
@@ -335,9 +335,9 @@ pub struct PyBoundMethod {
335335
}
336336

337337
impl SlotCall for PyBoundMethod {
338-
fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
339-
let args = args.insert(self.object.clone());
340-
vm.invoke(&self.function, args)
338+
fn call(zelf: PyRef<Self>, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
339+
let args = args.insert(zelf.object.clone());
340+
vm.invoke(&zelf.function, args)
341341
}
342342
}
343343

0 commit comments

Comments
 (0)