Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
slot inheritance
  • Loading branch information
youknowone committed May 25, 2022
commit cffe5e35938448c871ffa86a7d20aa7f3f2f5ec9
3 changes: 2 additions & 1 deletion vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl PyType {
.map(|x| x.iter_mro().cloned().collect())
.collect();
let mro = linearise_mro(mros)?;
slots.inherits(&mro);

if base.slots.flags.has_feature(PyTypeFlags::HAS_DICT) {
slots.flags |= PyTypeFlags::HAS_DICT
Expand Down Expand Up @@ -521,7 +522,7 @@ impl PyType {
// updated when __slots__ are supported (toggling the flag off if
// a class has __slots__ defined).
let flags = PyTypeFlags::heap_type_flags() | PyTypeFlags::HAS_DICT;
let slots = PyTypeSlots::from_flags(flags);
let slots = PyTypeSlots::with_flags(flags);

let typ = Self::new_verbose_ref(name.as_str(), base, bases, attributes, slots, metatype)
.map_err(|e| vm.new_type_error(e))?;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! py_class {
( $ctx:expr, $class_name:expr, $class_base:expr, $flags:expr, { $($name:tt => $value:expr),* $(,)* }) => {
{
#[allow(unused_mut)]
let mut slots = $crate::types::PyTypeSlots::from_flags($crate::types::PyTypeFlags::DEFAULT | $flags);
let mut slots = $crate::types::PyTypeSlots::with_flags($crate::types::PyTypeFlags::DEFAULT | $flags);
$($crate::py_class!(@extract_slots($ctx, &mut slots, $name, $value));)*
let py_class = $ctx.new_class(None, $class_name, $class_base, slots);
$($crate::py_class!(@extract_attrs($ctx, &py_class, $name, $value));)*
Expand Down
2 changes: 1 addition & 1 deletion vm/src/protocol/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl PySequence<'_> {
self.methods.get_or_init(|| {
let cls = self.obj.class();
if !cls.is(&vm.ctx.types.dict_type) {
if let Some(f) = cls.mro_find_map(|x| x.slots.as_sequence.load()) {
if let Some(f) = cls.slots.as_sequence.load() {
return f(self.obj, vm);
}
}
Expand Down
32 changes: 31 additions & 1 deletion vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,42 @@ pub struct PyTypeSlots {
}

impl PyTypeSlots {
pub fn from_flags(flags: PyTypeFlags) -> Self {
pub fn with_flags(flags: PyTypeFlags) -> Self {
Self {
flags,
..Default::default()
}
}

pub fn inherits(&mut self, mro: &[PyTypeRef]) {
macro_rules! inherit {
($name:ident) => {
if self.$name.load().is_none() {
for ty in mro {
if let Some(func) = ty.slots.$name.load() {
self.$name.store(Some(func));
break;
}
}
}
};
}
inherit!(as_sequence);
inherit!(as_mapping);
inherit!(hash);
inherit!(call);
inherit!(getattro);
inherit!(setattro);
// inherit!(as_buffer);
inherit!(richcompare);
inherit!(iter);
inherit!(iternext);
inherit!(descr_get);
inherit!(descr_set);
inherit!(init);
inherit!(new);
inherit!(del);
}
}

impl std::fmt::Debug for PyTypeSlots {
Expand Down