Ejemplo n.º 1
0
            private DynamicMetaObject /*!*/ GetFallbackError(DynamicMetaObjectBinder /*!*/ member)
            {
                if (member is PythonGetMemberBinder)
                {
                    // accessing from Python, produce our error
                    PythonGetMemberBinder pb = member as PythonGetMemberBinder;
                    if (pb.IsNoThrow)
                    {
                        return(new DynamicMetaObject(
                                   Expression.Constant(OperationFailed.Value),
                                   BindingRestrictions.GetInstanceRestriction(Expression, Value).Merge(Restrictions)
                                   ));
                    }
                    else
                    {
                        return(new DynamicMetaObject(
                                   member.Throw(
                                       Ast.Call(
                                           typeof(PythonOps).GetMethod(
                                               nameof(PythonOps.AttributeErrorForMissingAttribute),
                                               new Type[] { typeof(string), typeof(string) }
                                               ),
                                           AstUtils.Constant(DynamicHelpers.GetPythonType(Value).Name),
                                           AstUtils.Constant(pb.Name)
                                           ),
                                       typeof(object)
                                       ),
                                   BindingRestrictions.GetInstanceRestriction(Expression, Value).Merge(Restrictions)
                                   ));
                    }
                }

                // let the calling language bind the .NET members
                return(((GetMemberBinder)member).FallbackGetMember(_type));
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper for falling back - if we have a base object fallback to it first (which can
        /// then fallback to the calling site), otherwise fallback to the calling site.
        /// </summary>
        private DynamicMetaObject /*!*/ Fallback(DynamicMetaObjectBinder /*!*/ action, DynamicMetaObject codeContext)
        {
            if (_baseMetaObject != null)
            {
                IPythonGetable ipyget = _baseMetaObject as IPythonGetable;
                if (ipyget != null)
                {
                    PythonGetMemberBinder gmb = action as PythonGetMemberBinder;
                    if (gmb != null)
                    {
                        return(ipyget.GetMember(gmb, codeContext));
                    }
                }

                GetMemberBinder gma = action as GetMemberBinder;
                if (gma != null)
                {
                    return(_baseMetaObject.BindGetMember(gma));
                }

                return(_baseMetaObject.BindGetMember(
                           PythonContext.GetPythonContext(action).CompatGetMember(
                               GetGetMemberName(action),
                               false
                               )
                           ));
            }

            return(GetMemberFallback(this, action, codeContext));
        }
Ejemplo n.º 3
0
        private Expression FallbackGet(DynamicMetaObjectBinder member, DynamicMetaObject[] args)
        {
            if (member is GetMemberBinder sa)
            {
                return(sa.FallbackGetMember(args[0]).Expression);
            }

            PythonGetMemberBinder pyGetMem = member as PythonGetMemberBinder;

            if (pyGetMem.IsNoThrow)
            {
                return(Ast.Field(
                           null,
                           typeof(OperationFailed).GetDeclaredField("Value")
                           ));
            }
            else
            {
                return(member.Throw(
                           Ast.Call(
                               typeof(PythonOps).GetMethod(nameof(PythonOps.AttributeError)),
                               AstUtils.Constant("{0} instance has no attribute '{1}'"),
                               Ast.NewArrayInit(
                                   typeof(object),
                                   AstUtils.Constant(((OldInstance)Value)._class._name),
                                   AstUtils.Constant(pyGetMem.Name)
                                   )
                               )
                           ));
            }
        }
Ejemplo n.º 4
0
 public FastGetBinderHelper(PythonType type, CodeContext context, PythonGetMemberBinder binder)
     : base(type, context, binder.Name)
 {
     // capture these before we start producing the result
     _version     = type.Version;
     _metaVersion = DynamicHelpers.GetPythonType(type).Version;
     _binder      = binder;
 }
        public DynamicGetMemberExpression(PythonGetMemberBinder/*!*/ binder, CompilationMode/*!*/ mode, MSAst.Expression/*!*/ target, MSAst.Expression codeContext) {
            Assert.NotNull(binder, mode, target, codeContext);

            _binder = binder;
            _mode = mode;
            _target = target;
            _codeContext = codeContext;
        }
Ejemplo n.º 6
0
        public override DynamicMetaObject FallbackGetMember(DynamicMetaObject self, DynamicMetaObject errorSuggestion)
        {
#if FEATURE_COM
            DynamicMetaObject com;
            if (Microsoft.Scripting.ComInterop.ComBinder.TryBindGetMember(this, self, out com, true))
            {
                return(com);
            }
#endif
            return(PythonGetMemberBinder.FallbackWorker(_context, self, PythonContext.GetCodeContextMOCls(this), Name, _isNoThrow ? GetMemberOptions.IsNoThrow : GetMemberOptions.None, this, errorSuggestion));
        }
Ejemplo n.º 7
0
        internal static bool IsNoThrow(DynamicMetaObjectBinder action)
        {
            PythonGetMemberBinder gmb = action as PythonGetMemberBinder;

            if (gmb != null)
            {
                return(gmb.IsNoThrow);
            }

            return(false);
        }
Ejemplo n.º 8
0
        protected static DynamicMetaObject GetMemberFallback(DynamicMetaObject self, DynamicMetaObjectBinder member, DynamicMetaObject codeContext)
        {
            PythonGetMemberBinder gmb = member as PythonGetMemberBinder;

            if (gmb != null)
            {
                return(gmb.Fallback(self, codeContext));
            }

            GetMemberBinder gma = (GetMemberBinder)member;

            return(gma.FallbackGetMember(self.Restrict(self.GetLimitType())));
        }
Ejemplo n.º 9
0
        public override bool Equals(object obj)
        {
            PythonGetMemberBinder ob = obj as PythonGetMemberBinder;

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

            return(ob._context.Binder == _context.Binder &&
                   ob._options == _options &&
                   ob._name == _name);
        }
Ejemplo n.º 10
0
        protected static string GetGetMemberName(DynamicMetaObjectBinder member)
        {
            PythonGetMemberBinder gmb = member as PythonGetMemberBinder;

            if (gmb != null)
            {
                return(gmb.Name);
            }

            InvokeMemberBinder invoke = member as InvokeMemberBinder;

            if (invoke != null)
            {
                return(invoke.Name);
            }

            GetMemberBinder gma = (GetMemberBinder)member;

            return(gma.Name);
        }
Ejemplo n.º 11
0
        internal PythonGetMemberBinder/*!*/ GetMember(string/*!*/ name, bool isNoThrow) {
            Dictionary<string, PythonGetMemberBinder> dict;
            if (isNoThrow) {
                if (_tryGetMemberBinders == null) {
                    Interlocked.CompareExchange(
                        ref _tryGetMemberBinders,
                        new Dictionary<string, PythonGetMemberBinder>(),
                        null
                    );
                }

                dict = _tryGetMemberBinders;
            } else {
                if (_getMemberBinders == null) {
                    Interlocked.CompareExchange(
                        ref _getMemberBinders,
                        new Dictionary<string, PythonGetMemberBinder>(),
                        null
                    );
                }

                dict = _getMemberBinders;
            }

            lock (dict) {
                PythonGetMemberBinder res;
                if (!dict.TryGetValue(name, out res)) {
                    dict[name] = res = new PythonGetMemberBinder(this, name, isNoThrow);
                }

                return res;
            }
        }
Ejemplo n.º 12
0
 public DynamicMetaObject GetMember(PythonGetMemberBinder member, DynamicMetaObject codeContext) {
     // no codeContext filtering but avoid an extra site by handling this action directly
     return MakeMemberAccess(member, member.Name, MemberAccess.Get, this);
 }
Ejemplo n.º 13
0
 public DynamicMetaObject GetMember(PythonGetMemberBinder member, DynamicMetaObject codeContext) {
     return BindGetMemberWorker(member, member.Name, codeContext);
 }
Ejemplo n.º 14
0
 public DynamicMetaObject GetMember(PythonGetMemberBinder member, DynamicMetaObject codeContext) {
     // no codeContext filtering but avoid an extra site by handling this action directly
     return MakeGetMember(member, codeContext);
 }
Ejemplo n.º 15
0
 public DynamicMetaObject GetMember(PythonGetMemberBinder member, DynamicMetaObject codeContext)
 {
     // no codeContext filtering but avoid an extra site by handling this action directly
     return(MakeMemberAccess(member, member.Name, MemberAccess.Get, this));
 }
Ejemplo n.º 16
0
 public GetMemberInstruction(PythonGetMemberBinder binder) {
     _binder = binder;
 }
Ejemplo n.º 17
0
        public FastGetBase(PythonGetMemberBinder/*!*/ binder) {
            Assert.NotNull(binder);

            _binder = binder;
        }
Ejemplo n.º 18
0
 public DynamicMetaObject /*!*/ GetMember(PythonGetMemberBinder /*!*/ member, DynamicMetaObject /*!*/ codeContext)
 {
     return(GetMemberWorker(member, codeContext.Expression));
 }
Ejemplo n.º 19
0
 public DynamicMetaObject GetMember(PythonGetMemberBinder/*!*/ member, Expression/*!*/ codeContext) {
     return GetMemberWorker(member, codeContext);
 }