public static object MakeNew(PythonType cls, object @object) { IWeakReferenceable iwr = @object as IWeakReferenceable; if (iwr == null) { throw Ops.TypeError("cannot create weak reference to '{0}' object", Ops.GetDynamicType(@object).__name__); } if (cls == Ops.GetDynamicTypeFromType(typeof(PythonWeakReference))) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { for (int i = 0; i < wrt.HandlerCount; i++) { if (wrt.GetHandlerCallback(i) == null && wrt.GetWeakRef(i) is PythonWeakReference) { return(wrt.GetWeakRef(i)); } } } return(new PythonWeakReference(@object)); } else { return(cls.ctor.Call(cls, @object)); } }
public static object __new__(CodeContext context, PythonType cls, object @object) { IWeakReferenceable iwr = ConvertToWeakReferenceable(context.LanguageContext, @object); if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(@ref))) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { for (int i = 0; i < wrt.HandlerCount; i++) { if (wrt.GetHandlerCallback(i) == null && wrt.GetWeakRef(i) is @ref) { return(wrt.GetWeakRef(i)); } } } return(new @ref(context, @object)); } else { return(cls.CreateInstance(context, @object)); } }
internal static object MakeNew(object @object, object callback) { IWeakReferenceable iwr = @object as IWeakReferenceable; if (iwr == null) { throw Ops.TypeError("cannot create weak reference to '{0}' object", Ops.GetDynamicType(@object).__name__); } if (callback == null) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { for (int i = 0; i < wrt.HandlerCount; i++) { if (wrt.GetHandlerCallback(i) == null && wrt.GetWeakRef(i) is PythonCallableWeakRefProxy) { return(wrt.GetWeakRef(i)); } } } } return(new PythonCallableWeakRefProxy(@object, callback)); }
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) { if (instance == null) { value = this; return(true); } IWeakReferenceable reference = instance as IWeakReferenceable; if (reference != null) { WeakRefTracker tracker = reference.GetWeakRef(); if (tracker == null || tracker.HandlerCount == 0) { value = null; } else { value = tracker.GetHandlerCallback(0); } return(true); } value = null; return(false); }
internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) { IWeakReferenceable reference = instance as IWeakReferenceable; if (reference != null) { return(reference.SetWeakRef(new WeakRefTracker(value, instance))); } return(false); }
internal static IWeakReferenceable ConvertToWeakReferenceable(object obj) { IWeakReferenceable iwr = obj as IWeakReferenceable; if (iwr != null) { return(iwr); } throw PythonOps.TypeError("cannot create weak reference to '{0}' object", PythonOps.GetPythonTypeName(obj)); }
private static void AddFinalizer(CodeContext /*!*/ context, PythonType dt, object newObject) { // check if object has finalizer... if (dt.TryResolveSlot(context, "__del__", out _)) { IWeakReferenceable iwr = context.LanguageContext.ConvertToWeakReferenceable(newObject); Debug.Assert(iwr != null); InstanceFinalizer nif = new InstanceFinalizer(context, newObject); iwr.SetFinalizer(new WeakRefTracker(iwr, nif, nif)); } }
internal static int GetWeakRefCount(object o) { IWeakReferenceable iwr = o as IWeakReferenceable; if (iwr != null) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { return(wrt.HandlerCount); } } return(0); }
public static WeakRefTracker InitializeWeakRef(PythonContext context, object self, object target, object callback) { IWeakReferenceable iwr = ConvertToWeakReferenceable(context, target); WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt == null) { if (!iwr.SetWeakRef(wrt = new WeakRefTracker(iwr))) { throw PythonOps.TypeError("cannot create weak reference to '{0}' object", PythonOps.GetPythonTypeName(target)); } } wrt.ChainCallback(callback, self); return(wrt); }
internal static List GetWeakRefs(object o) { List l = new List(); IWeakReferenceable iwr = o as IWeakReferenceable; if (iwr != null) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { for (int i = 0; i < wrt.HandlerCount; i++) { l.AddNoLock(wrt.GetWeakRef(i)); } } } return(l); }
public static void InitializeWeakRef(object self, object target, object callback) { IWeakReferenceable iwr = target as IWeakReferenceable; if (iwr == null) { throw Ops.TypeError("cannot create weak reference to '{0}' object", Ops.GetDynamicType(target).__name__); } WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt == null) { iwr.SetWeakRef(new WeakRefTracker(callback, self)); } else { wrt.ChainCallback(callback, self); } }
internal static object MakeNew(CodeContext /*!*/ context, object @object, object callback) { IWeakReferenceable iwr = ConvertToWeakReferenceable(context.LanguageContext, @object); if (callback == null) { WeakRefTracker wrt = iwr.GetWeakRef(); if (wrt != null) { for (int i = 0; i < wrt.HandlerCount; i++) { if (wrt.GetHandlerCallback(i) == null && wrt.GetWeakRef(i) is weakproxy) { return(wrt.GetWeakRef(i)); } } } } return(new weakproxy(context, @object, callback)); }
public WeakRefTracker(IWeakReferenceable target, object callback, object weakRef) : this(target) { ChainCallback(callback, weakRef); }
public WeakRefTracker(IWeakReferenceable target) { _targetId = IdDispenser.GetId(target); }
public WeakRefTracker(IWeakReferenceable target) { }