Esempio n. 1
0
 public CompletionOr <DateTime> ToDateTime()
 {
     return(DateTimeRangeValid
         ? Completion.NormalWithStruct(DateConstructor.Epoch.AddMilliseconds(PrimitiveValue))
         : Completion.ThrowRangeError("Date is out of range").WithEmpty <DateTime>());
 }
Esempio n. 2
0
        public static Completion toString(IValue thisValue, IReadOnlyList <IValue> arguments)
        {
            var xComp = thisNumberValue(thisValue);

            if (xComp.IsAbrupt())
            {
                return(xComp);
            }
            var x = xComp.Other !;
            int radixNumber;

            if (arguments.Count == 0)
            {
                radixNumber = 10;
            }
            else if (arguments[0] == UndefinedValue.Instance)
            {
                radixNumber = 10;
            }
            else
            {
                var comp = arguments[0].ToNumber();
                if (comp.IsAbrupt())
                {
                    return(comp);
                }
                radixNumber = (int)(comp.value as NumberValue) !.number;
            }

            if (radixNumber < 2 || radixNumber > 36)
            {
                return(Completion.ThrowRangeError("radix argument must be between 2 and 36"));
            }
            if (double.IsNaN(x.number))
            {
                return(Completion.NormalCompletion(new StringValue("NaN")));
            }

            if (x.number == 0)
            {
                return(Completion.NormalCompletion(new StringValue("0")));
            }

            if (double.IsPositiveInfinity(x.number) || x.number >= double.MaxValue)
            {
                return(Completion.NormalCompletion(new StringValue("Infinity")));
            }

            if (radixNumber == 10)
            {
                return(Completion.NormalCompletion(new StringValue(x.number.ToString(CultureInfo.InvariantCulture))));
            }

            var integer  = (long)x.number;
            var fraction = x.number - integer;

            string result = ToBase(integer, radixNumber);

            if (fraction != 0)
            {
                result += "." + ToFractionBase(fraction, radixNumber);
            }

            return(Completion.NormalCompletion(new StringValue(result)));
        }
Esempio n. 3
0
        private static Completion toExponential(IValue thisValue, IReadOnlyList <IValue> arguments)
        {
            var fractionDigits = arguments.At(0, UndefinedValue.Instance);
            var xComp          = thisNumberValue(thisValue);

            if (xComp.IsAbrupt())
            {
                return(xComp);
            }
            var x           = xComp.Other !.number;
            var fracNumComp = fractionDigits.ToNumber();

            if (fracNumComp.IsAbrupt())
            {
                return(fracNumComp);
            }
            var f = (int)(fracNumComp.value as NumberValue) !.number;

            if (fractionDigits == UndefinedValue.Instance)
            {
                f = 0;
            }
            if (double.IsNaN(x))
            {
                return(Completion.NormalCompletion(new StringValue("NaN")));
            }
            var s = "";

            if (x < 0)
            {
                s = "-";
                x = -x;
            }
            if (double.IsPositiveInfinity(x))
            {
                return(Completion.NormalCompletion(new StringValue(s + "Infinity")));
            }

            if (f < 0 || f > 100)
            {
                return(Completion.ThrowRangeError("fractionDigits argument must be between 0 and 100"));
            }
            //string m;
            //int e;
            //if (x == 0)
            //{
            //    m = new string('0', f + 1);
            //    e = 0;
            //}
            //else
            //{
            //    int n;
            //    if (fractionDigits != UndefinedValue.Instance)
            //    {

            //    }
            //    else
            //    {

            //    }
            //    m = n.ToString(CultureInfo.InvariantCulture);
            //}
            //if (f != 0)
            //{
            //    var a = m[0..1];
            //    var b = m[1..];
            //    m = $"{a}.{b}";
            //}
            //string c, d;
            //if (e == 0)
            //{
            //    c = "+";
            //    d = "0";
            //}
            //else
            //{
            //    if (e > 0)
            //        c = "+";
            //    else
            //    {
            //        c = "-";
            //        e = -e;
            //    }
            //    d = e.ToString(CultureInfo.InvariantCulture);
            //}
            //m = m + "e" + c + d;
            //return Completion.NormalCompletion(new StringValue(s + m));

            if (f > 0)
            {
                return(Completion.NormalCompletion(new StringValue(x.ToString($"0.{new string('#', f)}e+0", CultureInfo.InvariantCulture))));
            }
            return(Completion.NormalCompletion(new StringValue(x.ToString($"0.{new string('#', 100)}e+0", CultureInfo.InvariantCulture))));
        }
Esempio n. 4
0
        public override Completion InternalConstruct(IReadOnlyList <IValue> arguments, Object?newTarget)
        {
            if (newTarget == null)
            {
                newTarget = this;
            }
            var proto = Utils.GetPrototypeFromConstructor(newTarget, i => i.ArrayPrototype);

            if (proto.IsAbrupt())
            {
                return(proto);
            }

            if (arguments.Count == 0)
            {
                return(Completion.NormalCompletion(ArrayObject.ArrayCreate(0, proto.value as Object)));
            }
            else if (arguments.Count == 1)
            {
                var array = ArrayObject.ArrayCreate(0, proto.value as Object);

                int intLen;

                if (!(arguments[0] is NumberValue))
                {
                    var defineStatus = Utils.CreateDataProperty(array, "0", arguments[0]);
                    if (!defineStatus.Other)
                    {
                        throw new InvalidOperationException("CreateDataProperty [0] in array constructor failed. Spec 22.1.1.2 step 6b");
                    }
                    intLen = 1;
                }
                else
                {
                    intLen = (int)(arguments[0] as NumberValue) !.number;
                    if (intLen != (arguments[0] as NumberValue) !.number || intLen < 0)
                    {
                        return(Completion.ThrowRangeError("length argument cannot be used as a length"));
                    }
                }
                array.Set("length", new NumberValue(intLen), true);
                return(Completion.NormalCompletion(array));
            }
            else
            {
                var array = ArrayObject.ArrayCreate(arguments.Count, proto.value as Object);
                int k     = 0;
                foreach (var arg in arguments)
                {
                    var defineStatus = Utils.CreateDataProperty(array, k.ToString(System.Globalization.CultureInfo.InvariantCulture), arg);
                    if (!defineStatus.Other)
                    {
                        throw new InvalidOperationException("CreateDataProperty in array constructor failed. Spec 22.1.1.3 step 8d");
                    }
                    k++;
                }
                if ((array.Get("length").value as NumberValue) !.number != arguments.Count)
                {
                    throw new InvalidOperationException("array length is invalid. Spec 22.1.1.3 step 9");
                }
                return(Completion.NormalCompletion(array));
            }
        }