|
| 1 | +// ========================================================================== |
| 2 | +// This software is subject to the provisions of the Zope Public License, |
| 3 | +// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. |
| 4 | +// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
| 5 | +// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 6 | +// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
| 7 | +// FOR A PARTICULAR PURPOSE. |
| 8 | +// ========================================================================== |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Reflection; |
| 12 | +using System.Reflection.Emit; |
| 13 | +using System.Collections.Generic; |
| 14 | +using System.Threading; |
| 15 | +using System.Linq; |
| 16 | + |
| 17 | +namespace Python.Runtime |
| 18 | +{ |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Managed class that provides the implementation for reflected types. |
| 22 | + /// Managed classes and value types are represented in Python by actual |
| 23 | + /// Python type objects. Each of those type objects is associated with |
| 24 | + /// an instance of ClassObject, which provides its implementation. |
| 25 | + /// </summary> |
| 26 | + |
| 27 | + internal class ClassDerivedObject : ClassObject |
| 28 | + { |
| 29 | + static private Dictionary<string, AssemblyBuilder> assemblyBuilders; |
| 30 | + static private Dictionary<Tuple<string, string>, ModuleBuilder> moduleBuilders; |
| 31 | + |
| 32 | + static ClassDerivedObject() |
| 33 | + { |
| 34 | + assemblyBuilders = new Dictionary<string, AssemblyBuilder>(); |
| 35 | + moduleBuilders = new Dictionary<Tuple<string, string>, ModuleBuilder>(); |
| 36 | + } |
| 37 | + |
| 38 | + internal ClassDerivedObject(Type tp) |
| 39 | + : base(tp) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + //==================================================================== |
| 44 | + // Implements __new__ for derived classes of reflected classes. |
| 45 | + //==================================================================== |
| 46 | + new public static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw) |
| 47 | + { |
| 48 | + // derived classes have a __pyobj__ field that points back to the python object |
| 49 | + // (see Trampoline.InvokeMethod and CreateDerivedType) |
| 50 | + IntPtr pyobj = ClassObject.tp_new(tp, args, kw); |
| 51 | + CLRObject obj = (CLRObject)ManagedType.GetManagedObject(pyobj); |
| 52 | + FieldInfo fi = obj.inst.GetType().GetField("__pyobj__"); |
| 53 | + fi.SetValue(obj.inst, pyobj); |
| 54 | + return pyobj; |
| 55 | + } |
| 56 | + |
| 57 | + //==================================================================== |
| 58 | + // Creates a new managed type derived from a base type with any virtual |
| 59 | + // methods overriden to call out to python if the associated python |
| 60 | + // object has overriden the method. |
| 61 | + //==================================================================== |
| 62 | + internal static Type CreateDerivedType(string name, |
| 63 | + Type baseType, |
| 64 | + string namespaceStr, |
| 65 | + string assemblyName, |
| 66 | + string moduleName="Python.Runtime.Dynamic.dll") |
| 67 | + { |
| 68 | + if (null != namespaceStr) |
| 69 | + name = namespaceStr + "." + name; |
| 70 | + |
| 71 | + if (null == assemblyName) |
| 72 | + assemblyName = Assembly.GetExecutingAssembly().FullName; |
| 73 | + |
| 74 | + ModuleBuilder moduleBuilder = GetModuleBuilder(assemblyName, moduleName); |
| 75 | + TypeBuilder typeBuilder = moduleBuilder.DefineType(name, TypeAttributes.Public | TypeAttributes.Class); |
| 76 | + typeBuilder.SetParent(baseType); |
| 77 | + |
| 78 | + // add a field for storing the python object pointer |
| 79 | + FieldBuilder fb = typeBuilder.DefineField("__pyobj__", typeof(IntPtr), FieldAttributes.Public); |
| 80 | + |
| 81 | + // override any virtual methods |
| 82 | + MethodInfo[] methods = baseType.GetMethods(); |
| 83 | + List<string> baseMethodNames = new List<string>(); |
| 84 | + foreach (MethodInfo method in methods) |
| 85 | + { |
| 86 | + if (!method.Attributes.HasFlag(MethodAttributes.Virtual) | method.Attributes.HasFlag(MethodAttributes.Final)) |
| 87 | + continue; |
| 88 | + |
| 89 | + ParameterInfo[] parameters = method.GetParameters(); |
| 90 | + Type[] parameterTypes = (from param in parameters select param.ParameterType).ToArray(); |
| 91 | + |
| 92 | + // create a method for calling the original method |
| 93 | + string baseMethodName = "_" + baseType.Name + "__" + method.Name; |
| 94 | + baseMethodNames.Add(baseMethodName); |
| 95 | + MethodBuilder mb = typeBuilder.DefineMethod(baseMethodName, |
| 96 | + MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.HideBySig, |
| 97 | + method.ReturnType, |
| 98 | + parameterTypes); |
| 99 | + |
| 100 | + // emit the assembly for calling the original method using call instead of callvirt |
| 101 | + ILGenerator il = mb.GetILGenerator(); |
| 102 | + il.Emit(OpCodes.Ldarg_0); |
| 103 | + for (int i = 0; i < parameters.Length; ++i) |
| 104 | + il.Emit(OpCodes.Ldarg, i + 1); |
| 105 | + il.Emit(OpCodes.Call, method); |
| 106 | + il.Emit(OpCodes.Ret); |
| 107 | + |
| 108 | + // override the original method with a new one that dispatches to python |
| 109 | + mb = typeBuilder.DefineMethod(method.Name, |
| 110 | + MethodAttributes.Public | MethodAttributes.ReuseSlot | |
| 111 | + MethodAttributes.Virtual | MethodAttributes.HideBySig, |
| 112 | + method.CallingConvention, |
| 113 | + method.ReturnType, |
| 114 | + parameterTypes); |
| 115 | + |
| 116 | + il = mb.GetILGenerator(); |
| 117 | + il.DeclareLocal(typeof(Object[])); |
| 118 | + il.Emit(OpCodes.Ldarg_0); |
| 119 | + il.Emit(OpCodes.Ldstr, method.Name); |
| 120 | + il.Emit(OpCodes.Ldstr, baseMethodName); |
| 121 | + il.Emit(OpCodes.Ldc_I4, parameters.Length); |
| 122 | + il.Emit(OpCodes.Newarr, typeof(System.Object)); |
| 123 | + il.Emit(OpCodes.Stloc_0); |
| 124 | + for (int i = 0; i < parameters.Length; ++i) |
| 125 | + { |
| 126 | + il.Emit(OpCodes.Ldloc_0); |
| 127 | + il.Emit(OpCodes.Ldc_I4, i); |
| 128 | + il.Emit(OpCodes.Ldarg, i + 1); |
| 129 | + if (parameterTypes[i].IsPrimitive) |
| 130 | + il.Emit(OpCodes.Box, parameterTypes[i]); |
| 131 | + il.Emit(OpCodes.Stelem, typeof(Object)); |
| 132 | + } |
| 133 | + il.Emit(OpCodes.Ldloc_0); |
| 134 | + if (method.ReturnType == typeof(void)) |
| 135 | + { |
| 136 | + il.Emit(OpCodes.Call, typeof(Trampoline).GetMethod("InvokeMethodVoid")); |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + il.Emit(OpCodes.Call, typeof(Trampoline).GetMethod("InvokeMethod").MakeGenericMethod(method.ReturnType)); |
| 141 | + } |
| 142 | + il.Emit(OpCodes.Ret); |
| 143 | + } |
| 144 | + |
| 145 | + Type type = typeBuilder.CreateType(); |
| 146 | + |
| 147 | + // scan the assembly so the newly added class can be imported |
| 148 | + Assembly assembly = Assembly.GetAssembly(type); |
| 149 | + AssemblyManager.ScanAssembly(assembly); |
| 150 | + |
| 151 | + return type; |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + private static ModuleBuilder GetModuleBuilder(string assemblyName, string moduleName) |
| 156 | + { |
| 157 | + // find or create a dynamic assembly and module |
| 158 | + AppDomain domain = AppDomain.CurrentDomain; |
| 159 | + ModuleBuilder moduleBuilder = null; |
| 160 | + |
| 161 | + if (moduleBuilders.ContainsKey(Tuple.Create(assemblyName, moduleName))) |
| 162 | + { |
| 163 | + moduleBuilder = moduleBuilders[Tuple.Create(assemblyName, moduleName)]; |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + AssemblyBuilder assemblyBuilder = null; |
| 168 | + if (assemblyBuilders.ContainsKey(assemblyName)) |
| 169 | + { |
| 170 | + assemblyBuilder = assemblyBuilders[assemblyName]; |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + assemblyBuilder = domain.DefineDynamicAssembly(new AssemblyName(assemblyName), |
| 175 | + AssemblyBuilderAccess.Run); |
| 176 | + assemblyBuilders[assemblyName] = assemblyBuilder; |
| 177 | + } |
| 178 | + |
| 179 | + moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName); |
| 180 | + moduleBuilders[Tuple.Create(assemblyName, moduleName)] = moduleBuilder; |
| 181 | + } |
| 182 | + |
| 183 | + return moduleBuilder; |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + // This has to be public as it's called from methods on dynamically built classes |
| 189 | + // potentially in other assemblies |
| 190 | + public class Trampoline |
| 191 | + { |
| 192 | + //==================================================================== |
| 193 | + // This is the implementaion of the overriden methods in the derived |
| 194 | + // type. It looks for a python method with the same name as the method |
| 195 | + // on the managed base class and if it exists and isn't the managed |
| 196 | + // method binding (ie it has been overriden in the derived python |
| 197 | + // class) it calls it, otherwise it calls the base method. |
| 198 | + //==================================================================== |
| 199 | + public static T InvokeMethod<T>(Object obj, string methodName, string origMethodName, Object[] args) |
| 200 | + { |
| 201 | + FieldInfo fi = obj.GetType().GetField("__pyobj__"); |
| 202 | + IntPtr ptr = (IntPtr)fi.GetValue(obj); |
| 203 | + if (null != ptr) |
| 204 | + { |
| 205 | + IntPtr gs = Runtime.PyGILState_Ensure(); |
| 206 | + try |
| 207 | + { |
| 208 | + PyObject pyobj = new PyObject(ptr); |
| 209 | + PyObject method = pyobj.GetAttr(methodName, new PyObject(Runtime.PyNone)); |
| 210 | + if (method.Handle != Runtime.PyNone) |
| 211 | + { |
| 212 | + // if the method hasn't been overriden then it will be a managed object |
| 213 | + ManagedType managedMethod = ManagedType.GetManagedObject(method.Handle); |
| 214 | + if (null == managedMethod) |
| 215 | + { |
| 216 | + PyObject[] pyargs = new PyObject[args.Length]; |
| 217 | + for (int i = 0; i < args.Length; ++i) |
| 218 | + { |
| 219 | + pyargs[i] = new PyObject(Converter.ToPython(args[i], args[i].GetType())); |
| 220 | + } |
| 221 | + |
| 222 | + PyObject py_result = method.Invoke(pyargs); |
| 223 | + return (T)py_result.AsManagedObject(typeof(T)); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + finally |
| 228 | + { |
| 229 | + Runtime.PyGILState_Release(gs); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return (T)obj.GetType().InvokeMember(origMethodName, |
| 234 | + BindingFlags.InvokeMethod, |
| 235 | + null, |
| 236 | + obj, |
| 237 | + args); |
| 238 | + } |
| 239 | + |
| 240 | + public static void InvokeMethodVoid(Object obj, string methodName, string origMethodName, Object[] args) |
| 241 | + { |
| 242 | + FieldInfo fi = obj.GetType().GetField("__pyobj__"); |
| 243 | + IntPtr ptr = (IntPtr)fi.GetValue(obj); |
| 244 | + if (null != ptr) |
| 245 | + { |
| 246 | + IntPtr gs = Runtime.PyGILState_Ensure(); |
| 247 | + try |
| 248 | + { |
| 249 | + PyObject pyobj = new PyObject(ptr); |
| 250 | + PyObject method = pyobj.GetAttr(methodName, new PyObject(Runtime.PyNone)); |
| 251 | + if (method.Handle != Runtime.PyNone) |
| 252 | + { |
| 253 | + // if the method hasn't been overriden then it will be a managed object |
| 254 | + ManagedType managedMethod = ManagedType.GetManagedObject(method.Handle); |
| 255 | + if (null == managedMethod) |
| 256 | + { |
| 257 | + PyObject[] pyargs = new PyObject[args.Length]; |
| 258 | + for (int i = 0; i < args.Length; ++i) |
| 259 | + { |
| 260 | + pyargs[i] = new PyObject(Converter.ToPython(args[i], args[i].GetType())); |
| 261 | + } |
| 262 | + |
| 263 | + PyObject py_result = method.Invoke(pyargs); |
| 264 | + return; |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + finally |
| 269 | + { |
| 270 | + Runtime.PyGILState_Release(gs); |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + obj.GetType().InvokeMember(origMethodName, |
| 275 | + BindingFlags.InvokeMethod, |
| 276 | + null, |
| 277 | + obj, |
| 278 | + args); |
| 279 | + } |
| 280 | + } |
| 281 | +} |
0 commit comments