Skip to content

Commit 7ed0c7a

Browse files
committed
WIP
1 parent a3591b6 commit 7ed0c7a

31 files changed

+555
-762
lines changed

src/runtime/DefaultBaseTypeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static BorrowedReference GetBaseType(Type type)
2424
return Exceptions.Exception;
2525

2626
return type.BaseType is not null
27-
? ClassManager.GetClass(type.BaseType).ObjectReference
27+
? ClassManager.GetClass(type.BaseType)
2828
: Runtime.PyBaseObjectType;
2929
}
3030

src/runtime/ManagedTypes.cd

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Class Name="Python.Runtime.ClassBase" Collapsed="true">
44
<Position X="15" Y="1.75" Width="1.5" />
55
<TypeIdentifier>
6-
<HashCode>FAAAAgAIAAAEDAAAAAAAAEACIACJAAIAAAAAAAIAAAQ=</HashCode>
6+
<HashCode>VAAAAgAIAAAEDAAAAAAAAEACIACLAAIAAAAAAAIAoBU=</HashCode>
77
<FileName>classbase.cs</FileName>
88
</TypeIdentifier>
99
</Class>
@@ -17,7 +17,7 @@
1717
<Class Name="Python.Runtime.ArrayObject" Collapsed="true">
1818
<Position X="10.5" Y="3.25" Width="1.5" />
1919
<TypeIdentifier>
20-
<HashCode>AAAAAAAAABAAAAAAAAAAACAAIAAJAAAAIAAAAACAAAI=</HashCode>
20+
<HashCode>AAQAgAAAABAQAAAAAAAARGAAMgAJAAAAJAACACSABAI=</HashCode>
2121
<FileName>arrayobject.cs</FileName>
2222
</TypeIdentifier>
2323
</Class>
@@ -31,28 +31,28 @@
3131
<Class Name="Python.Runtime.BoundContructor" Collapsed="true">
3232
<Position X="29.5" Y="4.5" Width="1.5" />
3333
<TypeIdentifier>
34-
<HashCode>AAAACAAAAAAABABAAAAACAAAABAJAEAAAAAAAAIAAAA=</HashCode>
34+
<HashCode>AAAACAAAAAAAAABAAAAACAAAABAJAAAAAAAAAAJAAAA=</HashCode>
3535
<FileName>constructorbinding.cs</FileName>
3636
</TypeIdentifier>
3737
</Class>
3838
<Class Name="Python.Runtime.CLRObject" Collapsed="true">
3939
<Position X="18.25" Y="1.75" Width="1.5" />
4040
<TypeIdentifier>
41-
<HashCode>EAAAAAAAAAAAAAAAAAACAAACBIAAAAJAAAAAAAAAAAA=</HashCode>
41+
<HashCode>AAAAAAAAAAAAAAAAAAACAAACAIAAAAIAAAAAAAAAABA=</HashCode>
4242
<FileName>clrobject.cs</FileName>
4343
</TypeIdentifier>
4444
</Class>
4545
<Class Name="Python.Runtime.CLRModule" Collapsed="true">
4646
<Position X="25" Y="4.75" Width="1.5" />
4747
<TypeIdentifier>
48-
<HashCode>AAAAAEAgIAQABAAAAABAAAAAIAIAAAAAAhAQAAAAKBA=</HashCode>
48+
<HashCode>AAAAAEIgIAQABAAAAABAAAAAIAIAAAAgAhAQAAAAKBA=</HashCode>
4949
<FileName>moduleobject.cs</FileName>
5050
</TypeIdentifier>
5151
</Class>
5252
<Class Name="Python.Runtime.ConstructorBinding" Collapsed="true">
5353
<Position X="29.5" Y="3.25" Width="1.5" />
5454
<TypeIdentifier>
55-
<HashCode>AAAACAAAAAAABAAAAAAACAAAABAJAEAAAAAAAAIAEAA=</HashCode>
55+
<HashCode>AAAACAAAAAAAAAAAAAAACAAAABAJAAAAAAAAAAJAEAA=</HashCode>
5656
<FileName>constructorbinding.cs</FileName>
5757
</TypeIdentifier>
5858
</Class>
@@ -74,14 +74,14 @@
7474
</Path>
7575
</InheritanceLine>
7676
<TypeIdentifier>
77-
<HashCode>AAAAAAAAAAAADAAAIAAAEABAAAAAAAACAAAAAAIAAAQ=</HashCode>
77+
<HashCode>AAAAAAAAAAAACAAAIAAAEABAAAAAAAACAAAAAAJAAAA=</HashCode>
7878
<FileName>eventbinding.cs</FileName>
7979
</TypeIdentifier>
8080
</Class>
8181
<Class Name="Python.Runtime.EventObject" Collapsed="true">
8282
<Position X="22.75" Y="3.25" Width="1.5" />
8383
<TypeIdentifier>
84-
<HashCode>AAACAAAAAAAAAAAAAAIAAIAAAAAEAAAAQABAAAIBEAQ=</HashCode>
84+
<HashCode>AAACAAAAAAAAAAAAAAIAAIAAAAAEAAAAQABAAAJBEAA=</HashCode>
8585
<FileName>eventobject.cs</FileName>
8686
</TypeIdentifier>
8787
</Class>
@@ -95,7 +95,7 @@
9595
<Class Name="Python.Runtime.ExtensionType" Collapsed="true">
9696
<Position X="27.5" Y="1.75" Width="1.5" />
9797
<TypeIdentifier>
98-
<HashCode>AAAAAAAAAAAAAAAAAAAAAAACAAAAAEEBAAAAAAABAAQ=</HashCode>
98+
<HashCode>AAAAAAAAAAAABAAAAAAAAAgCAAAAAEEBAAAAAABAABQ=</HashCode>
9999
<FileName>extensiontype.cs</FileName>
100100
</TypeIdentifier>
101101
</Class>
@@ -116,14 +116,14 @@
116116
<Class Name="Python.Runtime.ManagedType" Collapsed="true">
117117
<Position X="25" Y="0.5" Width="1.5" />
118118
<TypeIdentifier>
119-
<HashCode>UCBBgoBAIUgAAAEAACAAsAACAgAIABIAQYAAACIYIBA=</HashCode>
119+
<HashCode>UCBBgIAAAUgAAAAAASAAMACCAAAIABIAAZAAAAIYABA=</HashCode>
120120
<FileName>managedtype.cs</FileName>
121121
</TypeIdentifier>
122122
</Class>
123123
<Class Name="Python.Runtime.MetaType" Collapsed="true">
124124
<Position X="20.5" Y="1.75" Width="1.5" />
125125
<TypeIdentifier>
126-
<HashCode>AQAAAAAICBAAAQBAAAABAAIAAgABAAABAAAAUBCAAAQ=</HashCode>
126+
<HashCode>AQAAAAAICBAAAQBAAAABAAIAAgABAAABAAAAUBCAABQ=</HashCode>
127127
<FileName>metatype.cs</FileName>
128128
</TypeIdentifier>
129129
</Class>
@@ -138,14 +138,14 @@
138138
</Path>
139139
</InheritanceLine>
140140
<TypeIdentifier>
141-
<HashCode>EAAAAAAAAIAADABAIAAAAAAAAAgBAAAAUgAAAAIAAAQ=</HashCode>
141+
<HashCode>AAAAAAAAAIAACABAIAAAAAAAAAABAAAAUgAAAAJAAAE=</HashCode>
142142
<FileName>methodbinding.cs</FileName>
143143
</TypeIdentifier>
144144
</Class>
145145
<Class Name="Python.Runtime.MethodObject" Collapsed="true">
146146
<Position X="33.75" Y="3.25" Width="1.5" />
147147
<TypeIdentifier>
148-
<HashCode>FIADAAAAAAAIBAAAIAAIAAAIAAgFAAAAUAAgAAIAEAQ=</HashCode>
148+
<HashCode>BIADAAAAAAAIAAAAIAAIAAAIAAAFAAAAUAAgAAJAFAA=</HashCode>
149149
<FileName>methodobject.cs</FileName>
150150
</TypeIdentifier>
151151
</Class>
@@ -159,7 +159,7 @@
159159
<Class Name="Python.Runtime.ModuleObject" Collapsed="true">
160160
<Position X="25" Y="3.25" Width="1.5" />
161161
<TypeIdentifier>
162-
<HashCode>ECCCCkAAAAAABAAAAAABAAACAAAIAIIAEAAAAAIACAQ=</HashCode>
162+
<HashCode>ECCCCkAAgAAAAAAAAAABAAgCAABIAAIBEAAAAAJACAA=</HashCode>
163163
<FileName>moduleobject.cs</FileName>
164164
</TypeIdentifier>
165165
</Class>
@@ -188,7 +188,7 @@
188188
</Path>
189189
</InheritanceLine>
190190
<TypeIdentifier>
191-
<HashCode>AAAAAAAAAAAAAAAAIAAAAAAAAAABAAAAAgAAAAIAAAQ=</HashCode>
191+
<HashCode>AAAAAAAAAAAAAAAAIAAAAAAAAAABAAAAAgAAAAJAAAA=</HashCode>
192192
<FileName>overload.cs</FileName>
193193
</TypeIdentifier>
194194
</Class>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
3+
namespace Python.Runtime;
4+
5+
public class CLRMappedItem
6+
{
7+
public object Instance { get; private set; }
8+
public List<PyObject> PyRefs { get; set; } = new List<PyObject>();
9+
public bool Stored { get; set; }
10+
11+
public CLRMappedItem(object instance)
12+
{
13+
Instance = instance;
14+
}
15+
16+
internal void AddRef(PyObject pyRef)
17+
{
18+
this.PyRefs.Add(pyRef);
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Python.Runtime;
4+
5+
public class CLRWrapperCollection : KeyedCollection<object, CLRMappedItem>
6+
{
7+
public bool TryGetValue(object key, out CLRMappedItem value)
8+
{
9+
if (Dictionary == null)
10+
{
11+
value = null;
12+
return false;
13+
}
14+
return Dictionary.TryGetValue(key, out value);
15+
}
16+
17+
protected override object GetKeyForItem(CLRMappedItem item)
18+
{
19+
return item.Instance;
20+
}
21+
}

src/runtime/StateSerialization/ClassManagerState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ namespace Python.Runtime.StateSerialization;
77
internal class ClassManagerState
88
{
99
public Dictionary<PyType, InterDomainContext> Contexts { get; set; }
10-
public Dictionary<MaybeType, ClassBase> Cache { get; set; }
10+
public Dictionary<MaybeType, PyType> Cache { get; set; }
1111
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace Python.Runtime;
4+
5+
public interface ICLRObjectStorer
6+
{
7+
ICollection<CLRMappedItem> Store(CLRWrapperCollection wrappers, RuntimeDataStorage storage);
8+
CLRWrapperCollection Restore(RuntimeDataStorage storage);
9+
}

src/runtime/StateSerialization/SharedObjectsState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ namespace Python.Runtime.StateSerialization;
66
[Serializable]
77
internal class SharedObjectsState
88
{
9-
public List<CLRObject> InternalStores { get; set; }
10-
public List<ManagedType> Extensions { get; set; }
9+
public Dictionary<PyObject, CLRObject> InternalStores { get; set; }
10+
public Dictionary<PyObject, ExtensionType> Extensions { get; set; }
1111
public RuntimeDataStorage Wrappers { get; set; }
1212
public Dictionary<PyObject, InterDomainContext> Contexts { get; set; }
1313
}

src/runtime/arrayobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Python.Runtime
1111
/// to support natural array usage (indexing) from Python.
1212
/// </summary>
1313
[Serializable]
14-
internal class ArrayObject : ClassBase
14+
internal sealed class ArrayObject : ClassBase
1515
{
1616
internal ArrayObject(Type tp) : base(tp)
1717
{

src/runtime/classbase.cs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Linq;
66
using System.Reflection;
7+
using System.Runtime.InteropServices;
78

89
namespace 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

Comments
 (0)