private static IntPtr AddCustomMetaMethod(string name, IntPtr type, IntPtr mdef, SlotsHolder slotsHolder) { MethodInfo mi = typeof(MetaType).GetMethod(name); ThunkInfo thunkInfo = Interop.GetThunk(mi, "BinaryFunc"); slotsHolder.KeeapAlive(thunkInfo); // XXX: Hard code with mode check. if (Runtime.ShutdownMode != ShutdownMode.Reload) { IntPtr mdefAddr = mdef; slotsHolder.AddDealloctor(() => { var tp_dict = new BorrowedReference(Marshal.ReadIntPtr(type, TypeOffset.tp_dict)); if (Runtime.PyDict_DelItemString(tp_dict, name) != 0) { Runtime.PyErr_Print(); Debug.Fail($"Cannot remove {name} from metatype"); } FreeMethodDef(mdefAddr); }); } mdef = WriteMethodDef(mdef, name, thunkInfo.Address); return(mdef); }
internal static IntPtr RestoreRuntimeData(RuntimeDataStorage storage) { PyCLRMetaType = storage.PopValue <IntPtr>(); _metaSlotsHodler = new SlotsHolder(PyCLRMetaType); TypeManager.InitializeSlots(PyCLRMetaType, typeof(MetaType), _metaSlotsHodler); IntPtr mdef = Marshal.ReadIntPtr(PyCLRMetaType, TypeOffset.tp_methods); foreach (var methodName in CustomMethods) { var mi = typeof(MetaType).GetMethod(methodName); ThunkInfo thunkInfo = Interop.GetThunk(mi, "BinaryFunc"); _metaSlotsHodler.KeeapAlive(thunkInfo); mdef = TypeManager.WriteMethodDef(mdef, methodName, thunkInfo.Address); } return(PyCLRMetaType); }