Esempio n. 1
0
        internal static bool ContainsOperatorCompiled(System.Management.Automation.ExecutionContext context, CallSite <Func <CallSite, object, IEnumerator> > getEnumeratorSite, CallSite <Func <CallSite, object, object, object> > comparerSite, object left, object right)
        {
            IEnumerator enumerator = getEnumeratorSite.Target(getEnumeratorSite, left);

            if ((enumerator != null) && !(enumerator is EnumerableOps.NonEnumerableObjectEnumerator))
            {
                while (EnumerableOps.MoveNext(context, enumerator))
                {
                    object obj2 = EnumerableOps.Current(enumerator);
                    if ((bool)comparerSite.Target(comparerSite, obj2, right))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            return((bool)comparerSite.Target(comparerSite, left, right));
        }
Esempio n. 2
0
        // internal static object AddAssignArrayDefault(Array target, Array arg)
        // {
        //     int newSize = target.Length + arg.Length;
        //     object[] result  = new object[newSize];

        //     target.CopyTo(result, 0);
        //     arg.CopyTo(result, target.Length);

        //     return result;
        // }

        internal static object AddAssignEnumerable(ExecutionContext context, Array target, IEnumerator arg, Type elementType)
        {
//            var fakeEnumerator = arg as NonEnumerableObjectEnumerator;

            var argAsList = new List <object>();

            while (EnumerableOps.MoveNext(context, arg))
            {
                argAsList.Add(EnumerableOps.Current(arg));
            }

            Array result = Array.CreateInstance(elementType, target.Length + argAsList.Count);

            target.CopyTo(result, 0);
            argAsList.ToArray().CopyTo(result, target.Length);

            return(result);
        }
Esempio n. 3
0
        internal static object GetMDArrayValueOrSlice(Array array, object indexes)
        {
            Exception whyFailed = null;

            int[] indexArray = null;
            try
            {
                indexArray = (int[])LanguagePrimitives.ConvertTo(indexes, typeof(int[]), NumberFormatInfo.InvariantInfo);
            }
            catch (InvalidCastException ice)
            {
                // Ignore an exception here as we may actually be looking at an array of arrays
                // which could still be ok. Save the exception as we may use it later...
                whyFailed = ice;
            }

            if (indexArray != null)
            {
                if (indexArray.Length != array.Rank)
                {
                    // rank failed to match so error...
                    ReportIndexingError(array, indexes, null);
                }

                return(GetMDArrayValue(array, indexArray, false));
            }

            var indexList = new List <int[]>();

            var ie = LanguagePrimitives.GetEnumerator(indexes);

            while (EnumerableOps.MoveNext(null, ie))
            {
                var currentIndex = EnumerableOps.Current(ie);
                try
                {
                    indexArray = LanguagePrimitives.ConvertTo <int[]>(currentIndex);
                }
                catch (InvalidCastException)
                {
                    indexArray = null;
                }

                if (indexArray == null || indexArray.Length != array.Rank)
                {
                    if (whyFailed != null)
                    {
                        // If the first fails, report the original exception and all indices
                        ReportIndexingError(array, indexes, whyFailed);
                        Diagnostics.Assert(false, "ReportIndexingError must throw");
                    }
                    // If the second or subsequent index fails, report the failure for just that index
                    ReportIndexingError(array, currentIndex, null);
                    Diagnostics.Assert(false, "ReportIndexingError must throw");
                }

                // Only use whyFailed the first time through, otherwise
                whyFailed = null;
                indexList.Add(indexArray);
            }

            // Optimistically assume all indices are valid so the result array is the same size.
            // If that turns out to be wrong, we'll just copy the elements produced.
            var result = new object[indexList.Count];
            int j      = 0;

            foreach (var i in indexList)
            {
                var value = GetMDArrayValue(array, i, true);
                if (value != AutomationNull.Value)
                {
                    result[j++] = value;
                }
            }

            if (j != indexList.Count)
            {
                var shortResult = new object[j];
                Array.Copy(result, shortResult, j);
                return(shortResult);
            }

            return(result);
        }
Esempio n. 4
0
        internal static object GetMDArrayValueOrSlice(Array array, object indexes)
        {
            Exception reason = null;

            int[] numArray = null;
            try
            {
                numArray = (int[])LanguagePrimitives.ConvertTo(indexes, typeof(int[]), NumberFormatInfo.InvariantInfo);
            }
            catch (InvalidCastException exception2)
            {
                reason = exception2;
            }
            if (numArray != null)
            {
                if (numArray.Length != array.Rank)
                {
                    ReportIndexingError(array, indexes, null);
                }
                return(GetMDArrayValue(array, numArray, false));
            }
            List <int[]> list       = new List <int[]>();
            IEnumerator  enumerator = LanguagePrimitives.GetEnumerator(indexes);

            while (EnumerableOps.MoveNext(null, enumerator))
            {
                object valueToConvert = EnumerableOps.Current(enumerator);
                try
                {
                    numArray = LanguagePrimitives.ConvertTo <int[]>(valueToConvert);
                }
                catch (InvalidCastException)
                {
                    numArray = null;
                }
                if ((numArray == null) || (numArray.Length != array.Rank))
                {
                    if (reason != null)
                    {
                        ReportIndexingError(array, indexes, reason);
                    }
                    ReportIndexingError(array, valueToConvert, null);
                }
                reason = null;
                list.Add(numArray);
            }
            object[] sourceArray = new object[list.Count];
            int      length      = 0;

            foreach (int[] numArray2 in list)
            {
                object obj3 = GetMDArrayValue(array, numArray2, true);
                if (obj3 != AutomationNull.Value)
                {
                    sourceArray[length++] = obj3;
                }
            }
            if (length != list.Count)
            {
                object[] destinationArray = new object[length];
                Array.Copy(sourceArray, destinationArray, length);
                return(destinationArray);
            }
            return(sourceArray);
        }