1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2004-2018 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 * USA
19 *
20 */
21
22#include "config.h"
23#include "DateConstructor.h"
24
25#include "DateConversion.h"
26#include "DateInstance.h"
27#include "DatePrototype.h"
28#include "JSDateMath.h"
29#include "JSFunction.h"
30#include "JSGlobalObject.h"
31#include "JSString.h"
32#include "ObjectPrototype.h"
33#include "JSCInlines.h"
34#include <math.h>
35#include <time.h>
36#include <wtf/MathExtras.h>
37
38#if HAVE(SYS_TIME_H)
39#include <sys/time.h>
40#endif
41
42#if HAVE(SYS_TIMEB_H)
43#include <sys/timeb.h>
44#endif
45
46namespace JSC {
47
48EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
49EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
50
51}
52
53#include "DateConstructor.lut.h"
54
55namespace JSC {
56
57using namespace WTF;
58
59const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, &dateConstructorTable, nullptr, CREATE_METHOD_TABLE(DateConstructor) };
60
61/* Source for DateConstructor.lut.h
62@begin dateConstructorTable
63 parse dateParse DontEnum|Function 1
64 UTC dateUTC DontEnum|Function 7
65 now dateNow DontEnum|Function 0
66@end
67*/
68
69STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DateConstructor);
70
71static EncodedJSValue JSC_HOST_CALL callDate(ExecState*);
72static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState*);
73
74DateConstructor::DateConstructor(VM& vm, Structure* structure)
75 : InternalFunction(vm, structure, callDate, constructWithDateConstructor)
76{
77}
78
79void DateConstructor::finishCreation(VM& vm, DatePrototype* datePrototype)
80{
81 Base::finishCreation(vm, vm.propertyNames->Date.string(), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
82 putDirectWithoutTransition(vm, vm.propertyNames->prototype, datePrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
83 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(7), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
84}
85
86static double millisecondsFromComponents(ExecState* exec, const ArgList& args, WTF::TimeType timeType)
87{
88 VM& vm = exec->vm();
89 auto scope = DECLARE_THROW_SCOPE(vm);
90
91 // Initialize doubleArguments with default values.
92 double doubleArguments[7] {
93 0, 0, 1, 0, 0, 0, 0
94 };
95 unsigned numberOfUsedArguments = std::max(std::min<unsigned>(7U, args.size()), 1U);
96 for (unsigned i = 0; i < numberOfUsedArguments; ++i) {
97 doubleArguments[i] = args.at(i).toNumber(exec);
98 RETURN_IF_EXCEPTION(scope, 0);
99 }
100 for (unsigned i = 0; i < numberOfUsedArguments; ++i) {
101 if (!std::isfinite(doubleArguments[i]) || (doubleArguments[i] > INT_MAX) || (doubleArguments[i] < INT_MIN))
102 return PNaN;
103 }
104
105 GregorianDateTime t;
106 int year = JSC::toInt32(doubleArguments[0]);
107 t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
108 t.setMonth(JSC::toInt32(doubleArguments[1]));
109 t.setMonthDay(JSC::toInt32(doubleArguments[2]));
110 t.setHour(JSC::toInt32(doubleArguments[3]));
111 t.setMinute(JSC::toInt32(doubleArguments[4]));
112 t.setSecond(JSC::toInt32(doubleArguments[5]));
113 t.setIsDST(-1);
114 return gregorianDateTimeToMS(vm, t, doubleArguments[6], timeType);
115}
116
117// ECMA 15.9.3
118JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, JSValue newTarget, const ArgList& args)
119{
120 VM& vm = exec->vm();
121 auto scope = DECLARE_THROW_SCOPE(vm);
122 int numArgs = args.size();
123
124 double value;
125
126 if (numArgs == 0) // new Date() ECMA 15.9.3.3
127 value = jsCurrentTime();
128 else if (numArgs == 1) {
129 JSValue arg0 = args.at(0);
130 if (auto* dateInstance = jsDynamicCast<DateInstance*>(vm, arg0))
131 value = dateInstance->internalNumber();
132 else {
133 JSValue primitive = arg0.toPrimitive(exec);
134 RETURN_IF_EXCEPTION(scope, nullptr);
135 if (primitive.isString()) {
136 value = parseDate(exec, vm, asString(primitive)->value(exec));
137 RETURN_IF_EXCEPTION(scope, nullptr);
138 } else
139 value = primitive.toNumber(exec);
140 }
141 } else
142 value = millisecondsFromComponents(exec, args, WTF::LocalTime);
143 RETURN_IF_EXCEPTION(scope, nullptr);
144
145 Structure* dateStructure = InternalFunction::createSubclassStructure(exec, newTarget, globalObject->dateStructure());
146 RETURN_IF_EXCEPTION(scope, nullptr);
147
148 return DateInstance::create(vm, dateStructure, value);
149}
150
151static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
152{
153 ArgList args(exec);
154 return JSValue::encode(constructDate(exec, jsCast<InternalFunction*>(exec->jsCallee())->globalObject(exec->vm()), exec->newTarget(), args));
155}
156
157// ECMA 15.9.2
158static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
159{
160 VM& vm = exec->vm();
161 GregorianDateTime ts;
162 msToGregorianDateTime(vm, WallTime::now().secondsSinceEpoch().milliseconds(), WTF::LocalTime, ts);
163 return JSValue::encode(jsNontrivialString(&vm, formatDateTime(ts, DateTimeFormatDateAndTime, false)));
164}
165
166EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
167{
168 VM& vm = exec->vm();
169 auto scope = DECLARE_THROW_SCOPE(vm);
170 String dateStr = exec->argument(0).toWTFString(exec);
171 RETURN_IF_EXCEPTION(scope, encodedJSValue());
172 RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(parseDate(exec, vm, dateStr))));
173}
174
175EncodedJSValue JSC_HOST_CALL dateNow(ExecState*)
176{
177 return JSValue::encode(jsNumber(jsCurrentTime()));
178}
179
180EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec)
181{
182 double ms = millisecondsFromComponents(exec, ArgList(exec), WTF::UTCTime);
183 return JSValue::encode(jsNumber(timeClip(ms)));
184}
185
186} // namespace JSC
187