Beispiel #1
0
 public static PSGetIndexBinder Get(int argCount, PSMethodInvocationConstraints constraints, bool allowSlicing = true)
 {
     lock (_binderCache)
     {
         PSGetIndexBinder binder;
         Tuple <CallInfo, PSMethodInvocationConstraints, bool> key = Tuple.Create <CallInfo, PSMethodInvocationConstraints, bool>(new CallInfo(argCount, new string[0]), constraints, allowSlicing);
         if (!_binderCache.TryGetValue(key, out binder))
         {
             binder = new PSGetIndexBinder(key);
             _binderCache.Add(key, binder);
         }
         return(binder);
     }
 }
Beispiel #2
0
 private DynamicMetaObject CheckForSlicing(DynamicMetaObject target, DynamicMetaObject[] indexes)
 {
     if (this._allowSlicing)
     {
         if (indexes.Length > 1)
         {
             PSGetIndexBinder nonSlicingBinder = Get(1, this._constraints, false);
             return(new DynamicMetaObject(Expression.NewArrayInit(typeof(object), (IEnumerable <Expression>)(from i in indexes select Expression.Dynamic(nonSlicingBinder, typeof(object), target.Expression, i.Expression))), target.CombineRestrictions(indexes)));
         }
         DynamicMetaObject obj2 = PSEnumerableBinder.IsEnumerable(indexes[0]);
         if (obj2 != null)
         {
             return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.EnumerableOps_SlicingIndex, target.Expression.Cast(typeof(object)), obj2.Expression.Cast(typeof(IEnumerator)), Expression.Constant(this.GetNonSlicingIndexer())), target.CombineRestrictions(new DynamicMetaObject[] { obj2 })));
         }
     }
     return(null);
 }
        public Expression GetValue(Compiler compiler, List <Expression> exprs, List <ParameterExpression> temps)
        {
            Expression right = compiler.Compile(this.IndexExpressionAst.Target);

            this._targetExprTemp = Expression.Variable(right.Type);
            temps.Add(this._targetExprTemp);
            exprs.Add(Expression.Assign(this._targetExprTemp, right));
            ExpressionAst   index = this.IndexExpressionAst.Index;
            ArrayLiteralAst ast2  = index as ArrayLiteralAst;
            PSMethodInvocationConstraints invocationConstraints = this.GetInvocationConstraints();

            if (ast2 != null)
            {
                return(Expression.Dynamic(PSGetIndexBinder.Get(ast2.Elements.Count, invocationConstraints, true), typeof(object), ast2.Elements.Select <ExpressionAst, Expression>(new Func <ExpressionAst, Expression>(compiler.Compile)).Prepend <Expression>(this._targetExprTemp)));
            }
            Expression expression3 = compiler.Compile(index);

            this._indexExprTemp = Expression.Variable(expression3.Type);
            temps.Add(this._indexExprTemp);
            exprs.Add(Expression.Assign(this._indexExprTemp, expression3));
            return(Expression.Dynamic(PSGetIndexBinder.Get(1, invocationConstraints, true), typeof(object), this._targetExprTemp, this._indexExprTemp));
        }
Beispiel #4
0
        private DynamicMetaObject SetIndexMultiDimensionArray(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            Type       elementType = target.LimitType.GetElementType();
            Expression expression  = PSGetIndexBinder.ConvertIndex(value, elementType);

            if (expression != null)
            {
                if ((int)indexes.Length != 1)
                {
                    Array arrays = (Array)target.Value;
                    if ((int)indexes.Length == arrays.Rank)
                    {
                        Expression[] expressionArray = new Expression[(int)indexes.Length];
                        int          num             = 0;
                        while (num < (int)indexes.Length)
                        {
                            expressionArray[num] = PSGetIndexBinder.ConvertIndex(indexes[num], typeof(int));
                            if (expressionArray[num] != null)
                            {
                                num++;
                            }
                            else
                            {
                                DynamicMetaObject[] dynamicMetaObjectArray = new DynamicMetaObject[1];
                                dynamicMetaObjectArray[0] = indexes[num];
                                return(PSConvertBinder.ThrowNoConversion(indexes[num], typeof(int), this, this._version, indexes.Except <DynamicMetaObject>(dynamicMetaObjectArray).Append <DynamicMetaObject>(target).Append <DynamicMetaObject>(value).ToArray <DynamicMetaObject>()));
                            }
                        }
                        return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.ArrayOps_SetMDArrayValue, target.Expression.Cast(typeof(Array)), Expression.NewArrayInit(typeof(int), expressionArray), expression.Cast(typeof(object))), target.CombineRestrictions(indexes).Merge(value.PSGetTypeRestriction())));
                    }
                    else
                    {
                        DynamicMetaObject dynamicMetaObject  = errorSuggestion;
                        DynamicMetaObject dynamicMetaObject1 = dynamicMetaObject;
                        if (dynamicMetaObject == null)
                        {
                            DynamicMetaObject   dynamicMetaObject2      = target;
                            DynamicMetaObject[] dynamicMetaObjectArray1 = indexes;
                            BindingRestrictions bindingRestriction      = value.PSGetTypeRestriction();
                            string       str = "NeedMultidimensionalIndex";
                            string       needMultidimensionalIndex = ParserStrings.NeedMultidimensionalIndex;
                            Expression[] expressionArray1          = new Expression[2];
                            expressionArray1[0] = ExpressionCache.Constant(arrays.Rank);
                            Expression[]        expressionArray2           = expressionArray1;
                            int                 num1                       = 1;
                            MethodInfo          arrayOpsIndexStringMessage = CachedReflectionInfo.ArrayOps_IndexStringMessage;
                            Type                type                       = typeof(object);
                            DynamicMetaObject[] dynamicMetaObjectArray2    = indexes;
                            expressionArray2[num1] = Expression.Call(arrayOpsIndexStringMessage, Expression.NewArrayInit(type, dynamicMetaObjectArray2.Select <DynamicMetaObject, Expression>((DynamicMetaObject i) => i.Expression.Cast(typeof(object)))));
                            dynamicMetaObject1     = dynamicMetaObject2.ThrowRuntimeError(dynamicMetaObjectArray1, bindingRestriction, str, needMultidimensionalIndex, expressionArray1);
                        }
                        return(dynamicMetaObject1);
                    }
                }
                else
                {
                    Expression expression1 = PSGetIndexBinder.ConvertIndex(indexes[0], typeof(int[]));
                    if (expression1 != null)
                    {
                        return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.ArrayOps_SetMDArrayValue, target.Expression.Cast(typeof(Array)), expression1, expression.Cast(typeof(object))), target.CombineRestrictions(indexes).Merge(value.PSGetTypeRestriction())));
                    }
                    else
                    {
                        DynamicMetaObject dynamicMetaObject3 = errorSuggestion;
                        DynamicMetaObject dynamicMetaObject4 = dynamicMetaObject3;
                        if (dynamicMetaObject3 == null)
                        {
                            DynamicMetaObject[] dynamicMetaObjectArray3 = new DynamicMetaObject[2];
                            dynamicMetaObjectArray3[0] = target;
                            dynamicMetaObjectArray3[1] = value;
                            dynamicMetaObject4         = PSConvertBinder.ThrowNoConversion(indexes[0], typeof(int[]), this, this._version, dynamicMetaObjectArray3);
                        }
                        return(dynamicMetaObject4);
                    }
                }
            }
            else
            {
                DynamicMetaObject dynamicMetaObject5 = errorSuggestion;
                DynamicMetaObject dynamicMetaObject6 = dynamicMetaObject5;
                if (dynamicMetaObject5 == null)
                {
                    dynamicMetaObject6 = PSConvertBinder.ThrowNoConversion(value, elementType, this, this._version, indexes.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>());
                }
                return(dynamicMetaObject6);
            }
        }
Beispiel #5
0
        private DynamicMetaObject SetIndexArray(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            Array arrays = (Array)target.Value;

            if (arrays.Rank <= 1)
            {
                if ((int)indexes.Length <= 1)
                {
                    Expression expression = PSGetIndexBinder.ConvertIndex(indexes[0], typeof(int));
                    if (expression != null)
                    {
                        Type       elementType = target.LimitType.GetElementType();
                        Expression expression1 = PSGetIndexBinder.ConvertIndex(value, elementType);
                        if (expression1 != null)
                        {
                            PSSetIndexBinder  pSSetIndexBinder   = this;
                            DynamicMetaObject dynamicMetaObject  = new DynamicMetaObject(target.Expression.Cast(target.LimitType), target.PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject1 = new DynamicMetaObject(expression, indexes[0].PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject2 = new DynamicMetaObject(expression1, value.PSGetTypeRestriction());
                            PropertyInfo      property           = target.LimitType.GetProperty("Length");
                            return(pSSetIndexBinder.IndexWithNegativeChecks(dynamicMetaObject, dynamicMetaObject1, dynamicMetaObject2, property, (Expression t, Expression i, Expression v) =>
                            {
                                Expression[] expressionArray = new Expression[1];
                                expressionArray[0] = i;
                                return Expression.Assign(Expression.ArrayAccess(t, expressionArray), v);
                            }
                                                                            ));
                        }
                        else
                        {
                            DynamicMetaObject dynamicMetaObject3 = errorSuggestion;
                            DynamicMetaObject dynamicMetaObject4 = dynamicMetaObject3;
                            if (dynamicMetaObject3 == null)
                            {
                                dynamicMetaObject4 = PSConvertBinder.ThrowNoConversion(value, elementType, this, this._version, indexes.Prepend <DynamicMetaObject>(target).OfType <DynamicMetaObject>().ToArray <DynamicMetaObject>());
                            }
                            return(dynamicMetaObject4);
                        }
                    }
                    else
                    {
                        DynamicMetaObject dynamicMetaObject5 = errorSuggestion;
                        DynamicMetaObject dynamicMetaObject6 = dynamicMetaObject5;
                        if (dynamicMetaObject5 == null)
                        {
                            DynamicMetaObject[] dynamicMetaObjectArray = new DynamicMetaObject[2];
                            dynamicMetaObjectArray[0] = target;
                            dynamicMetaObjectArray[1] = value;
                            dynamicMetaObject6        = PSConvertBinder.ThrowNoConversion(indexes[0], typeof(int), this, this._version, dynamicMetaObjectArray);
                        }
                        return(dynamicMetaObject6);
                    }
                }
                else
                {
                    DynamicMetaObject dynamicMetaObject7 = errorSuggestion;
                    DynamicMetaObject dynamicMetaObject8 = dynamicMetaObject7;
                    if (dynamicMetaObject7 == null)
                    {
                        DynamicMetaObject   dynamicMetaObject9      = target;
                        DynamicMetaObject[] dynamicMetaObjectArray1 = indexes;
                        BindingRestrictions bindingRestriction      = value.PSGetTypeRestriction();
                        string              str = "ArraySliceAssignmentFailed";
                        string              arraySliceAssignmentFailed = ParserStrings.ArraySliceAssignmentFailed;
                        Expression[]        expressionArray1           = new Expression[1];
                        Expression[]        expressionArray2           = expressionArray1;
                        int                 num = 0;
                        MethodInfo          arrayOpsIndexStringMessage = CachedReflectionInfo.ArrayOps_IndexStringMessage;
                        Type                type = typeof(object);
                        DynamicMetaObject[] dynamicMetaObjectArray2 = indexes;
                        expressionArray2[num] = Expression.Call(arrayOpsIndexStringMessage, Expression.NewArrayInit(type, dynamicMetaObjectArray2.Select <DynamicMetaObject, Expression>((DynamicMetaObject i) => i.Expression.Cast(typeof(object)))));
                        dynamicMetaObject8    = dynamicMetaObject9.ThrowRuntimeError(dynamicMetaObjectArray1, bindingRestriction, str, arraySliceAssignmentFailed, expressionArray1);
                    }
                    return(dynamicMetaObject8);
                }
            }
            else
            {
                return(this.SetIndexMultiDimensionArray(target, indexes, value, errorSuggestion));
            }
        }
Beispiel #6
0
        private DynamicMetaObject InvokeIndexer(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion, string methodName)
        {
            DynamicMetaObject dynamicMetaObject;
            Func <Expression, Expression, Expression, Expression> func = null;
            MethodInfo methodInfo = PSInvokeMemberBinder.FindBestMethod(target, indexes.Append <DynamicMetaObject>(value), string.Concat("set_", methodName), false, this._constraints);

            if (methodInfo != null)
            {
                ParameterInfo[] parameters = methodInfo.GetParameters();
                if ((int)parameters.Length == (int)indexes.Length + 1)
                {
                    Expression[] expressionArray = new Expression[(int)parameters.Length];
                    int          num             = 0;
                    while (num < (int)parameters.Length)
                    {
                        Type         parameterType    = parameters[num].ParameterType;
                        Expression[] expressionArray1 = expressionArray;
                        int          num1             = num;
                        if (num == (int)parameters.Length - 1)
                        {
                            dynamicMetaObject = value;
                        }
                        else
                        {
                            dynamicMetaObject = indexes[num];
                        }
                        expressionArray1[num1] = PSGetIndexBinder.ConvertIndex(dynamicMetaObject, parameterType);
                        if (expressionArray[num] != null)
                        {
                            num++;
                        }
                        else
                        {
                            DynamicMetaObject dynamicMetaObject1 = errorSuggestion;
                            DynamicMetaObject dynamicMetaObject2 = dynamicMetaObject1;
                            if (dynamicMetaObject1 == null)
                            {
                                dynamicMetaObject2 = PSConvertBinder.ThrowNoConversion(target, parameterType, this, this._version, indexes.Append <DynamicMetaObject>(value).ToArray <DynamicMetaObject>());
                            }
                            return(dynamicMetaObject2);
                        }
                    }
                    if ((int)parameters.Length == 2 && parameters[0].ParameterType.Equals(typeof(int)) && target.Value as IDictionary == null)
                    {
                        PropertyInfo property     = target.LimitType.GetProperty("Length");
                        PropertyInfo propertyInfo = property;
                        if (property == null)
                        {
                            propertyInfo = target.LimitType.GetProperty("Count");
                        }
                        PropertyInfo propertyInfo1 = propertyInfo;
                        if (propertyInfo1 != null)
                        {
                            PSSetIndexBinder  pSSetIndexBinder   = this;
                            DynamicMetaObject dynamicMetaObject3 = new DynamicMetaObject(target.Expression.Cast(target.LimitType), target.PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject4 = new DynamicMetaObject(expressionArray[0], indexes[0].PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject5 = new DynamicMetaObject(expressionArray[1], value.PSGetTypeRestriction());
                            PropertyInfo      propertyInfo2      = propertyInfo1;
                            if (func == null)
                            {
                                func = (Expression t, Expression i, Expression v) => Expression.Call(t, methodInfo, i, v);
                            }
                            return(pSSetIndexBinder.IndexWithNegativeChecks(dynamicMetaObject3, dynamicMetaObject4, dynamicMetaObject5, propertyInfo2, func));
                        }
                    }
                    BindingRestrictions bindingRestriction = target.CombineRestrictions(indexes).Merge(value.PSGetTypeRestriction());
                    bindingRestriction = bindingRestriction.Merge(BinderUtils.GetVersionCheck(this, this._version));
                    bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage());
                    Expression          expression          = expressionArray[(int)expressionArray.Length - 1];
                    ParameterExpression parameterExpression = Expression.Parameter(expression.Type, "value");
                    expressionArray[(int)expressionArray.Length - 1] = parameterExpression;
                    ParameterExpression[] parameterExpressionArray = new ParameterExpression[1];
                    parameterExpressionArray[0] = parameterExpression;
                    Expression[] expressionArray2 = new Expression[3];
                    expressionArray2[0] = Expression.Assign(parameterExpression, expression);
                    expressionArray2[1] = Expression.Call(target.Expression.Cast(target.LimitType), methodInfo, expressionArray);
                    expressionArray2[2] = parameterExpression.Cast(typeof(object));
                    return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray2), bindingRestriction));
                }
                else
                {
                    DynamicMetaObject dynamicMetaObject6 = errorSuggestion;
                    DynamicMetaObject dynamicMetaObject7 = dynamicMetaObject6;
                    if (dynamicMetaObject6 == null)
                    {
                        dynamicMetaObject7 = this.CannotIndexTarget(target, indexes, value);
                    }
                    return(dynamicMetaObject7);
                }
            }
            else
            {
                DynamicMetaObject dynamicMetaObject8 = errorSuggestion;
                DynamicMetaObject dynamicMetaObject9 = dynamicMetaObject8;
                if (dynamicMetaObject8 == null)
                {
                    dynamicMetaObject9 = this.CannotIndexTarget(target, indexes, value);
                }
                return(dynamicMetaObject9);
            }
        }
Beispiel #7
0
 public static PSGetIndexBinder Get(int argCount, PSMethodInvocationConstraints constraints, bool allowSlicing = true)
 {
     lock (_binderCache)
     {
         PSGetIndexBinder binder;
         Tuple<CallInfo, PSMethodInvocationConstraints, bool> key = Tuple.Create<CallInfo, PSMethodInvocationConstraints, bool>(new CallInfo(argCount, new string[0]), constraints, allowSlicing);
         if (!_binderCache.TryGetValue(key, out binder))
         {
             binder = new PSGetIndexBinder(key);
             _binderCache.Add(key, binder);
         }
         return binder;
     }
 }