forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.rs
More file actions
177 lines (158 loc) · 5.4 KB
/
Copy pathpayload.rs
File metadata and controls
177 lines (158 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::object::{MaybeTraverse, Py, PyObjectRef, PyRef, PyResult};
use crate::{
PyObject, PyRefExact,
builtins::{PyBaseExceptionRef, PyType, PyTypeRef},
types::PyTypeFlags,
vm::{Context, VirtualMachine},
};
use core::ptr::NonNull;
cfg_select! {
feature = "threading" => {
pub trait PyThreadingConstraint: Send + Sync {}
impl<T: Send + Sync> PyThreadingConstraint for T {}
}
_ => {
pub trait PyThreadingConstraint {}
impl<T> PyThreadingConstraint for T {}
}
}
#[cold]
pub(crate) fn cold_downcast_type_error(
vm: &VirtualMachine,
class: &Py<PyType>,
obj: &PyObject,
) -> PyBaseExceptionRef {
vm.new_downcast_type_error(class, obj)
}
pub trait PyPayload: MaybeTraverse + PyThreadingConstraint + Sized + 'static {
const PAYLOAD_TYPE_ID: core::any::TypeId = core::any::TypeId::of::<Self>();
/// # Safety
/// This function should only be called if `payload_type_id` matches the type of `obj`.
#[inline]
unsafe fn validate_downcastable_from(_obj: &PyObject) -> bool {
true
}
fn try_downcast_from(obj: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
if obj.downcastable::<Self>() {
return Ok(());
}
let class = Self::class(&vm.ctx);
Err(cold_downcast_type_error(vm, class, obj))
}
fn class(ctx: &Context) -> &'static Py<PyType>;
/// Whether this type has a freelist. Types with freelists require
/// immediate (non-deferred) GC untracking during dealloc to prevent
/// race conditions when the object is reused.
const HAS_FREELIST: bool = false;
/// Maximum number of objects to keep in the freelist.
const MAX_FREELIST: usize = 0;
/// Try to push a dead object onto this type's freelist for reuse.
/// Returns true if the object was stored (caller must NOT free the memory).
/// Called before tp_clear, so the payload is still intact.
///
/// # Safety
/// `obj` must be a valid pointer to a `PyInner<Self>` with refcount 0.
/// The payload is still initialized and can be read for bucket selection.
#[inline]
unsafe fn freelist_push(_obj: *mut PyObject) -> bool {
false
}
/// Try to pop a pre-allocated object from this type's freelist.
/// The returned pointer still has the old payload; the caller must
/// reinitialize `ref_count`, `gc_bits`, and `payload`.
///
/// # Safety
/// The returned pointer (if Some) must point to a valid `PyInner<Self>`
/// whose payload is still initialized from a previous allocation. The caller
/// will drop and overwrite `payload` before reuse.
#[inline]
unsafe fn freelist_pop(_payload: &Self) -> Option<NonNull<PyObject>> {
None
}
#[inline]
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef
where
Self: core::fmt::Debug,
{
self.into_ref(&vm.ctx).into()
}
#[inline]
fn _into_ref(self, cls: PyTypeRef, ctx: &Context) -> PyRef<Self>
where
Self: core::fmt::Debug,
{
let dict = if cls.slots.flags.has_feature(PyTypeFlags::HAS_DICT) {
Some(ctx.new_dict())
} else {
None
};
PyRef::new_ref(self, cls, dict)
}
#[inline]
fn into_exact_ref(self, ctx: &Context) -> PyRefExact<Self>
where
Self: core::fmt::Debug,
{
unsafe {
// Self::into_ref() always returns exact typed PyRef
PyRefExact::new_unchecked(self.into_ref(ctx))
}
}
#[inline]
fn into_ref(self, ctx: &Context) -> PyRef<Self>
where
Self: core::fmt::Debug,
{
let cls = Self::class(ctx);
self._into_ref(cls.to_owned(), ctx)
}
#[inline]
fn into_ref_with_type(self, vm: &VirtualMachine, cls: PyTypeRef) -> PyResult<PyRef<Self>>
where
Self: core::fmt::Debug,
{
let exact_class = Self::class(&vm.ctx);
if cls.fast_issubclass(exact_class) {
if exact_class.slots.basicsize != cls.slots.basicsize {
#[cold]
#[inline(never)]
fn _into_ref_size_error(
vm: &VirtualMachine,
cls: &Py<PyType>,
exact_class: &Py<PyType>,
) -> PyBaseExceptionRef {
vm.new_type_error(format!(
"cannot create '{}' instance: size differs from base type '{}'",
cls.name(),
exact_class.name()
))
}
return Err(_into_ref_size_error(vm, &cls, exact_class));
}
Ok(self._into_ref(cls, &vm.ctx))
} else {
#[cold]
#[inline(never)]
fn _into_ref_with_type_error(
vm: &VirtualMachine,
cls: &Py<PyType>,
exact_class: &Py<PyType>,
) -> PyBaseExceptionRef {
vm.new_type_error(format!(
"'{}' is not a subtype of '{}'",
&cls.name(),
exact_class.name()
))
}
Err(_into_ref_with_type_error(vm, &cls, exact_class))
}
}
}
pub trait PyObjectPayload:
PyPayload + core::any::Any + core::fmt::Debug + MaybeTraverse + PyThreadingConstraint + 'static
{
}
impl<T: PyPayload + core::fmt::Debug + 'static> PyObjectPayload for T {}
pub trait SlotOffset {
fn offset() -> usize;
}