Пример #1
0
            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));
                }
            }
Пример #2
0
            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));
                }
            }
Пример #3
0
            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));
            }
Пример #4
0
        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);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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));
        }
Пример #7
0
        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));
            }
        }
Пример #8
0
            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);
            }
Пример #9
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);
            }
Пример #10
0
            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);
            }
Пример #11
0
            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);
                }
            }
Пример #12
0
            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));
            }
Пример #13
0
 public WeakRefTracker(IWeakReferenceable target, object callback, object weakRef) : this(target) {
     ChainCallback(callback, weakRef);
 }
Пример #14
0
 public WeakRefTracker(IWeakReferenceable target)
 {
     _targetId = IdDispenser.GetId(target);
 }
Пример #15
0
 public WeakRefTracker(IWeakReferenceable target)
 {
 }