5
5
using System . Runtime . InteropServices ;
6
6
using System . Reflection ;
7
7
using System . Text ;
8
- using System . Collections . Generic ;
9
8
10
9
namespace Python . Runtime
11
10
{
@@ -69,27 +68,66 @@ public ModulePropertyAttribute()
69
68
}
70
69
}
71
70
71
+ internal static partial class TypeOffset
72
+ {
73
+ static TypeOffset ( )
74
+ {
75
+ Type type = typeof ( TypeOffset ) ;
76
+ FieldInfo [ ] fields = type . GetFields ( ) ;
77
+ int size = IntPtr . Size ;
78
+ for ( int i = 0 ; i < fields . Length ; i ++ )
79
+ {
80
+ int offset = i * size ;
81
+ FieldInfo fi = fields [ i ] ;
82
+ fi . SetValue ( null , offset ) ;
83
+ }
84
+ }
85
+
86
+ public static int magic ( ) => ManagedDataOffsets . Magic ;
87
+ }
88
+
72
89
internal static class ManagedDataOffsets
73
90
{
74
- static ManagedDataOffsets ( )
91
+ public static int Magic { get ; private set ; }
92
+ public static readonly Dictionary < string , int > NameMapping = new Dictionary < string , int > ( ) ;
93
+
94
+ static class DataOffsets
75
95
{
76
- FieldInfo [ ] fi = typeof ( ManagedDataOffsets ) . GetFields ( BindingFlags . Static | BindingFlags . Public ) ;
77
- for ( int i = 0 ; i < fi . Length ; i ++ )
96
+ public static readonly int ob_data ;
97
+ public static readonly int ob_dict ;
98
+
99
+ static DataOffsets ( )
78
100
{
79
- fi [ i ] . SetValue ( null , - ( i * IntPtr . Size ) - IntPtr . Size ) ;
101
+ FieldInfo [ ] fields = typeof ( DataOffsets ) . GetFields ( BindingFlags . Static | BindingFlags . Public ) ;
102
+ for ( int i = 0 ; i < fields . Length ; i ++ )
103
+ {
104
+ fields [ i ] . SetValue ( null , - ( i * IntPtr . Size ) - IntPtr . Size ) ;
105
+ }
80
106
}
107
+ }
81
108
82
- size = fi . Length * IntPtr . Size ;
109
+ static ManagedDataOffsets ( )
110
+ {
111
+ Type type = typeof ( TypeOffset ) ;
112
+ foreach ( FieldInfo fi in type . GetFields ( ) )
113
+ {
114
+ NameMapping [ fi . Name ] = ( int ) fi . GetValue ( null ) ;
115
+ }
116
+ Magic = TypeOffset . members ;
117
+ FieldInfo [ ] fields = typeof ( DataOffsets ) . GetFields ( BindingFlags . Static | BindingFlags . Public ) ;
118
+ size = fields . Length * IntPtr . Size ;
83
119
}
84
120
85
- public static readonly int ob_data ;
86
- public static readonly int ob_dict ;
121
+ public static int GetSlotOffset ( string name )
122
+ {
123
+ return NameMapping [ name ] ;
124
+ }
87
125
88
126
private static int BaseOffset ( IntPtr type )
89
127
{
90
128
Debug . Assert ( type != IntPtr . Zero ) ;
91
129
int typeSize = Marshal . ReadInt32 ( type , TypeOffset . tp_basicsize ) ;
92
- Debug . Assert ( typeSize > 0 && typeSize <= ExceptionOffset . Size ( ) ) ;
130
+ Debug . Assert ( typeSize > 0 ) ;
93
131
return typeSize ;
94
132
}
95
133
public static int DataOffset ( IntPtr type )
@@ -102,6 +140,8 @@ public static int DictOffset(IntPtr type)
102
140
return BaseOffset ( type ) + ob_dict ;
103
141
}
104
142
143
+ public static int ob_data => DataOffsets . ob_data ;
144
+ public static int ob_dict => DataOffsets . ob_dict ;
105
145
public static int Size { get { return size ; } }
106
146
107
147
private static readonly int size ;
@@ -320,34 +360,34 @@ public static void FreeModuleDef(IntPtr ptr)
320
360
/// </summary>
321
361
internal class TypeFlags
322
362
{
323
- public static int HeapType = ( 1 << 9 ) ;
324
- public static int BaseType = ( 1 << 10 ) ;
325
- public static int Ready = ( 1 << 12 ) ;
326
- public static int Readying = ( 1 << 13 ) ;
327
- public static int HaveGC = ( 1 << 14 ) ;
363
+ public const int HeapType = ( 1 << 9 ) ;
364
+ public const int BaseType = ( 1 << 10 ) ;
365
+ public const int Ready = ( 1 << 12 ) ;
366
+ public const int Readying = ( 1 << 13 ) ;
367
+ public const int HaveGC = ( 1 << 14 ) ;
328
368
// 15 and 16 are reserved for stackless
329
- public static int HaveStacklessExtension = 0 ;
369
+ public const int HaveStacklessExtension = 0 ;
330
370
/* XXX Reusing reserved constants */
331
- public static int Managed = ( 1 << 15 ) ; // PythonNet specific
332
- public static int Subclass = ( 1 << 16 ) ; // PythonNet specific
333
- public static int HaveIndex = ( 1 << 17 ) ;
371
+ public const int Managed = ( 1 << 15 ) ; // PythonNet specific
372
+ public const int Subclass = ( 1 << 16 ) ; // PythonNet specific
373
+ public const int HaveIndex = ( 1 << 17 ) ;
334
374
/* Objects support nb_index in PyNumberMethods */
335
- public static int HaveVersionTag = ( 1 << 18 ) ;
336
- public static int ValidVersionTag = ( 1 << 19 ) ;
337
- public static int IsAbstract = ( 1 << 20 ) ;
338
- public static int HaveNewBuffer = ( 1 << 21 ) ;
375
+ public const int HaveVersionTag = ( 1 << 18 ) ;
376
+ public const int ValidVersionTag = ( 1 << 19 ) ;
377
+ public const int IsAbstract = ( 1 << 20 ) ;
378
+ public const int HaveNewBuffer = ( 1 << 21 ) ;
339
379
// TODO: Implement FastSubclass functions
340
- public static int IntSubclass = ( 1 << 23 ) ;
341
- public static int LongSubclass = ( 1 << 24 ) ;
342
- public static int ListSubclass = ( 1 << 25 ) ;
343
- public static int TupleSubclass = ( 1 << 26 ) ;
344
- public static int StringSubclass = ( 1 << 27 ) ;
345
- public static int UnicodeSubclass = ( 1 << 28 ) ;
346
- public static int DictSubclass = ( 1 << 29 ) ;
347
- public static int BaseExceptionSubclass = ( 1 << 30 ) ;
348
- public static int TypeSubclass = ( 1 << 31 ) ;
349
-
350
- public static int Default = (
380
+ public const int IntSubclass = ( 1 << 23 ) ;
381
+ public const int LongSubclass = ( 1 << 24 ) ;
382
+ public const int ListSubclass = ( 1 << 25 ) ;
383
+ public const int TupleSubclass = ( 1 << 26 ) ;
384
+ public const int StringSubclass = ( 1 << 27 ) ;
385
+ public const int UnicodeSubclass = ( 1 << 28 ) ;
386
+ public const int DictSubclass = ( 1 << 29 ) ;
387
+ public const int BaseExceptionSubclass = ( 1 << 30 ) ;
388
+ public const int TypeSubclass = ( 1 << 31 ) ;
389
+
390
+ public const int Default = (
351
391
HaveStacklessExtension |
352
392
HaveVersionTag ) ;
353
393
}
0 commit comments