Skip to content

Commit 633a9b0

Browse files
committed
Move more methods onto PyClassRef.
1 parent 1e39512 commit 633a9b0

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

vm/src/obj/objtype.rs

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::vm::VirtualMachine;
99

1010
use super::objdict;
1111
use super::objlist::PyList;
12-
use super::objstr;
12+
use super::objstr::{self, PyStringRef};
1313
use super::objtuple::PyTuple;
1414

1515
#[derive(Clone, Debug)]
@@ -59,6 +59,35 @@ impl PyClassRef {
5959
offset: None,
6060
}
6161
}
62+
63+
fn mro(self, _vm: &mut VirtualMachine) -> PyTuple {
64+
PyTuple::from(_mro(&self))
65+
}
66+
67+
fn dir(self, vm: &mut VirtualMachine) -> PyList {
68+
let attributes = get_attributes(self);
69+
let attributes: Vec<PyObjectRef> = attributes
70+
.keys()
71+
.map(|k| vm.ctx.new_str(k.to_string()))
72+
.collect();
73+
PyList::from(attributes)
74+
}
75+
76+
fn instance_check(self, obj: PyObjectRef, _vm: &mut VirtualMachine) -> bool {
77+
isinstance(&obj, self.as_object())
78+
}
79+
80+
fn subclass_check(self, subclass: PyObjectRef, _vm: &mut VirtualMachine) -> bool {
81+
issubclass(&subclass, self.as_object())
82+
}
83+
84+
fn repr(self, _vm: &mut VirtualMachine) -> String {
85+
format!("<class '{}'>", self.name)
86+
}
87+
88+
fn prepare(_name: PyStringRef, _bases: PyObjectRef, vm: &mut VirtualMachine) -> PyObjectRef {
89+
vm.new_dict()
90+
}
6291
}
6392

6493
/*
@@ -86,21 +115,17 @@ pub fn init(ctx: &PyContext) {
86115
extend_class!(&ctx, &ctx.type_type, {
87116
"__call__" => ctx.new_rustfunc(type_call),
88117
"__new__" => ctx.new_rustfunc(type_new),
89-
"__mro__" => ctx.new_property(type_mro),
90-
"__repr__" => ctx.new_rustfunc(type_repr),
91-
"__prepare__" => ctx.new_rustfunc(type_prepare),
118+
"__mro__" => ctx.new_property(PyClassRef::mro),
119+
"__repr__" => ctx.new_rustfunc(PyClassRef::repr),
120+
"__prepare__" => ctx.new_rustfunc(PyClassRef::prepare),
92121
"__getattribute__" => ctx.new_rustfunc(type_getattribute),
93-
"__instancecheck__" => ctx.new_rustfunc(type_instance_check),
94-
"__subclasscheck__" => ctx.new_rustfunc(type_subclass_check),
122+
"__instancecheck__" => ctx.new_rustfunc(PyClassRef::instance_check),
123+
"__subclasscheck__" => ctx.new_rustfunc(PyClassRef::subclass_check),
95124
"__doc__" => ctx.new_str(type_doc.to_string()),
96-
"__dir__" => ctx.new_rustfunc(type_dir),
125+
"__dir__" => ctx.new_rustfunc(PyClassRef::dir),
97126
});
98127
}
99128

100-
fn type_mro(cls: PyClassRef, _vm: &mut VirtualMachine) -> PyResult<PyTuple> {
101-
Ok(PyTuple::from(_mro(&cls)))
102-
}
103-
104129
fn _mro(cls: &PyClassRef) -> Vec<PyObjectRef> {
105130
cls.iter_mro().cloned().collect()
106131
}
@@ -111,15 +136,6 @@ pub fn isinstance(obj: &PyObjectRef, cls: &PyObjectRef) -> bool {
111136
issubclass(obj.type_ref(), &cls)
112137
}
113138

114-
fn type_instance_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
115-
arg_check!(
116-
vm,
117-
args,
118-
required = [(typ, Some(vm.ctx.type_type())), (obj, None)]
119-
);
120-
Ok(vm.new_bool(isinstance(obj, typ)))
121-
}
122-
123139
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
124140
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
125141
/// method.
@@ -128,18 +144,6 @@ pub fn issubclass(subclass: &PyObjectRef, cls: &PyObjectRef) -> bool {
128144
subclass.is(&cls) || mro.iter().any(|c| c.is(&cls))
129145
}
130146

131-
fn type_subclass_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
132-
arg_check!(
133-
vm,
134-
args,
135-
required = [
136-
(cls, Some(vm.ctx.type_type())),
137-
(subclass, Some(vm.ctx.type_type()))
138-
]
139-
);
140-
Ok(vm.new_bool(issubclass(subclass, cls)))
141-
}
142-
143147
pub fn get_type_name(typ: &PyObjectRef) -> String {
144148
if let Some(PyClass { name, .. }) = &typ.payload::<PyClass>() {
145149
name.clone()
@@ -239,15 +243,6 @@ pub fn type_getattribute(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
239243
}
240244
}
241245

242-
pub fn type_dir(obj: PyClassRef, vm: &mut VirtualMachine) -> PyList {
243-
let attributes = get_attributes(obj);
244-
let attributes: Vec<PyObjectRef> = attributes
245-
.keys()
246-
.map(|k| vm.ctx.new_str(k.to_string()))
247-
.collect();
248-
PyList::from(attributes)
249-
}
250-
251246
pub fn get_attributes(cls: PyClassRef) -> PyAttributes {
252247
// Gather all members here:
253248
let mut attributes = PyAttributes::new();
@@ -335,16 +330,6 @@ pub fn new(
335330
.into_ref())
336331
}
337332

338-
fn type_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
339-
arg_check!(vm, args, required = [(obj, Some(vm.ctx.type_type()))]);
340-
let type_name = get_type_name(&obj);
341-
Ok(vm.new_str(format!("<class '{}'>", type_name)))
342-
}
343-
344-
fn type_prepare(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
345-
Ok(vm.new_dict())
346-
}
347-
348333
#[cfg(test)]
349334
mod tests {
350335
use super::{linearise_mro, new};

0 commit comments

Comments
 (0)