44using System . Diagnostics ;
55using System . Linq ;
66using System . Reflection ;
7+ using System . Runtime . InteropServices ;
78
89namespace Python . Runtime
910{
@@ -24,12 +25,6 @@ internal class ClassBase : ManagedType
2425 internal readonly Dictionary < int , MethodObject > richcompare = new ( ) ;
2526 internal MaybeType type ;
2627
27- internal new PyType pyHandle
28- {
29- get => ( PyType ) base . pyHandle ;
30- set => base . pyHandle = value ;
31- }
32-
3328 internal ClassBase ( Type tp )
3429 {
3530 if ( tp is null ) throw new ArgumentNullException ( nameof ( type ) ) ;
@@ -83,8 +78,8 @@ public virtual NewReference type_subscript(BorrowedReference idx)
8378 {
8479 return Exceptions . RaiseTypeError ( e . Message ) ;
8580 }
86- ManagedType c = ClassManager . GetClass ( t ) ;
87- return new NewReference ( c . ObjectReference ) ;
81+ var c = ClassManager . GetClass ( t ) ;
82+ return new NewReference ( c ) ;
8883 }
8984
9085 return Exceptions . RaiseTypeError ( $ "{ type . Value . Namespace } .{ type . Name } does not accept { types . Length } generic parameters") ;
@@ -254,7 +249,7 @@ public static NewReference tp_iter(BorrowedReference ob)
254249 }
255250 }
256251
257- return new NewReference ( new Iterator ( o , elemType ) . ObjectReference ) ;
252+ return new Iterator ( o , elemType ) . Alloc ( ) ;
258253 }
259254
260255
@@ -339,11 +334,16 @@ public static NewReference tp_repr(BorrowedReference ob)
339334 /// </summary>
340335 public static void tp_dealloc ( NewReference lastRef )
341336 {
342- ManagedType self = GetManagedObject ( lastRef . Borrow ( ) ) ! ;
337+ var self = ( CLRObject ) GetManagedObject ( lastRef . Borrow ( ) ) ! ;
338+ GCHandle gcHandle = GetGCHandle ( lastRef . Borrow ( ) ) ;
343339 tp_clear ( lastRef . Borrow ( ) ) ;
344340 Runtime . PyObject_GC_UnTrack ( lastRef . Borrow ( ) ) ;
345341 Runtime . PyObject_GC_Del ( lastRef . Steal ( ) ) ;
346- self ? . FreeGCHandle ( ) ;
342+
343+ bool deleted = CLRObject . reflectedObjects . Remove ( lastRef . DangerousGetAddress ( ) ) ;
344+ Debug . Assert ( deleted ) ;
345+
346+ gcHandle . Free ( ) ;
347347 }
348348
349349 public static int tp_clear ( BorrowedReference ob )
@@ -399,26 +399,17 @@ static unsafe int BaseUnmanagedClear(BorrowedReference ob)
399399 return clear ( ob ) ;
400400 }
401401
402- protected override void OnSave ( InterDomainContext context )
402+ protected override void OnSave ( BorrowedReference ob , InterDomainContext context )
403403 {
404- base . OnSave ( context ) ;
405- if ( ! this . IsClrMetaTypeInstance ( ) )
406- {
407- BorrowedReference dict = GetObjectDict ( ObjectReference ) ;
408- context . Storage . AddValue ( "dict" , PyObject . FromNullableReference ( dict ) ) ;
409- }
404+ base . OnSave ( ob , context ) ;
405+ context . Storage . AddValue ( "impl" , this ) ;
410406 }
411407
412- protected override void OnLoad ( InterDomainContext context )
408+ protected override void OnLoad ( BorrowedReference ob , InterDomainContext context )
413409 {
414- base . OnLoad ( context ) ;
415- if ( ! this . IsClrMetaTypeInstance ( ) )
416- {
417- var dict = context . Storage . GetValue < PyObject > ( "dict" ) ;
418- SetObjectDict ( ObjectReference , dict . NewReferenceOrNull ( ) . StealNullable ( ) ) ;
419- }
420- gcHandle = AllocGCHandle ( ) ;
421- SetGCHandle ( ObjectReference , gcHandle ) ;
410+ base . OnLoad ( ob , context ) ;
411+ var gcHandle = GCHandle . Alloc ( this ) ;
412+ SetGCHandle ( ob , gcHandle ) ;
422413 }
423414
424415
@@ -539,14 +530,14 @@ static IEnumerable<MethodInfo> GetCallImplementations(Type type)
539530 => type . GetMethods ( BindingFlags . Public | BindingFlags . Instance )
540531 . Where ( m => m . Name == "__call__" ) ;
541532
542- public virtual void InitializeSlots ( SlotsHolder slotsHolder )
533+ public virtual void InitializeSlots ( BorrowedReference pyType , SlotsHolder slotsHolder )
543534 {
544535 if ( ! this . type . Valid ) return ;
545536
546537 if ( GetCallImplementations ( this . type . Value ) . Any ( )
547538 && ! slotsHolder . IsHolding ( TypeOffset . tp_call ) )
548539 {
549- TypeManager . InitializeSlot ( ObjectReference , TypeOffset . tp_call , new Interop . BBB_N ( tp_call_impl ) , slotsHolder ) ;
540+ TypeManager . InitializeSlot ( pyType , TypeOffset . tp_call , new Interop . BBB_N ( tp_call_impl ) , slotsHolder ) ;
550541 }
551542 }
552543 }
0 commit comments