Skip to content
Closed
Changes from all commits
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
43 changes: 41 additions & 2 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::any::Any;
use std::any::TypeId;
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::hash_map::{Entry, HashMap};
use std::fmt;
use std::marker::PhantomData;
use std::mem;
Expand Down Expand Up @@ -162,6 +163,7 @@ pub struct PyContext {
pub weakproxy_type: PyClassRef,
pub object: PyClassRef,
pub exceptions: exceptions::ExceptionZoo,
rust_classes: RefCell<HashMap<TypeId, PyClassRef>>,
}

pub fn create_type(name: &str, type_type: &PyClassRef, base: &PyClassRef) -> PyClassRef {
Expand Down Expand Up @@ -367,6 +369,7 @@ impl PyContext {
weakproxy_type,
type_type,
exceptions,
rust_classes: RefCell::default(),
};
objtype::init(&context);
objlist::init(&context);
Expand Down Expand Up @@ -407,6 +410,36 @@ impl PyContext {
context
}

pub fn _add_class(&self, typeid: TypeId, class: PyClassRef) {
let classes = &mut self.rust_classes.borrow_mut();
match classes.entry(typeid) {
Entry::Occupied(o) => panic!(
"Attempted to add rust type twice to the same PyContext, previously held class \
was {:?}",
o.get()
),
Entry::Vacant(v) => {
v.insert(class);
}
}
}

#[inline]
pub fn add_class<T: PyClassImpl + 'static>(&self) -> PyClassRef {
let class = T::make_class(self);
self._add_class(TypeId::of::<T>(), class.clone());
class
}

pub fn _get_class(&self, typeid: &TypeId) -> Option<PyClassRef> {
self.rust_classes.borrow_mut().get(typeid).cloned()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use borrow_mut here, we are not mutating the mapping.

}

#[inline]
pub fn get_class<T: 'static>(&self) -> Option<PyClassRef> {
self._get_class(&TypeId::of::<T>())
}

pub fn bytearray_type(&self) -> PyClassRef {
self.bytearray_type.clone()
}
Expand Down Expand Up @@ -1235,7 +1268,13 @@ impl PyObject<dyn PyObjectPayload> {
pub trait PyValue: fmt::Debug + Sized + 'static {
const HAVE_DICT: bool = false;

fn class(vm: &VirtualMachine) -> PyClassRef;
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.ctx.get_class::<Self>().expect(
"Default .class implementation cannot find your class in PyContext.rust_classes. \
Is there some special way to access your class, or did you forget to call \
`ctx.add_class::<T>()`?",
)
}

fn into_ref(self, vm: &VirtualMachine) -> PyRef<Self> {
PyRef {
Expand Down