Skip to content

Commit 8c8d66e

Browse files
committed
* Move fields of ManagedDataOffsets into nested type
* Remove assert condition which is `typeSize <= ExceptionOffset.Size()` cause of the type size of `PyHeapTypeObject` derived from clr type may over `ExceptionOffset.Size()`
1 parent cc2219e commit 8c8d66e

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/runtime/interop.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ public ModulePropertyAttribute()
7171

7272
internal static partial class TypeOffset
7373
{
74+
static TypeOffset()
75+
{
76+
Type type = typeof(TypeOffset);
77+
FieldInfo[] fields = type.GetFields();
78+
int size = IntPtr.Size;
79+
for (int i = 0; i < fields.Length; i++)
80+
{
81+
int offset = i * size;
82+
FieldInfo fi = fields[i];
83+
fi.SetValue(null, offset);
84+
}
85+
}
86+
7487
public static int magic() => ManagedDataOffsets.Magic;
7588
}
7689

@@ -79,23 +92,33 @@ internal static class ManagedDataOffsets
7992
public static int Magic { get; private set; }
8093
public static readonly Dictionary<string, int> NameMapping = new Dictionary<string, int>();
8194

82-
public static readonly int ob_data;
83-
public static readonly int ob_dict;
95+
static class DataOffsets
96+
{
97+
public static readonly int ob_data;
98+
public static readonly int ob_dict;
99+
100+
static DataOffsets()
101+
{
102+
FieldInfo[] fields = typeof(DataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
103+
for (int i = 0; i < fields.Length; i++)
104+
{
105+
fields[i].SetValue(null, -(i * IntPtr.Size) - IntPtr.Size);
106+
}
107+
}
108+
}
84109

85110
static ManagedDataOffsets()
86111
{
87112
Type type = typeof(TypeOffset);
88-
FieldInfo[] fields = type.GetFields();
89-
int size = IntPtr.Size;
90-
for (int i = 0; i < fields.Length; i++)
113+
foreach (FieldInfo fi in type.GetFields())
91114
{
92-
int offset = i * size;
93-
FieldInfo fi = fields[i];
94-
fi.SetValue(null, offset);
95-
NameMapping[fi.Name] = offset;
115+
NameMapping[fi.Name] = (int)fi.GetValue(null);
96116
}
97117
// XXX: Use the members after PyHeapTypeObject as magic slot
98118
Magic = TypeOffset.members;
119+
120+
FieldInfo[] fields = typeof(DataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public);
121+
size = fields.Length * IntPtr.Size;
99122
}
100123

101124
public static int GetSlotOffset(string name)
@@ -107,20 +130,22 @@ private static int BaseOffset(IntPtr type)
107130
{
108131
Debug.Assert(type != IntPtr.Zero);
109132
int typeSize = Marshal.ReadInt32(type, TypeOffset.tp_basicsize);
110-
Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size());
133+
Debug.Assert(typeSize > 0);
111134
return typeSize;
112135
}
113136

114137
public static int DataOffset(IntPtr type)
115138
{
116-
return BaseOffset(type) + ob_data;
139+
return BaseOffset(type) + DataOffsets.ob_data;
117140
}
118141

119142
public static int DictOffset(IntPtr type)
120143
{
121-
return BaseOffset(type) + ob_dict;
144+
return BaseOffset(type) + DataOffsets.ob_dict;
122145
}
123146

147+
public static int ob_data => DataOffsets.ob_data;
148+
public static int ob_dict => DataOffsets.ob_dict;
124149
public static int Size { get { return size; } }
125150

126151
private static readonly int size;

src/runtime/managedtype.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,14 @@ protected static void ClearObjectDict(IntPtr ob)
226226

227227
protected static IntPtr GetObjectDict(IntPtr ob)
228228
{
229-
return Marshal.ReadIntPtr(ob, ObjectOffset.TypeDictOffset(ob));
229+
IntPtr type = Runtime.PyObject_TYPE(ob);
230+
return Marshal.ReadIntPtr(ob, ObjectOffset.TypeDictOffset(type));
230231
}
231232

232233
protected static void SetObjectDict(IntPtr ob, IntPtr value)
233234
{
234-
Marshal.WriteIntPtr(ob, ObjectOffset.TypeDictOffset(ob), value);
235+
IntPtr type = Runtime.PyObject_TYPE(ob);
236+
Marshal.WriteIntPtr(ob, ObjectOffset.TypeDictOffset(type), value);
235237
}
236238
}
237239
}

0 commit comments

Comments
 (0)