@@ -12,30 +12,35 @@ internal static class OperatorMethod
1212 // Maps the compiled method name in .NET CIL (e.g. op_Addition) to
1313 // the equivalent Python operator (e.g. __add__) as well as the offset
1414 // that identifies that operator's slot (e.g. nb_add) in heap space.
15- public static Dictionary < string , Tuple < string , int > > OpMethodMap { get ; private set ; }
16-
15+ public static Dictionary < string , SlotDefinition > OpMethodMap { get ; private set ; }
16+ public readonly struct SlotDefinition
17+ {
18+ public SlotDefinition ( string methodName , int typeOffset )
19+ {
20+ MethodName = methodName ;
21+ TypeOffset = typeOffset ;
22+ }
23+ public string MethodName { get ; }
24+ public int TypeOffset { get ; }
25+ }
1726 private static PyObject _opType ;
1827
1928 static OperatorMethod ( )
2029 {
2130 // TODO: Rich compare, inplace operator support
22- OpMethodMap = new Dictionary < string , Tuple < string , int > >
31+ OpMethodMap = new Dictionary < string , SlotDefinition >
2332 {
24- [ "op_Addition" ] = Tuple . Create ( "__add__" , TypeOffset . nb_add ) ,
25- [ "op_Subtraction" ] = Tuple . Create ( "__sub__" , TypeOffset . nb_subtract ) ,
26- [ "op_Multiply" ] = Tuple . Create ( "__mul__" , TypeOffset . nb_multiply ) ,
27- #if PYTHON2
28- [ "op_Division" ] = Tuple . Create ( "__div__" , TypeOffset . nb_divide ) ,
29- #else
30- [ "op_Division" ] = Tuple . Create ( "__truediv__" , TypeOffset . nb_true_divide ) ,
31- #endif
32- [ "op_BitwiseAnd" ] = Tuple . Create ( "__and__" , TypeOffset . nb_and ) ,
33- [ "op_BitwiseOr" ] = Tuple . Create ( "__or__" , TypeOffset . nb_or ) ,
34- [ "op_ExclusiveOr" ] = Tuple . Create ( "__xor__" , TypeOffset . nb_xor ) ,
35- [ "op_LeftShift" ] = Tuple . Create ( "__lshift__" , TypeOffset . nb_lshift ) ,
36- [ "op_RightShift" ] = Tuple . Create ( "__rshift__" , TypeOffset . nb_rshift ) ,
37- [ "op_Modulus" ] = Tuple . Create ( "__mod__" , TypeOffset . nb_remainder ) ,
38- [ "op_OneComplement" ] = Tuple . Create ( "__invert__" , TypeOffset . nb_invert )
33+ [ "op_Addition" ] = new SlotDefinition ( "__add__" , TypeOffset . nb_add ) ,
34+ [ "op_Subtraction" ] = new SlotDefinition ( "__sub__" , TypeOffset . nb_subtract ) ,
35+ [ "op_Multiply" ] = new SlotDefinition ( "__mul__" , TypeOffset . nb_multiply ) ,
36+ [ "op_Division" ] = new SlotDefinition ( "__truediv__" , TypeOffset . nb_true_divide ) ,
37+ [ "op_BitwiseAnd" ] = new SlotDefinition ( "__and__" , TypeOffset . nb_and ) ,
38+ [ "op_BitwiseOr" ] = new SlotDefinition ( "__or__" , TypeOffset . nb_or ) ,
39+ [ "op_ExclusiveOr" ] = new SlotDefinition ( "__xor__" , TypeOffset . nb_xor ) ,
40+ [ "op_LeftShift" ] = new SlotDefinition ( "__lshift__" , TypeOffset . nb_lshift ) ,
41+ [ "op_RightShift" ] = new SlotDefinition ( "__rshift__" , TypeOffset . nb_rshift ) ,
42+ [ "op_Modulus" ] = new SlotDefinition ( "__mod__" , TypeOffset . nb_remainder ) ,
43+ [ "op_OneComplement" ] = new SlotDefinition ( "__invert__" , TypeOffset . nb_invert )
3944 } ;
4045 }
4146
@@ -53,11 +58,6 @@ public static void Shutdown()
5358 }
5459 }
5560
56- public static bool IsOperatorMethod ( string methodName )
57- {
58- return OpMethodMap . ContainsKey ( methodName ) ;
59- }
60-
6161 public static bool IsOperatorMethod ( MethodBase method )
6262 {
6363 if ( ! method . IsSpecialName )
@@ -82,7 +82,7 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
8282 {
8383 continue ;
8484 }
85- int offset = OpMethodMap [ method . Name ] . Item2 ;
85+ int offset = OpMethodMap [ method . Name ] . TypeOffset ;
8686 // Copy the default implementation of e.g. the nb_add slot,
8787 // which simply calls __add__ on the type.
8888 IntPtr func = Marshal . ReadIntPtr ( _opType . Handle , offset ) ;
@@ -97,7 +97,7 @@ public static void FixupSlots(IntPtr pyType, Type clrType)
9797
9898 public static string GetPyMethodName ( string clrName )
9999 {
100- return OpMethodMap [ clrName ] . Item1 ;
100+ return OpMethodMap [ clrName ] . MethodName ;
101101 }
102102
103103 private static string GenerateDummyCode ( )
@@ -106,7 +106,7 @@ private static string GenerateDummyCode()
106106 sb . AppendLine ( "class OperatorMethod(object):" ) ;
107107 foreach ( var item in OpMethodMap . Values )
108108 {
109- string def = string . Format ( " def {0}(self, other): pass" , item . Item1 ) ;
109+ string def = string . Format ( " def {0}(self, other): pass" , item . MethodName ) ;
110110 sb . AppendLine ( def ) ;
111111 }
112112 return sb . ToString ( ) ;
0 commit comments