示例#1
0
        private double ConstructTimeValue(JsValue[] arguments, bool useUtc)
        {
            if (arguments.Length < 2)
            {
                throw new ArgumentOutOfRangeException("arguments", "There must be at least two arguments.");
            }

            var y     = TypeConverter.ToNumber(arguments[0]);
            var m     = (int)TypeConverter.ToInteger(arguments[1]);
            var dt    = arguments.Length > 2 ? (int)TypeConverter.ToInteger(arguments[2]) : 1;
            var h     = arguments.Length > 3 ? (int)TypeConverter.ToInteger(arguments[3]) : 0;
            var min   = arguments.Length > 4 ? (int)TypeConverter.ToInteger(arguments[4]) : 0;
            var s     = arguments.Length > 5 ? (int)TypeConverter.ToInteger(arguments[5]) : 0;
            var milli = arguments.Length > 6 ? (int)TypeConverter.ToInteger(arguments[6]) : 0;

            for (int i = 2; i < arguments.Length; i++)
            {
                if (double.IsNaN(TypeConverter.ToNumber(arguments[i])))
                {
                    return(double.NaN);
                }
            }

            if ((!double.IsNaN(y)) && (0 <= TypeConverter.ToInteger(y)) && (TypeConverter.ToInteger(y) <= 99))
            {
                y += 1900;
            }

            var finalDate = DatePrototype.MakeDate(DatePrototype.MakeDay(y, m, dt),
                                                   DatePrototype.MakeTime(h, min, s, milli));

            return(useUtc ? finalDate : PrototypeObject.Utc(finalDate));
        }
示例#2
0
        public static DatePrototype CreatePrototypeObject(Engine engine, DateConstructor dateConstructor)
        {
            var obj = new DatePrototype(engine)
            {
                _prototype       = engine.Object.PrototypeObject,
                _dateConstructor = dateConstructor
            };

            return(obj);
        }
示例#3
0
 internal DateConstructor(
     Engine engine,
     Realm realm,
     FunctionPrototype functionPrototype,
     ObjectPrototype objectPrototype)
     : base(engine, realm, _functionName)
 {
     _prototype           = functionPrototype;
     PrototypeObject      = new DatePrototype(engine, realm, this, objectPrototype);
     _length              = new PropertyDescriptor(7, PropertyFlag.Configurable);
     _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
 }
示例#4
0
        public static DatePrototype CreatePrototypeObject(Engine engine, DateConstructor dateConstructor)
        {
            var obj = new DatePrototype(engine)
            {
                Prototype = engine.Object.PrototypeObject, 
                Extensible = true,
                PrimitiveValue = double.NaN
            };

            obj.FastAddProperty("constructor", dateConstructor, true, false, true);

            return obj;
        }
        public static DatePrototype CreatePrototypeObject(Engine engine, DateConstructor dateConstructor)
        {
            var obj = new DatePrototype(engine)
            {
                Prototype      = engine.Object.PrototypeObject,
                Extensible     = true,
                PrimitiveValue = double.NaN
            };

            obj.FastAddProperty("constructor", dateConstructor, true, false, true);

            return(obj);
        }
示例#6
0
        /// <summary>
        /// https://tc39.es/ecma262/#sec-date
        /// </summary>
        private ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
        {
            double dv;

            if (arguments.Length == 0 || newTarget.IsUndefined())
            {
                dv = FromDateTime(DateTime.UtcNow);
            }
            else if (arguments.Length == 1)
            {
                if (arguments[0] is DateInstance date)
                {
                    return(Construct(date.DateValue));
                }

                var v = TypeConverter.ToPrimitive(arguments[0]);
                if (v.IsString())
                {
                    return(Construct(((JsNumber)Parse(Undefined, Arguments.From(v)))._value));
                }

                dv = TimeClip(TypeConverter.ToNumber(v));
            }
            else
            {
                var y     = TypeConverter.ToNumber(arguments.At(0));
                var m     = TypeConverter.ToNumber(arguments.At(1));
                var dt    = TypeConverter.ToNumber(arguments.At(2, JsNumber.PositiveOne));
                var h     = TypeConverter.ToNumber(arguments.At(3, JsNumber.PositiveZero));
                var min   = TypeConverter.ToNumber(arguments.At(4, JsNumber.PositiveZero));
                var s     = TypeConverter.ToNumber(arguments.At(5, JsNumber.PositiveZero));
                var milli = TypeConverter.ToNumber(arguments.At(6, JsNumber.PositiveZero));

                var yInteger = TypeConverter.ToInteger(y);
                if (!double.IsNaN(y) && 0 <= yInteger && yInteger <= 99)
                {
                    y += 1900;
                }

                var finalDate = DatePrototype.MakeDate(
                    DatePrototype.MakeDay(y, m, dt),
                    DatePrototype.MakeTime(h, min, s, milli));

                dv = TimeClip(PrototypeObject.Utc(finalDate));
            }

            var o = OrdinaryCreateFromConstructor(
                newTarget,
                static intrinsics => intrinsics.Date.PrototypeObject,
示例#7
0
        public static DateConstructor CreateDateConstructor(Engine engine)
        {
            var obj = new DateConstructor(engine);

            obj.Extensible = true;

            // The value of the [[Prototype]] internal property of the Date constructor is the Function prototype object
            obj.Prototype       = engine.Function.PrototypeObject;
            obj.PrototypeObject = DatePrototype.CreatePrototypeObject(engine, obj);

            obj.FastAddProperty("length", 7, false, false, false);

            // The initial value of Date.prototype is the Date prototype object
            obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);

            return(obj);
        }
示例#8
0
        public static DateConstructor CreateDateConstructor(Engine engine)
        {
            var obj = new DateConstructor(engine)
            {
                _prototype = engine.Function.PrototypeObject
            };

            // The value of the [[Prototype]] internal property of the Date constructor is the Function prototype object
            obj.PrototypeObject = DatePrototype.CreatePrototypeObject(engine, obj);

            obj._length = new PropertyDescriptor(7, PropertyFlag.Configurable);

            // The initial value of Date.prototype is the Date prototype object
            obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);

            return(obj);
        }
示例#9
0
        /// <summary>
        /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.3
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public ObjectInstance Construct(JsValue[] arguments)
        {
            if (arguments.Length == 0)
            {
                return(Construct(DateTime.Now));
            }
            else if (arguments.Length == 1)
            {
                var v = TypeConverter.ToPrimitive(arguments[0]);
                if (v.IsString())
                {
                    return(Parse(Undefined.Instance, Arguments.From(v)).AsObject());
                }

                return(Construct(DatePrototype.TimeClip(TypeConverter.ToNumber(v))));
            }
            else
            {
                var y     = TypeConverter.ToNumber(arguments[0]);
                var m     = (int)TypeConverter.ToInteger(arguments[1]);
                var dt    = arguments.Length > 2 ? (int)TypeConverter.ToInteger(arguments[2]) : 1;
                var h     = arguments.Length > 3 ? (int)TypeConverter.ToInteger(arguments[3]) : 0;
                var min   = arguments.Length > 4 ? (int)TypeConverter.ToInteger(arguments[4]) : 0;
                var s     = arguments.Length > 5 ? (int)TypeConverter.ToInteger(arguments[5]) : 0;
                var milli = arguments.Length > 6 ? (int)TypeConverter.ToInteger(arguments[6]) : 0;

                for (int i = 2; i < arguments.Length; i++)
                {
                    if (double.IsNaN(TypeConverter.ToNumber(arguments[i])))
                    {
                        return(Construct(double.NaN));
                    }
                }

                if ((!double.IsNaN(y)) && (0 <= TypeConverter.ToInteger(y)) && (TypeConverter.ToInteger(y) <= 99))
                {
                    y += 1900;
                }

                var finalDate = DatePrototype.MakeDate(DatePrototype.MakeDay(y, m, dt),
                                                       DatePrototype.MakeTime(h, min, s, milli));

                return(Construct(DatePrototype.TimeClip(DatePrototype.Utc(finalDate))));
            }
        }
示例#10
0
        private JsValue Utc(JsValue thisObj, JsValue[] arguments)
        {
            var y     = TypeConverter.ToNumber(arguments.At(0));
            var m     = TypeConverter.ToNumber(arguments.At(1, JsNumber.PositiveZero));
            var dt    = TypeConverter.ToNumber(arguments.At(2, JsNumber.One));
            var h     = TypeConverter.ToNumber(arguments.At(3, JsNumber.PositiveZero));
            var min   = TypeConverter.ToNumber(arguments.At(4, JsNumber.PositiveZero));
            var s     = TypeConverter.ToNumber(arguments.At(5, JsNumber.PositiveZero));
            var milli = TypeConverter.ToNumber(arguments.At(6, JsNumber.PositiveZero));

            var yInteger = TypeConverter.ToInteger(y);

            if (!double.IsNaN(y) && 0 <= yInteger && yInteger <= 99)
            {
                y = yInteger + 1900;
            }

            var finalDate = DatePrototype.MakeDate(
                DatePrototype.MakeDay(y, m, dt),
                DatePrototype.MakeTime(h, min, s, milli));

            return(TimeClip(finalDate));
        }