Esempio n. 1
0
 private static ICollection <OverloadDoc> GetBuiltinFunctionOverloads(BuiltinFunction bf)
 {
     OverloadDoc[] res = new OverloadDoc[bf.Targets.Count];
     for (int i = 0; i < bf.Targets.Count; i++)
     {
         res[i] = GetOverloadDoc(bf.__name__, bf.Targets[i]);
     }
     return(res);
 }
Esempio n. 2
0
        public void SetDeclaredMethod(object m)
        {
            this.func              = m;
            this.funcAsFunc        = m as BuiltinFunction;
            this.isObjectMethod    = pythonType.type == typeof(object);
            this.isBuiltinMethod   = pythonType is ReflectedType;
            this.isSuperTypeMethod = false;

            //!!! the dictionary should be bound to this in a more sophisticated way
            pythonType.dict[this.name] = m;
        }
Esempio n. 3
0
        public object GetAttribute(object instance, object owner)
        {
            if (instance != null)
            {
                CheckSelf(instance);

                BuiltinFunction res = template.Clone() as BuiltinFunction;
                res.inst = instance;
                return(res);
            }
            return(this);
        }
Esempio n. 4
0
        private static BuiltinFunction GetNextFunctionTemplate()
        {
            BuiltinMethodDescriptor bimd = (BuiltinMethodDescriptor)TypeCache.Generator.GetAttr(
                DefaultContext.Default, null, SymbolTable.GeneratorNext);
            BuiltinFunction unOpt = bimd.template;

            BuiltinFunction res = Compiler.ReflectOptimizer.MakeFunction(unOpt);

            if (res == null)
            {
                return(unOpt);              // optimization is disabled...
            }
            return(res);
        }
Esempio n. 5
0
        public bool DeleteAttribute(object instance)
        {
            if (isSuperTypeMethod)
            {
                throw new NotImplementedException();
            }

            func              = null;
            funcAsFunc        = null;
            isSuperTypeMethod = true;
            UpdateFromBases(pythonType.MethodResolutionOrder);
            pythonType.UpdateSubclasses();
            return(true);
        }
Esempio n. 6
0
        public virtual string /*!*/ __repr__(CodeContext /*!*/ context)
        {
            BuiltinFunction bf = _func as BuiltinFunction;

            if (bf != null)
            {
                return(String.Format("<method {0} of {1} objects>",
                                     PythonOps.Repr(context, bf.Name),
                                     PythonOps.Repr(context, DynamicHelpers.GetPythonTypeFromType(bf.DeclaringType).Name)));
            }

            return(String.Format("<classmethod object at {0}>",
                                 IdDispenser.GetId(this)));
        }
Esempio n. 7
0
 public bool TryGetAttr(ICallerContext context, SymbolId name, out object value)
 {
     if (name == SymbolTable.GeneratorNext)
     {
         // next is the most common call on generators, we optimize that call here.  We get
         // two benefits out of this:
         //      1. Avoid the dictionary lookup for next
         //      2. Avoid the self-check in the method descriptor (because we know we're binding to a generator)
         BuiltinFunction res = nextFunction.Clone();
         res.inst = this;
         value    = res;
         return(true);
     }
     return(generatorType.TryGetAttr(context, this, name, out value));
 }
Esempio n. 8
0
        public static MethodWrapper MakeForObject(PythonType pt, SymbolId name, Delegate func)
        {
            MethodWrapper ret = new MethodWrapper(pt, name);

            ret.isObjectMethod    = true;
            ret.isBuiltinMethod   = true;
            ret.isSuperTypeMethod = false;

            ret.func       = BuiltinFunction.Make((string)SymbolTable.IdToString(name), func, new MethodBase[] { func.Method });
            ret.funcAsFunc = ret.func as BuiltinFunction;

            //pt.dict[name] = ret;

            return(ret);
        }
Esempio n. 9
0
        public override ICollection <OverloadDoc> GetOverloads(object value)
        {
            BuiltinFunction bf = value as BuiltinFunction;

            if (bf != null)
            {
                return(GetBuiltinFunctionOverloads(bf));
            }

            BuiltinMethodDescriptor bmd = value as BuiltinMethodDescriptor;

            if (bmd != null)
            {
                return(GetBuiltinFunctionOverloads(bmd.Template));
            }

            PythonFunction pf = value as PythonFunction;

            if (pf != null)
            {
                return(new[] {
                    new OverloadDoc(
                        pf.__name__,
                        pf.__doc__ as string,
                        GetParameterDocs(pf)
                        )
                });
            }

            Method method = value as Method;

            if (method != null)
            {
                return(GetOverloads(method.__func__));
            }

            Delegate dlg = value as Delegate;

            if (dlg != null)
            {
                return(new[] { DocBuilder.GetOverloadDoc(dlg.GetType().GetMethod("Invoke"), dlg.GetType().Name, 0, false) });
            }

            return(new OverloadDoc[0]);
        }
Esempio n. 10
0
        /// <summary>
        /// Returns a new BuiltinFunction that includes the
        /// optimized method that was requested to be added.
        /// </summary>
        /// <param name="adding"></param>
        internal BuiltinFunction ReOptimize(MethodInfo adding)
        {
            MethodInfo [] infos = new MethodInfo[targets.Length + 1];
            infos[0] = adding;
            Array.Copy(targets, 0, infos, 1, targets.Length);
            targets = infos;
            BuiltinFunction res = Compiler.ReflectOptimizer.MakeFunction(this);

            if (res != null)
            {
                res.FunctionType = FunctionType | FunctionType.SkipThisCheck;

                return(res);
            }

            // we now include our base type, we can't survie a type check.
            FunctionType |= FunctionType.SkipThisCheck;
            return(null);
        }
Esempio n. 11
0
        public override bool Equals(object obj)
        {
            BuiltinFunction bf = obj as BuiltinFunction;

            if (bf == null)
            {
                return(false);
            }

            if (bf.targets.Length != targets.Length)
            {
                return(false);
            }

            for (int i = 0; i < bf.targets.Length; i++)
            {
                if (bf.targets[i] != targets[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 12
0
        protected UserType(string name, Tuple bases, IDictionary <object, object> dict)
            : base(NewTypeMaker.GetNewType(name, bases, dict))
        {
            List <MethodInfo> ctors = new List <MethodInfo>();

            foreach (MethodInfo mi in type.GetMethods())
            {
                if (mi.Name == ReflectedType.MakeNewName)
                {
                    ctors.Add(mi);
                }
            }

            if (ctors.Count == 0)
            {
                throw new NotImplementedException("no MakeNew found");
            }

            ctor = BuiltinFunction.Make(name, ctors.ToArray(), ctors.ToArray(), FunctionType.Function);

            IAttributesDictionary fastDict = (IAttributesDictionary)dict;

            this.__name__   = name;
            this.__module__ = fastDict[SymbolTable.Module];   // should always be present...

            if (!fastDict.ContainsKey(SymbolTable.Doc))
            {
                fastDict[SymbolTable.Doc] = null;
            }

            InitializeUserType(bases, false);

            this.dict = CreateNamespaceDictionary(dict);

            AddProtocolWrappers();
        }
Esempio n. 13
0
        protected virtual void OptimizeMethod()
        {
            BuiltinFunction optimized = ReflectOptimizer.MakeFunction(this);

            if (optimized != null)
            {
                Debug.Assert(optimized.Targets.Length == Targets.Length);

                ReflectedType declType = (ReflectedType)DeclaringType;
                declType.Initialize();

                object   prevVal;
                SymbolId myId = SymbolTable.StringToId(Name);
                if (declType.dict.TryGetValue(myId, out prevVal))
                {
                    ClassMethod cm;
                    if ((cm = prevVal as ClassMethod) != null)
                    {
                        cm.func = optimized;
                    }
                    else
                    {
                        if (myId == SymbolTable.NewInst)
                        {
                            declType.dict[myId] = declType.ctor = optimized;
                        }
                        else
                        {
                            declType.dict[myId] = optimized.GetDescriptor();
                        }
                    }

                    optimizedTarget = optimized;
                }
            }
        }
Esempio n. 14
0
 public BuiltinFunctionOverloadMapper(BuiltinFunction builtinFunction)
 {
     this.function = builtinFunction;
 }
Esempio n. 15
0
 internal ClassMethodDescriptor(BuiltinFunction func)
 {
     this._func = func;
 }
Esempio n. 16
0
        private bool TryGetLazyValue(string name, bool publish, out object value)
        {
            if (!_cleared)
            {
                MemberInfo[] members = NonHiddenMembers(GetMember(name));
                if (members.Length > 0)
                {
                    // we only support fields, methods, and nested types in modules.
                    switch (members[0].MemberType)
                    {
                    case MemberTypes.Field:
                        Debug.Assert(members.Length == 1);

                        var fieldInfo = (FieldInfo)members[0];
                        if (fieldInfo.IsStatic)
                        {
                            value = ((FieldInfo)members[0]).GetValue(null);
                        }
                        else
                        {
                            throw new InvalidOperationException("instance field declared on module.  Fields should stored as PythonGlobals, should be static readonly, or marked as PythonHidden.");
                        }

                        if (publish)
                        {
                            LazyAdd(name, value);
                        }
                        return(true);

                    case MemberTypes.Method:
                        if (!((MethodInfo)members[0]).IsSpecialName)
                        {
                            var          methods = new MethodInfo[members.Length];
                            FunctionType ft      = FunctionType.ModuleMethod | FunctionType.AlwaysVisible;
                            for (int i = 0; i < members.Length; i++)
                            {
                                var method = (MethodInfo)members[i];
                                if (method.IsStatic)
                                {
                                    ft |= FunctionType.Function;
                                }
                                else
                                {
                                    ft |= FunctionType.Method;
                                }

                                methods[i] = method;
                            }

                            var builtinFunc = BuiltinFunction.MakeMethod(
                                name,
                                methods,
                                members[0].DeclaringType,
                                ft
                                );

                            if ((ft & FunctionType.Method) != 0 && Instance != null)
                            {
                                value = builtinFunc.BindToInstance(Instance);
                            }
                            else
                            {
                                value = builtinFunc;
                            }

                            if (publish)
                            {
                                LazyAdd(name, value);
                            }
                            return(true);
                        }
                        break;

                    case MemberTypes.Property:
                        Debug.Assert(members.Length == 1);

                        var propInfo = (PropertyInfo)members[0];
                        if ((propInfo.GetGetMethod() ?? propInfo.GetSetMethod()).IsStatic)
                        {
                            value = ((PropertyInfo)members[0]).GetValue(null, ArrayUtils.EmptyObjects);
                        }
                        else
                        {
                            throw new InvalidOperationException("instance property declared on module.  Propreties should be declared as static, marked as PythonHidden, or you should use a PythonGlobal.");
                        }

                        if (publish)
                        {
                            LazyAdd(name, value);
                        }

                        return(true);

                    case MemberTypes.NestedType:
                        if (members.Length == 1)
                        {
                            value = DynamicHelpers.GetPythonTypeFromType((Type)members[0]);
                        }
                        else
                        {
                            TypeTracker tt = (TypeTracker)MemberTracker.FromMemberInfo(members[0]);
                            for (int i = 1; i < members.Length; i++)
                            {
                                tt = TypeGroup.UpdateTypeEntity(tt, (TypeTracker)MemberTracker.FromMemberInfo(members[i]));
                            }

                            value = tt;
                        }

                        if (publish)
                        {
                            LazyAdd(name, value);
                        }
                        return(true);
                    }
                }
            }

            value = null;
            return(false);
        }
Esempio n. 17
0
        private bool TryGetLazyValue(string name, bool publish, out object value)
        {
            if (!_cleared)
            {
                MemberInfo[] members = NonHiddenMembers(GetMember(name));
                if (members.Length > 0)
                {
                    // we only support fields, methods, and nested types in modules.
                    switch (members[0].MemberType)
                    {
                    case MemberTypes.Field:
                        Debug.Assert(members.Length == 1);

                        value = ((FieldInfo)members[0]).GetValue(null);
                        if (publish)
                        {
                            LazyAdd(name, value);
                        }
                        return(true);

                    case MemberTypes.Method:
                        if (!((MethodInfo)members[0]).IsSpecialName)
                        {
                            value = BuiltinFunction.MakeFunction(
                                name,
                                ArrayUtils.ConvertAll <MemberInfo, MethodInfo>(members, delegate(MemberInfo mi) { return((MethodInfo)mi); }),
                                members[0].DeclaringType
                                );

                            if (publish)
                            {
                                LazyAdd(name, value);
                            }
                            return(true);
                        }
                        break;

                    case MemberTypes.Property:
                        Debug.Assert(members.Length == 1);

                        value = ((PropertyInfo)members[0]).GetValue(null, ArrayUtils.EmptyObjects);

                        if (publish)
                        {
                            LazyAdd(name, value);
                        }

                        return(true);

                    case MemberTypes.NestedType:
                        if (members.Length == 1)
                        {
                            value = DynamicHelpers.GetPythonTypeFromType((Type)members[0]);
                        }
                        else
                        {
                            TypeTracker tt = (TypeTracker)MemberTracker.FromMemberInfo(members[0]);
                            for (int i = 1; i < members.Length; i++)
                            {
                                tt = TypeGroup.UpdateTypeEntity(tt, (TypeTracker)MemberTracker.FromMemberInfo(members[i]));
                            }

                            value = tt;
                        }

                        if (publish)
                        {
                            LazyAdd(name, value);
                        }
                        return(true);
                    }
                }
            }

            value = null;
            return(false);
        }
Esempio n. 18
0
 internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value)
 {
     owner = CheckGetArgs(context, instance, owner);
     value = new BuiltinFunction(owner, _func._data);
     return(true);
 }
Esempio n. 19
0
 protected BuiltinFunction(BuiltinFunction from)
 {
     this.targets  = from.targets;
     this.name     = from.name;
     this.funcType = from.funcType;
 }
Esempio n. 20
0
 public BuiltinMethodDescriptor(BuiltinFunction function)
 {
     template = function;
 }