| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cc2219e commit 8c8d66e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,6 +71,19 @@ public ModulePropertyAttribute() | |||
| 71 | 71 | ||
| 72 | 72 | internal static partial class TypeOffset | |
| 73 | 73 | { | |
| 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 | + | ||
| 74 | 87 | public static int magic() => ManagedDataOffsets.Magic; | |
| 75 | 88 | } | |
| 76 | 89 | ||
@@ -79,23 +92,33 @@ internal static class ManagedDataOffsets | |||
| 79 | 92 | public static int Magic { get; private set; } | |
| 80 | 93 | public static readonly Dictionary<string, int> NameMapping = new Dictionary<string, int>(); | |
| 81 | 94 | ||
| 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 | + } | ||
| 84 | 109 | ||
| 85 | 110 | static ManagedDataOffsets() | |
| 86 | 111 | { | |
| 87 | 112 | 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()) | ||
| 91 | 114 | { | |
| 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); | ||
| 96 | 116 | } | |
| 97 | 117 | // XXX: Use the members after PyHeapTypeObject as magic slot | |
| 98 | 118 | Magic = TypeOffset.members; | |
| 119 | + | ||
| 120 | + FieldInfo[] fields = typeof(DataOffsets).GetFields(BindingFlags.Static | BindingFlags.Public); | ||
| 121 | + size = fields.Length * IntPtr.Size; | ||
| 99 | 122 | } | |
| 100 | 123 | ||
| 101 | 124 | public static int GetSlotOffset(string name) | |
@@ -107,20 +130,22 @@ private static int BaseOffset(IntPtr type) | |||
| 107 | 130 | { | |
| 108 | 131 | Debug.Assert(type != IntPtr.Zero); | |
| 109 | 132 | int typeSize = Marshal.ReadInt32(type, TypeOffset.tp_basicsize); | |
| 110 | - Debug.Assert(typeSize > 0 && typeSize <= ExceptionOffset.Size()); | ||
| 133 | + Debug.Assert(typeSize > 0); | ||
| 111 | 134 | return typeSize; | |
| 112 | 135 | } | |
| 113 | 136 | ||
| 114 | 137 | public static int DataOffset(IntPtr type) | |
| 115 | 138 | { | |
| 116 | - return BaseOffset(type) + ob_data; | ||
| 139 | + return BaseOffset(type) + DataOffsets.ob_data; | ||
| 117 | 140 | } | |
| 118 | 141 | ||
| 119 | 142 | public static int DictOffset(IntPtr type) | |
| 120 | 143 | { | |
| 121 | - return BaseOffset(type) + ob_dict; | ||
| 144 | + return BaseOffset(type) + DataOffsets.ob_dict; | ||
| 122 | 145 | } | |
| 123 | 146 | ||
| 147 | + public static int ob_data => DataOffsets.ob_data; | ||
| 148 | + public static int ob_dict => DataOffsets.ob_dict; | ||
| 124 | 149 | public static int Size { get { return size; } } | |
| 125 | 150 | ||
| 126 | 151 | private static readonly int size; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -226,12 +226,14 @@ protected static void ClearObjectDict(IntPtr ob) | |||
| 226 | 226 | ||
| 227 | 227 | protected static IntPtr GetObjectDict(IntPtr ob) | |
| 228 | 228 | { | |
| 229 | - return Marshal.ReadIntPtr(ob, ObjectOffset.TypeDictOffset(ob)); | ||
| 229 | + IntPtr type = Runtime.PyObject_TYPE(ob); | ||
| 230 | + return Marshal.ReadIntPtr(ob, ObjectOffset.TypeDictOffset(type)); | ||
| 230 | 231 | } | |
| 231 | 232 | ||
| 232 | 233 | protected static void SetObjectDict(IntPtr ob, IntPtr value) | |
| 233 | 234 | { | |
| 234 | - Marshal.WriteIntPtr(ob, ObjectOffset.TypeDictOffset(ob), value); | ||
| 235 | + IntPtr type = Runtime.PyObject_TYPE(ob); | ||
| 236 | + Marshal.WriteIntPtr(ob, ObjectOffset.TypeDictOffset(type), value); | ||
| 235 | 237 | } | |
| 236 | 238 | } | |
| 237 | 239 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments