Exemplo n.º 1
0
        private ExpressionAst CheckUsingExpression(ExpressionAst exprAst)
        {
            RuntimeHelpers.EnsureSufficientExecutionStack();
            if (exprAst is VariableExpressionAst)
            {
                return(null);
            }
            MemberExpressionAst ast = exprAst as MemberExpressionAst;

            if (((ast != null) && !(ast is InvokeMemberExpressionAst)) && (ast.Member is StringConstantExpressionAst))
            {
                return(this.CheckUsingExpression(ast.Expression));
            }
            IndexExpressionAst ast2 = exprAst as IndexExpressionAst;

            if (ast2 == null)
            {
                return(exprAst);
            }
            if (!this.IsValidAttributeArgument(ast2.Index))
            {
                return(ast2.Index);
            }
            return(this.CheckUsingExpression(ast2.Target));
        }
Exemplo n.º 2
0
    public System.Object VisitIndexExpression(System.Management.Automation.Language.IndexExpressionAst indexExpressionAst)
    {
        IScriptExtent mappedExtent = MapExtent(indexExpressionAst.Extent);

        ExpressionAst mappedTarget = _VisitExpression(indexExpressionAst.Target);
        ExpressionAst mappedIndex  = _VisitExpression(indexExpressionAst.Index);

        return(new IndexExpressionAst(mappedExtent, mappedTarget, mappedIndex));
    }
Exemplo n.º 3
0
        public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            // Get the value of the index and value and call the compiler
            var index = indexExpressionAst.Index.Accept(this);
            var target = indexExpressionAst.Target.Accept(this);
            if (index == null || target == null)
            {
                throw new ArgumentNullException("indexExpressionAst");
            }

            return GetIndexedValueFromTarget(target, index);
        }
Exemplo n.º 4
0
        private static VariableExpressionAst ExtractUsingVariableImpl(ExpressionAst expression)
        {
            VariableExpressionAst subExpression;
            UsingExpressionAst    ast = expression as UsingExpressionAst;

            if (ast != null)
            {
                subExpression = ast.SubExpression as VariableExpressionAst;
                if (subExpression != null)
                {
                    return(subExpression);
                }
                return(ExtractUsingVariableImpl(ast.SubExpression));
            }
            IndexExpressionAst ast3 = expression as IndexExpressionAst;

            if (ast3 != null)
            {
                subExpression = ast3.Target as VariableExpressionAst;
                if (subExpression != null)
                {
                    return(subExpression);
                }
                return(ExtractUsingVariableImpl(ast3.Target));
            }
            MemberExpressionAst ast4 = expression as MemberExpressionAst;

            if (ast4 == null)
            {
                return(null);
            }
            subExpression = ast4.Expression as VariableExpressionAst;
            if (subExpression != null)
            {
                return(subExpression);
            }
            return(ExtractUsingVariableImpl(ast4.Expression));
        }
Exemplo n.º 5
0
 public override AstVisitAction VisitIndexExpression(IndexExpressionAst ast)
 {
     return(Check(ast));
 }
Exemplo n.º 6
0
 public virtual AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return AstVisitAction.Continue;
 }
 /// <summary/>
 public virtual object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return _decorated.VisitIndexExpression(indexExpressionAst);
 }
Exemplo n.º 8
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     indexExpressionAst.Target.Accept(this);
     indexExpressionAst.Index.Accept(this);
     return null;
 }
Exemplo n.º 9
0
 public override AstVisitAction VisitIndexExpression(IndexExpressionAst ast) { return CheckParent(ast); }
Exemplo n.º 10
0
 private void SetIndexExpressionValue(IndexExpressionAst indexExpressionAst, object value)
 {
     var target = PSObject.Unwrap(EvaluateAst(indexExpressionAst.Target));
     var index = PSObject.Unwrap(EvaluateAst(indexExpressionAst.Index, false));
     var arrayTarget = target as Array;
     if (arrayTarget != null && arrayTarget.Rank > 1)
     {
         var rawIndices = (int[])LanguagePrimitives.ConvertTo(index, typeof(int[]));
         var indices = ConvertNegativeIndicesForMultidimensionalArray(arrayTarget, rawIndices);
         var convertedValue = LanguagePrimitives.ConvertTo(value, arrayTarget.GetType().GetElementType());
         arrayTarget.SetValue(convertedValue, indices);
         return;
     }
     // we don't support assignment to slices (, yet?), so throw an error
     if (index is Array && ((Array)index).Length > 1)
     {
         throw new PSInvalidOperationException("Assignment to slices is not supported");
     }
     // otherwise do assignments
     var iListTarget = target as IList;
     if (iListTarget != null) // covers also arrays
     {
         var intIdx = (int)LanguagePrimitives.ConvertTo(index, typeof(int));
         intIdx = intIdx < 0 ? intIdx + iListTarget.Count : intIdx;
         iListTarget[intIdx] = value;
     }
     else if (target is IDictionary)
     {
         ((IDictionary)target)[index] = value;
     }
     else
     {
         var msg = String.Format("Cannot set index for type '{0}'", target.GetType().FullName);
         throw new PSInvalidOperationException(msg);
     }
 }
Exemplo n.º 11
0
 /// <summary/>
 public virtual AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst) => DefaultVisit(indexExpressionAst);
Exemplo n.º 12
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     Expression element = this.CompileExpressionOperand(indexExpressionAst.Target);
     ExpressionAst index = indexExpressionAst.Index;
     ArrayLiteralAst ast2 = index as ArrayLiteralAst;
     PSMethodInvocationConstraints constraints = CombineTypeConstraintForMethodResolution(GetTypeConstraintForMethodResolution(indexExpressionAst.Target), GetTypeConstraintForMethodResolution(index));
     if ((ast2 != null) && (ast2.Elements.Count > 1))
     {
         return Expression.Dynamic(PSGetIndexBinder.Get(ast2.Elements.Count, constraints, true), typeof(object), ast2.Elements.Select<ExpressionAst, Expression>(new Func<ExpressionAst, Expression>(this.CompileExpressionOperand)).Prepend<Expression>(element));
     }
     return Expression.Dynamic(PSGetIndexBinder.Get(1, constraints, true), typeof(object), element, this.CompileExpressionOperand(index));
 }
Exemplo n.º 13
0
 internal SettableIndexExpression(IndexExpressionAst expressionAst, ExecutionVisitor currentExecution)
     : base (currentExecution)
 {
     _expressionAst = expressionAst;
 }
Exemplo n.º 14
0
 /// <summary/>
 public virtual object VisitIndexExpression(IndexExpressionAst indexExpressionAst) { return null; }
Exemplo n.º 15
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return((bool)indexExpressionAst.Index.Accept(this) && (bool)indexExpressionAst.Target.Accept(this));
 }
Exemplo n.º 16
0
        public override AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            var targetValue = PSObject.Unwrap(EvaluateAst(indexExpressionAst.Target, true));

            object index = PSObject.Unwrap(EvaluateAst(indexExpressionAst.Index));

            if (targetValue is PSObject) targetValue = ((PSObject)targetValue).BaseObject;

            // Check dicts/hashtables first
            var dictTarget = targetValue as IDictionary;
            if (dictTarget != null)
            {
                var idxobjects = index is object[] ? (object[])index : new object[] { index };
                foreach (var idxobj in idxobjects)
                {
                    var res = dictTarget[PSObject.Unwrap(idxobj)];
                    _pipelineCommandRuntime.WriteObject(res);
                }
                return AstVisitAction.SkipChildren;
            }

            // otherwise we need int indexing
            var indices = (int[]) LanguagePrimitives.ConvertTo(index, typeof(int[]));

            // check for real multidimensional array access first
            var arrayTarget = targetValue as Array;
            if (arrayTarget != null && arrayTarget.Rank > 1)
            {
                int[] convIndices = ConvertNegativeIndicesForMultidimensionalArray(arrayTarget, indices);
                object arrayValue = null;
                try
                {
                    arrayValue = arrayTarget.GetValue(convIndices);
                }
                catch (IndexOutOfRangeException) // just write nothing to pipeline
                {
                }
                this._pipelineCommandRuntime.WriteObject(arrayValue);
                return AstVisitAction.SkipChildren;
            }

            // if we have a string, we need to access it as an char[]
            if (targetValue is string)
            {
                targetValue = ((string)targetValue).ToCharArray();
            }

            // now we can access arrays, list, and string (charArrays) all the same by using the IList interface
            var iListTarget = targetValue as IList;
            if (iListTarget == null)
            {
                var msg = String.Format("Cannot index an object of type '{0}'.", targetValue.GetType().FullName);
                throw new PSInvalidOperationException(msg);
            }

            var numItems = iListTarget.Count;
            // now get all elements from the index list
            bool writtenFromList = false;
            foreach (int curIdx in indices)
            {
                // support negative indices
                var idx = curIdx < 0 ? curIdx + numItems : curIdx;
                // check if it's a valid index, otherwise ignore
                if (idx >= 0 && idx < numItems)
                {
                    _pipelineCommandRuntime.WriteObject(iListTarget[idx]);
                    writtenFromList = true;
                }
            }
            if (!writtenFromList)
            {
                _pipelineCommandRuntime.WriteObject(null);
            }
            return AstVisitAction.SkipChildren;
        }
Exemplo n.º 17
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return(false);
 }
Exemplo n.º 18
0
        public override AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            // Array references are never allowed.  They could turn into function calls.
            ReportError(indexExpressionAst, () => ParserStrings.ArrayReferenceNotSupportedInDataSection);

            return AstVisitAction.Continue;
        }
Exemplo n.º 19
0
 public override AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     this.ReportError(indexExpressionAst, () => ParserStrings.ArrayReferenceNotSupportedInDataSection, new object[0]);
     return AstVisitAction.Continue;
 }
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst) { throw new UnexpectedElementException(); }
Exemplo n.º 21
0
        /// <summary>
        /// Visit Index Expression
        /// </summary>
        /// <param name="indexExpressionAst"></param>
        /// <returns></returns>
        public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            if (indexExpressionAst == null) return null;

            indexExpressionAst.Target.Visit(this.Decorator);
            indexExpressionAst.Index.Visit(this.Decorator);
            return null;
        }
Exemplo n.º 22
0
 public override AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     this.ReportError(indexExpressionAst, () => ParserStrings.ArrayReferenceNotSupportedInDataSection, new object[0]);
     return(AstVisitAction.Continue);
 }
Exemplo n.º 23
0
 public override AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     _pipelineCommandRuntime.WriteObject(new SettableIndexExpression(indexExpressionAst, this).GetValue(), false);
     return AstVisitAction.SkipChildren;
 }
Exemplo n.º 24
0
 public override AstVisitAction VisitIndexExpression(IndexExpressionAst ast)
 {
     return this.Check(ast);
 }
Exemplo n.º 25
0
        public override AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            var targetValue = EvaluateAst(indexExpressionAst.Target);

            int index = (int)EvaluateAst(indexExpressionAst.Index);

            var stringTargetValue = targetValue as string;
            if (stringTargetValue != null)
            {
                var result = stringTargetValue[index];
                this._pipelineCommandRuntime.WriteObject(result);
            }

            else if (targetValue is IList)
            {
                var result = (targetValue as IList)[index];
                this._pipelineCommandRuntime.WriteObject(result);
            }

            else throw new NotImplementedException(indexExpressionAst.ToString() + " " + targetValue.GetType());

            return AstVisitAction.SkipChildren;
        }
Exemplo n.º 26
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return (!((bool) indexExpressionAst.Target.Accept(this)) ? ((object) 0) : ((object) ((bool) indexExpressionAst.Index.Accept(this))));
 }
Exemplo n.º 27
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     indexExpressionAst.Target.Accept(this);
     indexExpressionAst.Index.Accept(this);
     return(null);
 }
Exemplo n.º 28
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst) { return AutomationNull.Value; }
Exemplo n.º 29
0
 /// <summary/>
 public virtual object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return(null);
 }
Exemplo n.º 30
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return (bool)indexExpressionAst.Index.Accept(this) && (bool)indexExpressionAst.Target.Accept(this);
 }
Exemplo n.º 31
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return false;
 }
Exemplo n.º 32
0
        public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            // Get the value of the index and value and call the compiler
            var index = indexExpressionAst.Index.Accept(this);
            var target = indexExpressionAst.Target.Accept(this);
            if (index == null || target == null)
            {
                throw new ArgumentNullException("indexExpressionAst");
            }

            return GetIndexedValueFromTarget(target, index);
        }
Exemplo n.º 33
0
 /// <summary/>
 public virtual AstVisitAction VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return(AstVisitAction.Continue);
 }
Exemplo n.º 34
0
        public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
        {
            var targetExpr = CompileExpressionOperand(indexExpressionAst.Target);

            var index = indexExpressionAst.Index;
            var arrayLiteral = (index as ArrayLiteralAst);
            var constraints = CombineTypeConstraintForMethodResolution(GetTypeConstraintForMethodResolution(indexExpressionAst.Target),
                                                                       GetTypeConstraintForMethodResolution(index));

            // An array literal is either:
            //    $x[1,2]
            // or
            //    $x[,1]
            // In the former case, the user is requesting an array slice.  In the latter case, they index expression is likely
            // an array (dynamically determined) and they don't want an array slice, they want to use the array as the index
            // expression.
            if (arrayLiteral != null && arrayLiteral.Elements.Count > 1)
            {
                return DynamicExpression.Dynamic(PSGetIndexBinder.Get(arrayLiteral.Elements.Count, constraints), typeof(object),
                    arrayLiteral.Elements.Select(CompileExpressionOperand).Prepend(targetExpr));
            }

            return DynamicExpression.Dynamic(PSGetIndexBinder.Get(1, constraints), typeof(object), targetExpr, CompileExpressionOperand(index));
        }
Exemplo n.º 35
0
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     return(AutomationNull.Value);
 }
 public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
 {
     throw new NotImplementedException();
 }