ToPrimitive() private method

private ToPrimitive ( Object value, PreferredType preferredType ) : Object
value Object
preferredType PreferredType
return Object
Exemplo n.º 1
0
 private static Object DoOp(Object v1, Object v2, IConvertible ic1, IConvertible ic2, JSToken operatorTok)
 {
     if (operatorTok == JSToken.Minus)
     {
         IConvertible ic1a = ic1;
         Object       p1   = Convert.ToPrimitive(v1, PreferredType.Either, ref ic1a);
         TypeCode     t1   = Convert.GetTypeCode(p1, ic1a);
         if (t1 == TypeCode.Char)
         {
             IConvertible ic2a = ic2;
             Object       p2   = Convert.ToPrimitive(v2, PreferredType.Either, ref ic2a);
             TypeCode     t2   = Convert.GetTypeCode(p2, ic2a);
             if (t2 == TypeCode.String)
             {
                 String str2 = ic2a.ToString(null);
                 if (str2.Length == 1)
                 {
                     t2   = TypeCode.Char;
                     p2   = str2[0];
                     ic2a = Convert.GetIConvertible(p2);
                 }
             }
             Object result = NumericBinary.DoOp(Convert.ToNumber(p1, ic1a), Convert.ToNumber(p2, ic2a), operatorTok);
             if (t2 != TypeCode.Char)
             {
                 result = Convert.Coerce2(result, TypeCode.Char, false);
             }
             return(result);
         }
     }
     return(NumericBinary.DoOp(Convert.ToNumber(v1, ic1), Convert.ToNumber(v2, ic2), operatorTok));
 }
Exemplo n.º 2
0
 // Evaluate a plus operator on two values.
 public static Object DoOp(Object v1, Object v2)
 {
     v1 = Convert.ToPrimitive(v1, DefaultValueHint.None);
     v2 = Convert.ToPrimitive(v2, DefaultValueHint.None);
     if (v1 is String || v2 is String)
     {
         return(((String)v1) + ((String)v2));
     }
     else
     {
         return(Convert.ToNumber(v1) + Convert.ToNumber(v2));
     }
 }
Exemplo n.º 3
0
 // Compare two values.
 public static double JScriptCompare(Object v1, Object v2)
 {
     v1 = Convert.ToPrimitive(v1, DefaultValueHint.None);
     v2 = Convert.ToPrimitive(v2, DefaultValueHint.None);
     if (v1 is String && v2 is String)
     {
         return(String.CompareOrdinal((String)v1, (String)v2));
     }
     else
     {
         return(Convert.ToNumber(v1) - Convert.ToNumber(v2));
     }
 }
Exemplo n.º 4
0
        public object EvaluatePlus(object v1, object v2)
        {
            object val1 = Convert.ToPrimitive(v1, null);
            object val2 = Convert.ToPrimitive(v2, null);

            if (Convert.IsString(val1) || Convert.IsString(val2))
            {
                return(Convert.ToString(val1) + Convert.ToString(val2));
            }
            else
            {
                return(Convert.ToNumber(val1) + Convert.ToNumber(val2));
            }
        }
Exemplo n.º 5
0
        public new DateObject CreateInstance(params Object[] args)
        {
            if (args.Length == 0)
            {
                return(new DateObject(this.originalPrototype, DateTime.Now.ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970));
            }
            if (args.Length == 1)
            {
                Object       arg0  = args[0];
                IConvertible ic    = Convert.GetIConvertible(arg0);
                TypeCode     tcode = Convert.GetTypeCode(arg0, ic);
                if (tcode == TypeCode.DateTime)
                {
                    return(new DateObject(this.originalPrototype, ic.ToDateTime(null).ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970));
                }
                Object yv = Convert.ToPrimitive(arg0, PreferredType.Either, ref ic);
                if (Convert.GetTypeCode(yv, ic) == TypeCode.String)
                {
                    return(new DateObject(this.originalPrototype, parse(ic.ToString(null))));
                }
                double d = Convert.ToNumber(yv, ic);
                if (DatePrototype.minDate <= d && d <= DatePrototype.maxDate)
                {
                    return(new DateObject(this.originalPrototype, d));
                }
                return(new DateObject(this.originalPrototype, Double.NaN));
            }
            double dyear    = Convert.ToNumber(args[0]);
            double dmonth   = Convert.ToNumber(args[1]);
            double ddate    = (args.Length > 2) ? Convert.ToNumber(args[2]) : 1;
            double dhours   = (args.Length > 3) ? Convert.ToNumber(args[3]) : 0;
            double dminutes = (args.Length > 4) ? Convert.ToNumber(args[4]) : 0;
            double dseconds = (args.Length > 5) ? Convert.ToNumber(args[5]) : 0;
            double dms      = (args.Length > 6) ? Convert.ToNumber(args[6]) : 0;
            int    y        = (int)dyear;

            if (!Double.IsNaN(dyear) && 0 <= y && y <= 99)
            {
                dyear = y + 1900;
            }
            double day  = DatePrototype.MakeDay(dyear, dmonth, ddate);
            double time = DatePrototype.MakeTime(dhours, dminutes, dseconds, dms);

            return(new DateObject(this.originalPrototype, DatePrototype.TimeClip(DatePrototype.UTC(DatePrototype.MakeDate(day, time)))));
        }
Exemplo n.º 6
0
        public static double JScriptCompare(object v1, object v2)
        {
            object p1 = Convert.ToPrimitive(v1, null);
            object p2 = Convert.ToPrimitive(v2, null);

            if (Convert.IsString(p1) && Convert.IsString(p2))
            {
                string s1 = Convert.ToString(p1);
                string s2 = Convert.ToString(p2);
                return(s1.CompareTo(s2));
            }
            else
            {
                double n1 = Convert.ToNumber(p1);
                double n2 = Convert.ToNumber(p2);
                return(n1 - n2);
            }
        }
Exemplo n.º 7
0
        public static Object DoOp(Object v1, Object v2)
        {
            IConvertible ic1 = Convert.GetIConvertible(v1);
            IConvertible ic2 = Convert.GetIConvertible(v2);

            v1 = Convert.ToPrimitive(v1, PreferredType.Either, ref ic1);
            v2 = Convert.ToPrimitive(v2, PreferredType.Either, ref ic2);
            TypeCode t1 = Convert.GetTypeCode(v1, ic1);
            TypeCode t2 = Convert.GetTypeCode(v2, ic2);

            if (t1 == TypeCode.String)
            {
                if (v1 is ConcatString)
                {
                    return(new ConcatString((ConcatString)v1, Convert.ToString(v2, ic2)));
                }
                else
                {
                    return(new ConcatString(ic1.ToString(null), Convert.ToString(v2, ic2)));
                }
            }
            else if (t2 == TypeCode.String)
            {
                return(Convert.ToString(v1, ic1) + ic2.ToString(null));
            }
            else if (t1 == TypeCode.Char && t2 == TypeCode.Char)
            {
                return(ic1.ToString(null) + ic2.ToString(null));
            }
            else if ((t1 == TypeCode.Char && (Convert.IsPrimitiveNumericTypeCode(t2) || t2 == TypeCode.Boolean)) ||
                     (t2 == TypeCode.Char && (Convert.IsPrimitiveNumericTypeCode(t1) || t1 == TypeCode.Boolean)))
            {
                return((char)(int)Runtime.DoubleToInt64(Convert.ToNumber(v1, ic1) + Convert.ToNumber(v2, ic2)));
            }
            else
            {
                return(Convert.ToNumber(v1, ic1) + Convert.ToNumber(v2, ic2));
            }
        }
Exemplo n.º 8
0
        private static double JScriptCompare2(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2)
        {
            if (t1 == TypeCode.Object)
            {
                v1 = Convert.ToPrimitive(v1, PreferredType.Number, ref ic1);
                t1 = Convert.GetTypeCode(v1, ic1);
            }
            if (t2 == TypeCode.Object)
            {
                v2 = Convert.ToPrimitive(v2, PreferredType.Number, ref ic2);
                t2 = Convert.GetTypeCode(v2, ic2);
            }
            switch (t1)
            {
            case TypeCode.Char:
                if (t2 == TypeCode.String)
                {
                    return(String.CompareOrdinal(Convert.ToString(v1, ic1), ic2.ToString(null)));
                }
                goto case TypeCode.UInt16;

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
                long l = ic1.ToInt64(null);
                switch (t2)
                {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(l - ic2.ToInt64(null));

                case TypeCode.UInt64:
                    if (l < 0)
                    {
                        return(-1);
                    }
                    ulong ul2 = ic2.ToUInt64(null);
                    if (((ulong)l) < ul2)
                    {
                        return(-1);
                    }
                    if (((ulong)l) == ul2)
                    {
                        return(0);
                    }
                    return(1);

                case TypeCode.Single:
                case TypeCode.Double:
                    return(((double)l) - ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return((double)(new Decimal(l) - ic2.ToDecimal(null)));

                default:
                    Object bd2 = Convert.ToNumber(v2, ic2);
                    return(JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double));
                }

            case TypeCode.UInt64:
                ulong ul = ic1.ToUInt64(null);
                switch (t2)
                {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    long l2 = ic2.ToInt64(null);
                    if (l2 < 0)
                    {
                        return(1);
                    }
                    if (ul == (ulong)l2)
                    {
                        return(0);
                    }
                    return(-1);

                case TypeCode.UInt64:
                    ulong ul2 = ic2.ToUInt64(null);
                    if (ul < ul2)
                    {
                        return(-1);
                    }
                    if (ul == ul2)
                    {
                        return(0);
                    }
                    return(1);

                case TypeCode.Single:
                case TypeCode.Double:
                    return(((double)ul) - ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return((double)(new Decimal(ul) - ic2.ToDecimal(null)));

                default:
                    Object bd2 = Convert.ToNumber(v2, ic2);
                    return(JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double));
                }

            case TypeCode.Decimal:
                Decimal dec1 = ic1.ToDecimal(null);
                switch (t2)
                {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return((double)(dec1 - new Decimal(ic2.ToInt64(null))));

                case TypeCode.UInt64:
                    return((double)(dec1 - new Decimal(ic2.ToUInt64(null))));

                case TypeCode.Single:
                case TypeCode.Double:
                    return((double)(dec1 - new Decimal(ic2.ToDouble(null))));

                case TypeCode.Decimal:
                    return((double)(dec1 - ic2.ToDecimal(null)));

                default:
                    return((double)(dec1 - new Decimal(Convert.ToNumber(v2, ic2))));
                }

            case TypeCode.String:
                switch (t2)
                {
                case TypeCode.String: return(String.CompareOrdinal(ic1.ToString(null), ic2.ToString(null)));

                case TypeCode.Char: return(String.CompareOrdinal(ic1.ToString(null), Convert.ToString(v2, ic2)));
                }
                goto default;

            default:
                double d1 = Convert.ToNumber(v1, ic1);
                double d2 = Convert.ToNumber(v2, ic2);
                if (d1 == d2)
                {
                    return(0);      //d1 and d2 could be infinities
                }
                return(d1 - d2);
            }
        }
Exemplo n.º 9
0
        public new DateObject CreateInstance(params object [] args)
        {
            int    argc = args.Length;
            double year = 0, month = 0, date = 1, hours = 0, minutes = 0, seconds = 0, ms = 0;
            double result;

            if (argc == 0)
            {
                DateTime now = DateTime.Now;
                year    = now.Year;
                month   = now.Month - 1;
                date    = now.Day;
                hours   = now.Hour;
                minutes = now.Minute;
                seconds = now.Second;
                ms      = now.Millisecond;
                goto done;
            }
            else if (argc == 1)
            {
                object value = Convert.ToPrimitive(args [0], null);
                if (value.GetType() == typeof(string))
                {
                    result = parse((string)value);
                }
                else
                {
                    result = Convert.ToNumber(value);
                    if (Double.IsNaN(result) || Double.IsInfinity(result))
                    {
                        result = Double.NaN;
                    }
                    else
                    {
                        result = Math.Round(result);
                    }
                }
                return(new DateObject(result));
            }

            year = Convert.ToNumber(args [0]);
            --argc;
            month = Convert.ToNumber(args [1]);
            if (--argc == 0)
            {
                goto done;
            }
            date = Convert.ToNumber(args [2]);
            if (--argc == 0)
            {
                goto done;
            }
            hours = Convert.ToNumber(args [3]);
            if (--argc == 0)
            {
                goto done;
            }
            minutes = Convert.ToNumber(args [4]);
            if (--argc == 0)
            {
                goto done;
            }
            seconds = Convert.ToNumber(args [5]);
            if (--argc == 0)
            {
                goto done;
            }
            ms = Convert.ToNumber(args [6]);

done:
            if (!Double.IsNaN(year) && year >= 0 && year <= 99)
            {
                year += 1900;
            }

            double utc_off = 0;

            try {
                DateTime dt = new DateTime((int)year, (int)month + 1, (int)date,
                                           (int)hours, (int)minutes, (int)seconds);
                utc_off = TimeZone.CurrentTimeZone.GetUtcOffset(dt).TotalMilliseconds;
            } catch (ArgumentOutOfRangeException) { /* OK */ }

            result = msec_from_date(year, month, date, hours, minutes, seconds, ms - utc_off);
            return(new DateObject(result));
        }
Exemplo n.º 10
0
        public bool EvaluateEquality(object v1, object v2)
        {
            IConvertible ic1 = v1 as IConvertible;
            IConvertible ic2 = v2 as IConvertible;

            TypeCode tc1 = Convert.GetTypeCode(v1, ic1);
            TypeCode tc2 = Convert.GetTypeCode(v2, ic2);

            bool both_numbers = Convert.IsNumberTypeCode(tc1) && Convert.IsNumberTypeCode(tc2);

            if ((tc1 == tc2) || both_numbers)
            {
                switch (tc1)
                {
                case TypeCode.DBNull:
                case TypeCode.Empty:
                    return(true);

                case TypeCode.Boolean:
                    return(ic1.ToBoolean(null) == ic2.ToBoolean(null));

                case TypeCode.String:
                    return(ic1.ToString(null) == ic2.ToString(null));

                case TypeCode.Object:
                    if (v1 is ScriptFunction && v2 is ScriptFunction)
                    {
                        return(v1 == v2 || v1.Equals(v2));
                    }
                    else
                    {
                        return(v1 == v2);
                    }

                default:
                    if (both_numbers)
                    {
                        double num1;
                        if (Convert.IsFloatTypeCode(tc1))
                        {
                            num1 = ic1.ToDouble(null);
                        }
                        else
                        {
                            num1 = (double)ic1.ToInt64(null);
                        }

                        double num2;
                        if (Convert.IsFloatTypeCode(tc2))
                        {
                            num2 = ic2.ToDouble(null);
                        }
                        else
                        {
                            num2 = (double)ic2.ToInt64(null);
                        }

                        return(num1 == num2);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                bool swapped = false;

redo:
                switch (tc1)
                {
                case TypeCode.DBNull:
                    if (tc2 == TypeCode.Empty)
                    {
                        return(true);
                    }
                    break;

                case TypeCode.String:
                    if (Convert.IsNumberTypeCode(tc2))
                    {
                        return(EvaluateEquality(Convert.ToNumber(v1), v2));
                    }
                    break;

                case TypeCode.Boolean:
                    return(EvaluateEquality(Convert.ToNumber(v1), v2));

                case TypeCode.Object:
                    if (tc2 == TypeCode.String || Convert.IsNumberTypeCode(tc2))
                    {
                        return(EvaluateEquality(Convert.ToPrimitive(v1, null), v2));
                    }
                    break;
                }

                if (!swapped)
                {
                    swapped = true;

                    object vt = v1;
                    v1 = v2;
                    v2 = vt;

                    ic1 = v1 as IConvertible;
                    ic2 = v2 as IConvertible;

                    tc1 = Convert.GetTypeCode(v1, ic1);
                    tc2 = Convert.GetTypeCode(v2, ic2);

                    goto redo;
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 11
0
        private static bool JScriptEquals(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects)
        {
            if (StrictEquality.JScriptStrictEquals(v1, v2, ic1, ic2, t1, t2, checkForDebuggerObjects))
            {
                return(true);
            }
            if (t2 == TypeCode.Boolean)
            {
                v2  = ic2.ToBoolean(null) ? 1 : 0;
                ic2 = Convert.GetIConvertible(v2);
                return(Equality.JScriptEquals(v1, v2, ic1, ic2, t1, TypeCode.Int32, false));
            }
            switch (t1)
            {
            case TypeCode.Empty: return(t2 == TypeCode.Empty || t2 == TypeCode.DBNull || (t2 == TypeCode.Object && v2 is Missing));

            case TypeCode.Object:
                switch (t2)
                {
                case TypeCode.Empty:
                case TypeCode.DBNull:
                    return(v1 is Missing);

                case TypeCode.Char:
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.String:
                    IConvertible pvic1 = ic1;
                    Object       pv1   = Convert.ToPrimitive(v1, PreferredType.Either, ref pvic1);
                    if (pvic1 != null && pv1 != v1)
                    {
                        return(Equality.JScriptEquals(pv1, v2, pvic1, ic2, pvic1.GetTypeCode(), t2, false));
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(false);

            case TypeCode.DBNull: return(t2 == TypeCode.DBNull || t2 == TypeCode.Empty || (t2 == TypeCode.Object && v2 is Missing));

            case TypeCode.Boolean:
                v1  = ic1.ToBoolean(null) ? 1 : 0;
                ic1 = Convert.GetIConvertible(v1);
                return(Equality.JScriptEquals(v1, v2, ic1, ic2, TypeCode.Int32, t2, false));

            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                if (t2 == TypeCode.Object)
                {
                    IConvertible pvic2 = ic2;
                    Object       pv2   = Convert.ToPrimitive(v2, PreferredType.Either, ref pvic2);
                    if (pvic2 != null && pv2 != v2)
                    {
                        return(Equality.JScriptEquals(v1, pv2, ic1, pvic2, t1, pvic2.GetTypeCode(), false));
                    }
                    else
                    {
                        return(false);
                    }
                }
                if (t2 == TypeCode.String)
                {
                    if (v1 is Enum)
                    {
                        return(Convert.ToString(v1).Equals(ic2.ToString(null)));
                    }
                    v2  = Convert.ToNumber(v2, ic2);
                    ic2 = Convert.GetIConvertible(v2);
                    return(StrictEquality.JScriptStrictEquals(v1, v2, ic1, ic2, t1, TypeCode.Double, false));
                }
                return(false);

            case TypeCode.DateTime:
                if (t2 == TypeCode.Object)
                {
                    IConvertible pvic2 = ic2;
                    Object       pv2   = Convert.ToPrimitive(v2, PreferredType.Either, ref pvic2);
                    if (pv2 != null && pv2 != v2)
                    {
                        return(StrictEquality.JScriptStrictEquals(v1, pv2, ic1, pvic2, t1, pvic2.GetTypeCode(), false));
                    }
                }
                return(false);

            case TypeCode.String:
                switch (t2)
                {
                case TypeCode.Object: {
                    IConvertible pvic2 = ic2;
                    Object       pv2   = Convert.ToPrimitive(v2, PreferredType.Either, ref pvic2);
                    if (pvic2 != null && pv2 != v2)
                    {
                        return(Equality.JScriptEquals(v1, pv2, ic1, pvic2, t1, pvic2.GetTypeCode(), false));
                    }
                    else
                    {
                        return(false);
                    }
                }

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    if (v2 is Enum)
                    {
                        return(Convert.ToString(v2).Equals(ic1.ToString(null)));
                    }
                    v1  = Convert.ToNumber(v1, ic1);
                    ic1 = Convert.GetIConvertible(v1);
                    return(StrictEquality.JScriptStrictEquals(v1, v2, ic1, ic2, TypeCode.Double, t2, false));
                }
                return(false);
            }
            return(false);
        }