@@ -23,7 +23,6 @@ namespace Python.Runtime
2323 /// </summary>
2424 internal class NativeCall
2525 {
26- #if NETSTANDARD
2726 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
2827 private delegate void Void_1_Delegate ( IntPtr a1 ) ;
2928
@@ -48,132 +47,5 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
4847 var d = Marshal . GetDelegateForFunctionPointer < Interop . ObjObjArgFunc > ( fp ) ;
4948 return d ( a1 , a2 , a3 ) ;
5049 }
51- #else
52- private static AssemblyBuilder aBuilder ;
53- private static ModuleBuilder mBuilder ;
54-
55- public static INativeCall Impl ;
56-
57- static NativeCall ( )
58- {
59- // The static constructor is responsible for generating the
60- // assembly and the methods that implement the IJW thunks.
61- //
62- // To do this, we actually use reflection on the INativeCall
63- // interface (defined below) and generate the required thunk
64- // code based on the method signatures.
65-
66- var aname = new AssemblyName { Name = "e__NativeCall_Assembly" } ;
67- var aa = AssemblyBuilderAccess . Run ;
68-
69- aBuilder = Thread . GetDomain ( ) . DefineDynamicAssembly ( aname , aa ) ;
70- mBuilder = aBuilder . DefineDynamicModule ( "e__NativeCall_Module" ) ;
71-
72- var ta = TypeAttributes . Public ;
73- TypeBuilder tBuilder = mBuilder . DefineType ( "e__NativeCall" , ta ) ;
74-
75- Type iType = typeof ( INativeCall ) ;
76- tBuilder . AddInterfaceImplementation ( iType ) ;
77-
78- // Use reflection to loop over the INativeCall interface methods,
79- // calling GenerateThunk to create a managed thunk for each one.
80-
81- foreach ( MethodInfo method in iType . GetMethods ( ) )
82- {
83- GenerateThunk ( tBuilder , method ) ;
84- }
85-
86- Type theType = tBuilder . CreateType ( ) ;
87-
88- Impl = ( INativeCall ) Activator . CreateInstance ( theType ) ;
89- }
90-
91- private static void GenerateThunk ( TypeBuilder tb , MethodInfo method )
92- {
93- ParameterInfo [ ] pi = method . GetParameters ( ) ;
94- int count = pi . Length ;
95- int argc = count - 1 ;
96-
97- var args = new Type [ count ] ;
98- for ( var i = 0 ; i < count ; i ++ )
99- {
100- args [ i ] = pi [ i ] . ParameterType ;
101- }
102-
103- MethodBuilder mb = tb . DefineMethod (
104- method . Name ,
105- MethodAttributes . Public |
106- MethodAttributes . Virtual ,
107- method . ReturnType ,
108- args
109- ) ;
110-
111- // Build the method signature for the actual native function.
112- // This is essentially the signature of the wrapper method
113- // minus the first argument (the passed in function pointer).
114-
115- var nargs = new Type [ argc ] ;
116- for ( var i = 1 ; i < count ; i ++ )
117- {
118- nargs [ i - 1 ] = args [ i ] ;
119- }
120-
121- // IL generation: the (implicit) first argument of the method
122- // is the 'this' pointer and the second is the function pointer.
123- // This code pushes the real args onto the stack, followed by
124- // the function pointer, then the calli opcode to make the call.
125-
126- ILGenerator il = mb . GetILGenerator ( ) ;
127-
128- for ( var i = 0 ; i < argc ; i ++ )
129- {
130- il . Emit ( OpCodes . Ldarg_S , i + 2 ) ;
131- }
132-
133- il . Emit ( OpCodes . Ldarg_1 ) ;
134-
135- il . EmitCalli ( OpCodes . Calli ,
136- CallingConvention . Cdecl ,
137- method . ReturnType ,
138- nargs
139- ) ;
140-
141- il . Emit ( OpCodes . Ret ) ;
142-
143- tb . DefineMethodOverride ( mb , method ) ;
144- }
145-
146-
147- public static void Void_Call_1 ( IntPtr fp , IntPtr a1 )
148- {
149- Impl . Void_Call_1 ( fp , a1 ) ;
150- }
151-
152- public static IntPtr Call_3 ( IntPtr fp , IntPtr a1 , IntPtr a2 , IntPtr a3 )
153- {
154- return Impl . Call_3 ( fp , a1 , a2 , a3 ) ;
155- }
156-
157- public static int Int_Call_3 ( IntPtr fp , IntPtr a1 , IntPtr a2 , IntPtr a3 )
158- {
159- return Impl . Int_Call_3 ( fp , a1 , a2 , a3 ) ;
160- }
161- #endif
162- }
163-
164- #if ! NETSTANDARD
165- /// <summary>
166- /// Defines native call signatures to be generated by NativeCall.
167- /// </summary>
168- public interface INativeCall
169- {
170- void Void_Call_0 ( IntPtr funcPtr ) ;
171-
172- void Void_Call_1 ( IntPtr funcPtr , IntPtr arg1 ) ;
173-
174- int Int_Call_3 ( IntPtr funcPtr , IntPtr t , IntPtr n , IntPtr v ) ;
175-
176- IntPtr Call_3 ( IntPtr funcPtr , IntPtr a1 , IntPtr a2 , IntPtr a3 ) ;
17750 }
178- #endif
17951}
0 commit comments