Example #1
0
        private DynamicMetaObject GetIndexMultiDimensionArray(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject errorSuggestion)
        {
            Array array = (Array)target.Value;

            if (indexes.Length == 1)
            {
                if (PSEnumerableBinder.IsEnumerable(indexes[0]) == null)
                {
                    return(target.ThrowRuntimeError(indexes, BindingRestrictions.Empty, "NeedMultidimensionalIndex", ParserStrings.NeedMultidimensionalIndex, new Expression[] { ExpressionCache.Constant(array.Rank), Expression.Dynamic(PSToStringBinder.Get(), typeof(string), indexes[0].Expression, ExpressionCache.GetExecutionContextFromTLS) }));
                }
                return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.ArrayOps_GetMDArrayValueOrSlice, Expression.Convert(target.Expression, typeof(Array)), indexes[0].Expression.Cast(typeof(object))), target.CombineRestrictions(indexes)));
            }
            IEnumerable <Expression> source = from index in indexes
                                              select ConvertIndex(index, typeof(int)) into i
                                                  where i != null
                                              select i;

            if (source.Count <Expression>() == indexes.Length)
            {
                return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.ArrayOps_GetMDArrayValue, Expression.Convert(target.Expression, typeof(Array)), Expression.NewArrayInit(typeof(int), source), ExpressionCache.Constant(!this._allowSlicing)), target.CombineRestrictions(indexes)));
            }
            if (this._allowSlicing)
            {
                return(this.InvokeSlicingIndexer(target, indexes));
            }
            return(errorSuggestion ?? this.CannotIndexTarget(target, indexes));
        }
Example #2
0
 private DynamicMetaObject PropertyDoesntExist(DynamicMetaObject target, BindingRestrictions restrictions)
 {
     if (!this._nonEnumerating && (target.Value != AutomationNull.Value))
     {
         DynamicMetaObject obj2 = PSEnumerableBinder.IsEnumerable(target);
         if (obj2 != null)
         {
             return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.EnumerableOps_PropertyGetter, Expression.Constant(this.GetNonEnumeratingBinder()), obj2.Expression), restrictions));
         }
     }
     if (base.Name.Equals("Length", StringComparison.OrdinalIgnoreCase) || base.Name.Equals("Count", StringComparison.OrdinalIgnoreCase))
     {
         int i = (PSObject.Base(target.Value) == null) ? 0 : 1;
         return(new DynamicMetaObject(Expression.Condition(Compiler.IsStrictMode(2, null), this.ThrowPropertyNotFoundStrict(), ExpressionCache.Constant(i).Cast(typeof(object))), restrictions));
     }
     return(new DynamicMetaObject(Expression.Condition(Compiler.IsStrictMode(2, null), this.ThrowPropertyNotFoundStrict(), this._nonEnumerating ? ExpressionCache.AutomationNullConstant : ExpressionCache.NullConstant), restrictions));
 }
Example #3
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);
 }
Example #4
0
        public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            if (!target.HasValue)
            {
                return(base.Defer(target, args));
            }
            if (target.Value == AutomationNull.Value)
            {
                return(new DynamicMetaObject(Expression.Block(typeof(void), new Expression[] { Expression.Call(CachedReflectionInfo.PipelineOps_Nop, new Expression[0]) }), BindingRestrictions.GetInstanceRestriction(target.Expression, AutomationNull.Value)).WriteToDebugLog(this));
            }
            DynamicMetaObject obj2 = PSEnumerableBinder.IsEnumerable(target);

            if (obj2 == null)
            {
                DynamicMetaObject   obj3         = PSVariableAssignmentBinder.Get().Bind(target, new DynamicMetaObject[0]);
                BindingRestrictions restrictions = target.LimitType.IsValueType ? obj3.Restrictions : target.PSGetTypeRestriction();
                return(new DynamicMetaObject(Expression.Call(args[0].Expression, CachedReflectionInfo.Pipe_Add, new Expression[] { obj3.Expression.Cast(typeof(object)) }), restrictions).WriteToDebugLog(this));
            }
            bool b = !(PSObject.Base(target.Value) is IEnumerator);

            return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.EnumerableOps_WriteEnumerableToPipe, obj2.Expression, args[0].Expression, args[1].Expression, ExpressionCache.Constant(b)), obj2.Restrictions).WriteToDebugLog(this));
        }
        public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            if (!target.HasValue)
            {
                return(base.Defer(target, args));
            }
            if ((target.Value is PSObject) && (PSObject.Base(target.Value) != target.Value))
            {
                return(this.DeferForPSObject(new DynamicMetaObject[] { target }).WriteToDebugLog(this));
            }
            DynamicMetaObject obj2 = PSEnumerableBinder.IsEnumerable(target);

            if (obj2 == null)
            {
                return(new DynamicMetaObject(Expression.NewArrayInit(typeof(object), new Expression[] { target.Expression.Cast(typeof(object)) }), target.PSGetTypeRestriction()).WriteToDebugLog(this));
            }
            if (PSObject.Base(target.Value) is ArrayList)
            {
                return(new DynamicMetaObject(Expression.Call(PSEnumerableBinder.MaybeDebase(this, e => e.Cast(typeof(ArrayList)), target), CachedReflectionInfo.ArrayList_ToArray), PSEnumerableBinder.GetRestrictions(target)).WriteToDebugLog(this));
            }
            return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.EnumerableOps_ToArray, obj2.Expression), target.PSGetTypeRestriction()).WriteToDebugLog(this));
        }
Example #6
0
        public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
        {
            PSMemberInfo        pSMemberInfo       = null;
            BindingRestrictions bindingRestriction = null;
            bool       flag = false;
            Type       type = null;
            Expression expression;
            Type       value;

            MethodInformation[] methodInformationArray;
            Type type1;

            if (target.HasValue)
            {
                DynamicMetaObject[] dynamicMetaObjectArray = args;
                if (!dynamicMetaObjectArray.Where <DynamicMetaObject>((DynamicMetaObject arg) => !arg.HasValue).Any <DynamicMetaObject>())
                {
                    object obj = PSObject.Base(target.Value);
                    if (obj != null)
                    {
                        if (!this._getMemberBinder.HasInstanceMember || !PSGetMemberBinder.TryGetInstanceMember(target.Value, base.Name, out pSMemberInfo))
                        {
                            PSMethodInfo        pSMethodInfo            = this._getMemberBinder.GetPSMemberInfo(target, out bindingRestriction, out flag, out type, null, null) as PSMethodInfo;
                            DynamicMetaObject[] dynamicMetaObjectArray1 = args;
                            BindingRestrictions bindingRestriction1     = bindingRestriction;
                            bindingRestriction = dynamicMetaObjectArray1.Aggregate <DynamicMetaObject, BindingRestrictions>(bindingRestriction1, (BindingRestrictions current, DynamicMetaObject arg) => current.Merge(arg.PSGetMethodArgumentRestriction()));
                            if (ExecutionContext.HasEverUsedConstrainedLanguage)
                            {
                                bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage());
                                ExecutionContext  executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                                DynamicMetaObject dynamicMetaObject       = PSGetMemberBinder.EnsureAllowedInLanguageMode(executionContextFromTLS.LanguageMode, target, obj, base.Name, this._static, args, bindingRestriction, "MethodInvocationNotSupportedInConstrainedLanguage", ParserStrings.InvokeMethodConstrainedLanguage);
                                if (dynamicMetaObject != null)
                                {
                                    return(dynamicMetaObject.WriteToDebugLog(this));
                                }
                            }
                            if (flag)
                            {
                                PSObject pSObject = target.Value as PSObject;
                                if (pSObject != null && (obj.GetType().Equals(typeof(Hashtable)) || obj.GetType().Equals(typeof(ArrayList))))
                                {
                                    if (!pSObject.isDeserialized)
                                    {
                                        bindingRestriction = bindingRestriction.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Not(Expression.Field(target.Expression.Cast(typeof(PSObject)), CachedReflectionInfo.PSObject_isDeserialized))));
                                    }
                                    else
                                    {
                                        pSMethodInfo       = null;
                                        bindingRestriction = bindingRestriction.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Field(target.Expression.Cast(typeof(PSObject)), CachedReflectionInfo.PSObject_isDeserialized)));
                                    }
                                }
                                PSMethod pSMethod = pSMethodInfo as PSMethod;
                                if (pSMethod == null)
                                {
                                    PSScriptMethod pSScriptMethod = pSMethodInfo as PSScriptMethod;
                                    if (pSScriptMethod == null)
                                    {
                                        PSCodeMethod pSCodeMethod = pSMethodInfo as PSCodeMethod;
                                        if (pSCodeMethod == null)
                                        {
                                            PSParameterizedProperty pSParameterizedProperty = pSMethodInfo as PSParameterizedProperty;
                                            if (pSParameterizedProperty == null)
                                            {
                                                if (errorSuggestion == null)
                                                {
                                                    if (this._static)
                                                    {
                                                        value = (Type)target.Value;
                                                    }
                                                    else
                                                    {
                                                        value = PSObject.Base(target.Value).GetType();
                                                    }
                                                    Type type2 = value;
                                                    if (!this._static && !this._nonEnumerating && target.Value != AutomationNull.Value)
                                                    {
                                                        DynamicMetaObject dynamicMetaObject1 = PSEnumerableBinder.IsEnumerable(target);
                                                        if (dynamicMetaObject1 != null)
                                                        {
                                                            return(this.InvokeMemberOnCollection(dynamicMetaObject1, args, type2, bindingRestriction));
                                                        }
                                                    }
                                                    Expression[] expressionArray = new Expression[2];
                                                    expressionArray[0] = Expression.Constant(type2.FullName);
                                                    expressionArray[1] = Expression.Constant(base.Name);
                                                    return(new DynamicMetaObject(Compiler.ThrowRuntimeError("MethodNotFound", ParserStrings.MethodNotFound, expressionArray), bindingRestriction).WriteToDebugLog(this));
                                                }
                                                else
                                                {
                                                    return(errorSuggestion.WriteToDebugLog(this));
                                                }
                                            }
                                            else
                                            {
                                                DotNetAdapter.ParameterizedPropertyCacheEntry parameterizedPropertyCacheEntry = (DotNetAdapter.ParameterizedPropertyCacheEntry)pSParameterizedProperty.adapterData;
                                                PSInvokeMemberBinder pSInvokeMemberBinder    = this;
                                                DynamicMetaObject    dynamicMetaObject2      = target;
                                                DynamicMetaObject[]  dynamicMetaObjectArray2 = args;
                                                BindingRestrictions  bindingRestriction2     = bindingRestriction;
                                                if (this._propertySetter)
                                                {
                                                    methodInformationArray = parameterizedPropertyCacheEntry.setterInformation;
                                                }
                                                else
                                                {
                                                    methodInformationArray = parameterizedPropertyCacheEntry.getterInformation;
                                                }
                                                if (this._propertySetter)
                                                {
                                                    type1 = typeof(SetValueInvocationException);
                                                }
                                                else
                                                {
                                                    type1 = typeof(GetValueInvocationException);
                                                }
                                                return(pSInvokeMemberBinder.InvokeDotNetMethod(dynamicMetaObject2, dynamicMetaObjectArray2, bindingRestriction2, methodInformationArray, type1).WriteToDebugLog(this));
                                            }
                                        }
                                        else
                                        {
                                            return(new DynamicMetaObject(PSInvokeMemberBinder.InvokeMethod(pSCodeMethod.CodeReference, null, args.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>(), false).Cast(typeof(object)), bindingRestriction).WriteToDebugLog(this));
                                        }
                                    }
                                    else
                                    {
                                        MethodInfo         pSScriptMethodInvokeScript = CachedReflectionInfo.PSScriptMethod_InvokeScript;
                                        ConstantExpression constantExpression         = Expression.Constant(base.Name);
                                        ConstantExpression constantExpression1        = Expression.Constant(pSScriptMethod.Script);
                                        Expression         expression1 = target.Expression.Cast(typeof(object));
                                        Type type3 = typeof(object);
                                        DynamicMetaObject[] dynamicMetaObjectArray3 = args;
                                        return(new DynamicMetaObject(Expression.Call(pSScriptMethodInvokeScript, constantExpression, constantExpression1, expression1, Expression.NewArrayInit(type3, dynamicMetaObjectArray3.Select <DynamicMetaObject, Expression>((DynamicMetaObject e) => e.Expression.Cast(typeof(object))))), bindingRestriction).WriteToDebugLog(this));
                                    }
                                }
                                else
                                {
                                    DotNetAdapter.MethodCacheEntry methodCacheEntry = (DotNetAdapter.MethodCacheEntry)pSMethod.adapterData;
                                    return(this.InvokeDotNetMethod(target, args, bindingRestriction, methodCacheEntry.methodInformationStructures, typeof(MethodException)).WriteToDebugLog(this));
                                }
                            }
                            else
                            {
                                if (!this._propertySetter)
                                {
                                    MethodInfo         pSInvokeMemberBinderInvokeAdaptedMember = CachedReflectionInfo.PSInvokeMemberBinder_InvokeAdaptedMember;
                                    Expression         expression2         = PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object));
                                    ConstantExpression constantExpression2 = Expression.Constant(base.Name);
                                    Type type4 = typeof(object);
                                    DynamicMetaObject[] dynamicMetaObjectArray4 = args;
                                    expression = Expression.Call(pSInvokeMemberBinderInvokeAdaptedMember, expression2, constantExpression2, Expression.NewArrayInit(type4, dynamicMetaObjectArray4.Select <DynamicMetaObject, Expression>((DynamicMetaObject arg) => arg.Expression.Cast(typeof(object)))));
                                }
                                else
                                {
                                    MethodInfo         pSInvokeMemberBinderInvokeAdaptedSetMember = CachedReflectionInfo.PSInvokeMemberBinder_InvokeAdaptedSetMember;
                                    Expression         expression3         = PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object));
                                    ConstantExpression constantExpression3 = Expression.Constant(base.Name);
                                    Type type5 = typeof(object);
                                    IEnumerable <DynamicMetaObject> dynamicMetaObjects = args.Take <DynamicMetaObject>((int)args.Length - 1);
                                    expression = Expression.Call(pSInvokeMemberBinderInvokeAdaptedSetMember, expression3, constantExpression3, Expression.NewArrayInit(type5, dynamicMetaObjects.Select <DynamicMetaObject, Expression>((DynamicMetaObject arg) => arg.Expression.Cast(typeof(object)))), args.Last <DynamicMetaObject>().Expression.Cast(typeof(object)));
                                }
                                return(new DynamicMetaObject(expression, bindingRestriction).WriteToDebugLog(this));
                            }
                        }
                        else
                        {
                            ParameterExpression  parameterExpression  = Expression.Variable(typeof(PSMethodInfo));
                            MethodCallExpression methodCallExpression = Expression.Call(CachedReflectionInfo.PSInvokeMemberBinder_TryGetInstanceMethod, target.Expression.Cast(typeof(object)), Expression.Constant(base.Name), parameterExpression);
                            ParameterExpression  parameterExpression1 = parameterExpression;
                            MethodInfo           pSMethodInfoInvoke   = CachedReflectionInfo.PSMethodInfo_Invoke;
                            Expression[]         expressionArray1     = new Expression[1];
                            Expression[]         expressionArray2     = expressionArray1;
                            int  num   = 0;
                            Type type6 = typeof(object);
                            DynamicMetaObject[] dynamicMetaObjectArray5 = args;
                            expressionArray2[num] = Expression.NewArrayInit(type6, dynamicMetaObjectArray5.Select <DynamicMetaObject, Expression>((DynamicMetaObject dmo) => dmo.Expression.Cast(typeof(object))));
                            ConditionalExpression conditionalExpression    = Expression.Condition(methodCallExpression, Expression.Call(parameterExpression1, pSMethodInfoInvoke, expressionArray1), base.GetUpdateExpression(typeof(object)));
                            ParameterExpression[] parameterExpressionArray = new ParameterExpression[1];
                            parameterExpressionArray[0] = parameterExpression;
                            Expression[] expressionArray3 = new Expression[1];
                            expressionArray3[0] = conditionalExpression;
                            return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray3), BinderUtils.GetVersionCheck(this._getMemberBinder, this._getMemberBinder._version)).WriteToDebugLog(this));
                        }
                    }
                    else
                    {
                        return(target.ThrowRuntimeError(args, BindingRestrictions.Empty, "InvokeMethodOnNull", ParserStrings.InvokeMethodOnNull, new Expression[0]).WriteToDebugLog(this));
                    }
                }
            }
            return(base.Defer(args.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>()));
        }