@@ -5,7 +5,8 @@ use crate::pystate::with_vm;
55use crate :: slots:: { PySlot , PySlotKind , PySlotType } ;
66use crate :: util:: CStrExt ;
77use crate :: util:: FfiPtrExt ;
8- use core:: ffi:: { c_char, c_int, c_ulong, c_void} ;
8+ use core:: ffi:: { c_char, c_int, c_uint, c_ulong, c_void} ;
9+ use core:: ptr;
910use rustpython_vm:: builtins:: { PyStr , PyType } ;
1011use rustpython_vm:: function:: { ItemDoc , PyMethodFlags } ;
1112use rustpython_vm:: types:: { AtomicPyTypeFlags , PyTypeFlags , PyTypeSlots , SlotAccessor } ;
@@ -22,6 +23,15 @@ pub struct PyType_Slot {
2223 pub pfunc : * mut c_void ,
2324}
2425
26+ #[ repr( C ) ]
27+ pub struct PyType_Spec {
28+ pub name : * const c_char ,
29+ pub basicsize : c_int ,
30+ pub itemsize : c_int ,
31+ pub flags : c_uint ,
32+ pub slots : * mut PyType_Slot ,
33+ }
34+
2535impl PyType_Slot {
2636 pub ( crate ) fn iter < ' a > ( mut slots : * const Self ) -> impl Iterator < Item = & ' a Self > {
2737 core:: iter:: from_fn ( move || {
@@ -104,11 +114,11 @@ pub unsafe extern "C" fn PyType_GetFullyQualifiedName(ptr: *mut PyTypeObject) ->
104114
105115#[ unsafe( no_mangle) ]
106116pub unsafe extern "C" fn PyType_GetSlot ( _ty : * const PyTypeObject , _slot : c_int ) -> * mut c_void {
107- core :: ptr:: null_mut ( )
117+ ptr:: null_mut ( )
108118}
109119
110120#[ unsafe( no_mangle) ]
111- pub extern "C" fn PyType_FromSlots ( slots : * const PySlot ) -> * mut PyObject {
121+ pub unsafe extern "C" fn PyType_FromSlots ( slots : * const PySlot ) -> * mut PyObject {
112122 with_vm ( |vm| {
113123 let mut name = None ;
114124 let mut base = None ;
@@ -254,6 +264,78 @@ pub extern "C" fn PyType_FromSlots(slots: *const PySlot) -> *mut PyObject {
254264 } )
255265}
256266
267+ #[ unsafe( no_mangle) ]
268+ pub unsafe extern "C" fn PyType_FromMetaclass (
269+ metaclass : * mut PyTypeObject ,
270+ module : * mut PyObject ,
271+ spec : * mut PyType_Spec ,
272+ bases : * mut PyObject ,
273+ ) -> * mut PyObject {
274+ let & PyType_Spec {
275+ name,
276+ basicsize,
277+ itemsize,
278+ flags,
279+ slots : type_slots,
280+ } = unsafe { & * spec } ;
281+ let mut slots = [ PySlot :: SLOT_END ; 9 ] ;
282+ let mut write_slot = {
283+ let mut iter = slots. iter_mut ( ) ;
284+ move |slot| {
285+ debug_assert ! (
286+ iter. len( ) >= 2 ,
287+ "PyType_FromMetaclass: last slot is reserved for SLOT_END"
288+ ) ;
289+ let entry = iter
290+ . next ( )
291+ . expect ( "Too many slots for PyType_FromMetaclass" ) ;
292+ * entry = slot;
293+ }
294+ } ;
295+
296+ if !bases. is_null ( ) {
297+ write_slot ( PySlot :: new ( 49 , bases) ) ;
298+ }
299+
300+ if !metaclass. is_null ( ) {
301+ write_slot ( PySlot :: new ( 107 , metaclass) ) ;
302+ }
303+
304+ if !module. is_null ( ) {
305+ write_slot ( PySlot :: new ( 108 , module) ) ;
306+ }
307+
308+ write_slot ( PySlot :: new ( 95 , name. cast_mut ( ) ) ) ;
309+ write_slot ( PySlot :: new ( 96 , basicsize as isize ) ) ;
310+ write_slot ( PySlot :: new ( 98 , itemsize as isize ) ) ;
311+ write_slot ( PySlot :: new ( 99 , flags as u64 ) ) ;
312+ write_slot ( PySlot :: new ( 93 , type_slots) ) ;
313+
314+ unsafe { PyType_FromSlots ( slots. as_ptr ( ) ) }
315+ }
316+
317+ #[ unsafe( no_mangle) ]
318+ pub unsafe extern "C" fn PyType_FromModuleAndSpec (
319+ module : * mut PyObject ,
320+ spec : * mut PyType_Spec ,
321+ bases : * mut PyObject ,
322+ ) -> * mut PyObject {
323+ unsafe { PyType_FromMetaclass ( ptr:: null_mut ( ) , module, spec, bases) }
324+ }
325+
326+ #[ unsafe( no_mangle) ]
327+ pub unsafe extern "C" fn PyType_FromSpecWithBases (
328+ spec : * mut PyType_Spec ,
329+ bases : * mut PyObject ,
330+ ) -> * mut PyObject {
331+ unsafe { PyType_FromMetaclass ( ptr:: null_mut ( ) , ptr:: null_mut ( ) , spec, bases) }
332+ }
333+
334+ #[ unsafe( no_mangle) ]
335+ pub unsafe extern "C" fn PyType_FromSpec ( spec : * mut PyType_Spec ) -> * mut PyObject {
336+ unsafe { PyType_FromMetaclass ( ptr:: null_mut ( ) , ptr:: null_mut ( ) , spec, ptr:: null_mut ( ) ) }
337+ }
338+
257339#[ unsafe( no_mangle) ]
258340pub unsafe extern "C" fn PyObject_GetTypeData (
259341 obj : * mut PyObject ,
0 commit comments