Пример #1
0
        internal static EcmaValue ParseIntInternal(string inputString, int radix, bool allowTrail)
        {
            Guard.ArgumentNotNull(inputString, "inputString");
            inputString = inputString.Trim();
            int len = inputString.Length;

            if (len == 0)
            {
                return(EcmaValue.NaN);
            }
            int charIndex = 0;
            int sign      = 1;

            if (inputString[0] == '-')
            {
                sign      = -1;
                charIndex = 1;
            }
            else if (inputString[0] == '+')
            {
                charIndex = 1;
            }
            if (charIndex == len)
            {
                return(EcmaValue.NaN);
            }
            if ((radix == 16 || radix == 0) && len > charIndex + 1)
            {
                if (inputString[charIndex] == '0' && (inputString[charIndex + 1] == 'x' || inputString[charIndex + 1] == 'X'))
                {
                    charIndex += 2;
                    radix      = 16;
                }
            }
            if (radix == 0)
            {
                radix = 10;
            }
            if (radix < 2 || radix > 36)
            {
                return(EcmaValue.NaN);
            }
            int    maxDigit  = '0' + Math.Min(radix - 1, 9);
            int    maxAlphaL = 'a' + (radix - 11);
            int    maxAlphaC = 'A' + (radix - 11);
            double m         = 0;
            int    i         = charIndex;

            for (; i < len; i++)
            {
                char ch = inputString[i];
                if (ch >= '0' && ch <= maxDigit)
                {
                    m = m * radix + (ch - '0');
                    continue;
                }
                if (ch >= 'a' && ch <= maxAlphaL)
                {
                    m = m * radix + (ch - 'a' + 10);
                    continue;
                }
                if (ch >= 'A' && ch <= maxAlphaC)
                {
                    m = m * radix + (ch - 'A' + 10);
                    continue;
                }
                if (!allowTrail)
                {
                    return(EcmaValue.NaN);
                }
                break;
            }
            if (i == charIndex)
            {
                return(EcmaValue.NaN);
            }
            if (m == 0 && sign == -1)
            {
                return(EcmaValue.NegativeZero);
            }
            if (m < Int32.MaxValue)
            {
                return((int)m * sign);
            }
            if (m < Int64.MaxValue)
            {
                return((long)m * sign);
            }
            return(m * sign);
        }
Пример #2
0
        public static Expression ConvertFromEcmaValueExpression(Expression value, Type conversionType)
        {
            Guard.ArgumentNotNull(value, "value");
            Guard.ArgumentNotNull(conversionType, "conversionType");
            if (conversionType == value.Type)
            {
                return(value);
            }
            if (conversionType.IsAssignableFrom(value.Type))
            {
                return(Expression.Convert(value, conversionType));
            }
            switch (Type.GetTypeCode(conversionType))
            {
            case TypeCode.Boolean:
                return(Expression.Call(value, "ToBoolean", Type.EmptyTypes));

            case TypeCode.Byte:
                return(Expression.Call(value, "ToUInt8", Type.EmptyTypes));

            case TypeCode.Char:
                return(Expression.Call(value, "ToChar", Type.EmptyTypes));

            case TypeCode.Double:
                return(Expression.Call(value, "ToDouble", Type.EmptyTypes));

            case TypeCode.Int16:
                return(Expression.Call(value, "ToInt16", Type.EmptyTypes));

            case TypeCode.Int32:
                return(Expression.Call(value, "ToInt32", Type.EmptyTypes));

            case TypeCode.Int64:
                return(Expression.Call(value, "ToInt64", Type.EmptyTypes));

            case TypeCode.SByte:
                return(Expression.Call(value, "ToInt8", Type.EmptyTypes));

            case TypeCode.Single:
                return(Expression.ConvertChecked(Expression.Call(value, "ToDouble", Type.EmptyTypes), typeof(float)));

            case TypeCode.String:
                return(Expression.Call(value, "ToStringOrThrow", Type.EmptyTypes));

            case TypeCode.UInt16:
                return(Expression.Call(value, "ToUInt16", Type.EmptyTypes));

            case TypeCode.UInt32:
                return(Expression.Call(value, "ToUInt32", Type.EmptyTypes));

            case TypeCode.UInt64:
                return(Expression.ConvertChecked(Expression.Call(value, "ToInt64", Type.EmptyTypes), typeof(ulong)));
            }
            if (conversionType == typeof(EcmaValue))
            {
                return(value);
            }
            if (conversionType == typeof(EcmaValue?))
            {
                return(Expression.Call(conversionType, "op_implicit", Type.EmptyTypes, value));
            }
            if (conversionType == typeof(RuntimeObject))
            {
                return(Expression.Call(value, "ToObject", Type.EmptyTypes));
            }
            if (conversionType == typeof(Symbol))
            {
                return(Expression.Call(value, "ToSymbol", Type.EmptyTypes));
            }
            if (conversionType == typeof(EcmaPropertyKey))
            {
                return(Expression.Call(typeof(EcmaPropertyKey), "FromValue", Type.EmptyTypes, value));
            }
            if (!conversionType.IsValueType)
            {
                return(Expression.Call(typeof(EcmaValueUtility), "GetUnderlyingObject", new[] { conversionType }, value));
            }
            return(Expression.Convert(Expression.Call(typeof(EcmaValueUtility), "ConvertToUnknownType", Type.EmptyTypes, value, Expression.Constant(conversionType)), conversionType));
        }