]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/DateConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / DateConstructor.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
14957cd0 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
9dae56ea
A
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
ba379fdc 25#include "DateConversion.h"
9dae56ea 26#include "DateInstance.h"
9dae56ea 27#include "DatePrototype.h"
6fe7ccc8 28#include "JSDateMath.h"
ba379fdc 29#include "JSFunction.h"
9dae56ea
A
30#include "JSGlobalObject.h"
31#include "JSString.h"
32#include "ObjectPrototype.h"
81345200 33#include "JSCInlines.h"
9dae56ea
A
34#include <math.h>
35#include <time.h>
36#include <wtf/MathExtras.h>
37
81345200
A
38#if ENABLE(WEB_REPLAY)
39#include "InputCursor.h"
40#include "JSReplayInputs.h"
41#endif
42
9dae56ea
A
43#if HAVE(SYS_TIME_H)
44#include <sys/time.h>
45#endif
46
47#if HAVE(SYS_TIMEB_H)
48#include <sys/timeb.h>
49#endif
50
ba379fdc
A
51using namespace WTF;
52
9dae56ea
A
53namespace JSC {
54
ed1e77d3
A
55EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
56EncodedJSValue JSC_HOST_CALL dateNow(ExecState*);
57EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
14957cd0
A
58
59}
60
61#include "DateConstructor.lut.h"
62
63namespace JSC {
64
ed1e77d3 65const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, &dateConstructorTable, CREATE_METHOD_TABLE(DateConstructor) };
9dae56ea 66
14957cd0
A
67/* Source for DateConstructor.lut.h
68@begin dateConstructorTable
69 parse dateParse DontEnum|Function 1
70 UTC dateUTC DontEnum|Function 7
71 now dateNow DontEnum|Function 0
72@end
73*/
9dae56ea 74
81345200
A
75#if ENABLE(WEB_REPLAY)
76static double deterministicCurrentTime(JSGlobalObject* globalObject)
9dae56ea 77{
81345200
A
78 double currentTime = jsCurrentTime();
79 InputCursor& cursor = globalObject->inputCursor();
80 if (cursor.isCapturing())
81 cursor.appendInput<GetCurrentTime>(currentTime);
82
83 if (cursor.isReplaying()) {
84 if (GetCurrentTime* input = cursor.fetchInput<GetCurrentTime>())
85 currentTime = input->currentTime();
86 }
87 return currentTime;
6fe7ccc8 88}
81345200 89#endif
6fe7ccc8 90
81345200
A
91#if ENABLE(WEB_REPLAY)
92#define NORMAL_OR_DETERMINISTIC_FUNCTION(a, b) (b)
93#else
94#define NORMAL_OR_DETERMINISTIC_FUNCTION(a, b) (a)
95#endif
96
97STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DateConstructor);
98
99DateConstructor::DateConstructor(VM& vm, Structure* structure)
100 : InternalFunction(vm, structure)
6fe7ccc8 101{
14957cd0 102}
9dae56ea 103
81345200 104void DateConstructor::finishCreation(VM& vm, DatePrototype* datePrototype)
14957cd0 105{
81345200
A
106 Base::finishCreation(vm, datePrototype->classInfo()->className);
107 putDirectWithoutTransition(vm, vm.propertyNames->prototype, datePrototype, DontEnum | DontDelete | ReadOnly);
108 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(7), ReadOnly | DontEnum | DontDelete);
14957cd0 109}
9dae56ea 110
81345200 111bool DateConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
14957cd0 112{
ed1e77d3 113 return getStaticFunctionSlot<InternalFunction>(exec, dateConstructorTable, jsCast<DateConstructor*>(object), propertyName, slot);
9dae56ea
A
114}
115
116// ECMA 15.9.3
14957cd0 117JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
9dae56ea 118{
81345200 119 VM& vm = exec->vm();
9dae56ea
A
120 int numArgs = args.size();
121
122 double value;
123
124 if (numArgs == 0) // new Date() ECMA 15.9.3.3
81345200 125 value = NORMAL_OR_DETERMINISTIC_FUNCTION(jsCurrentTime(), deterministicCurrentTime(globalObject));
9dae56ea 126 else if (numArgs == 1) {
81345200 127 if (args.at(0).inherits(DateInstance::info()))
ba379fdc 128 value = asDateInstance(args.at(0))->internalNumber();
9dae56ea 129 else {
ba379fdc 130 JSValue primitive = args.at(0).toPrimitive(exec);
9dae56ea 131 if (primitive.isString())
81345200 132 value = parseDate(vm, primitive.getString(exec));
9dae56ea
A
133 else
134 value = primitive.toNumber(exec);
135 }
136 } else {
14957cd0
A
137 double doubleArguments[7] = {
138 args.at(0).toNumber(exec),
139 args.at(1).toNumber(exec),
140 args.at(2).toNumber(exec),
141 args.at(3).toNumber(exec),
142 args.at(4).toNumber(exec),
143 args.at(5).toNumber(exec),
144 args.at(6).toNumber(exec)
145 };
81345200
A
146 if ((!std::isfinite(doubleArguments[0]) || (doubleArguments[0] > INT_MAX) || (doubleArguments[0] < INT_MIN))
147 || (!std::isfinite(doubleArguments[1]) || (doubleArguments[1] > INT_MAX) || (doubleArguments[1] < INT_MIN))
148 || (numArgs >= 3 && (!std::isfinite(doubleArguments[2]) || (doubleArguments[2] > INT_MAX) || (doubleArguments[2] < INT_MIN)))
149 || (numArgs >= 4 && (!std::isfinite(doubleArguments[3]) || (doubleArguments[3] > INT_MAX) || (doubleArguments[3] < INT_MIN)))
150 || (numArgs >= 5 && (!std::isfinite(doubleArguments[4]) || (doubleArguments[4] > INT_MAX) || (doubleArguments[4] < INT_MIN)))
151 || (numArgs >= 6 && (!std::isfinite(doubleArguments[5]) || (doubleArguments[5] > INT_MAX) || (doubleArguments[5] < INT_MIN)))
152 || (numArgs >= 7 && (!std::isfinite(doubleArguments[6]) || (doubleArguments[6] > INT_MAX) || (doubleArguments[6] < INT_MIN))))
153 value = PNaN;
9dae56ea 154 else {
f9bf01c6 155 GregorianDateTime t;
14957cd0 156 int year = JSC::toInt32(doubleArguments[0]);
93a37866
A
157 t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
158 t.setMonth(JSC::toInt32(doubleArguments[1]));
159 t.setMonthDay((numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
160 t.setHour(JSC::toInt32(doubleArguments[3]));
161 t.setMinute(JSC::toInt32(doubleArguments[4]));
162 t.setSecond(JSC::toInt32(doubleArguments[5]));
163 t.setIsDST(-1);
14957cd0 164 double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
ed1e77d3 165 value = gregorianDateTimeToMS(vm, t, ms, WTF::LocalTime);
9dae56ea
A
166 }
167 }
168
81345200 169 return DateInstance::create(vm, globalObject->dateStructure(), value);
9dae56ea
A
170}
171
14957cd0 172static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
9dae56ea 173{
14957cd0
A
174 ArgList args(exec);
175 return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
176}
177
6fe7ccc8 178ConstructType DateConstructor::getConstructData(JSCell*, ConstructData& constructData)
9dae56ea
A
179{
180 constructData.native.function = constructWithDateConstructor;
181 return ConstructTypeHost;
182}
183
184// ECMA 15.9.2
14957cd0 185static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
9dae56ea 186{
81345200 187 VM& vm = exec->vm();
6fe7ccc8 188 GregorianDateTime ts;
ed1e77d3 189 msToGregorianDateTime(vm, currentTimeMS(), WTF::LocalTime, ts);
81345200 190 return JSValue::encode(jsNontrivialString(&vm, formatDateTime(ts, DateTimeFormatDateAndTime, false)));
9dae56ea
A
191}
192
6fe7ccc8 193CallType DateConstructor::getCallData(JSCell*, CallData& callData)
9dae56ea
A
194{
195 callData.native.function = callDate;
196 return CallTypeHost;
197}
198
ed1e77d3 199EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
9dae56ea 200{
81345200 201 return JSValue::encode(jsNumber(parseDate(exec->vm(), exec->argument(0).toString(exec)->value(exec))));
9dae56ea
A
202}
203
ed1e77d3 204EncodedJSValue JSC_HOST_CALL dateNow(ExecState* exec)
9dae56ea 205{
81345200
A
206#if !ENABLE(WEB_REPLAY)
207 UNUSED_PARAM(exec);
208#endif
209
210 return JSValue::encode(jsNumber(NORMAL_OR_DETERMINISTIC_FUNCTION(jsCurrentTime(), deterministicCurrentTime(exec->lexicalGlobalObject()))));
9dae56ea
A
211}
212
ed1e77d3 213EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec)
9dae56ea 214{
14957cd0
A
215 double doubleArguments[7] = {
216 exec->argument(0).toNumber(exec),
217 exec->argument(1).toNumber(exec),
218 exec->argument(2).toNumber(exec),
219 exec->argument(3).toNumber(exec),
220 exec->argument(4).toNumber(exec),
221 exec->argument(5).toNumber(exec),
222 exec->argument(6).toNumber(exec)
223 };
224 int n = exec->argumentCount();
81345200
A
225 if ((std::isnan(doubleArguments[0]) || (doubleArguments[0] > INT_MAX) || (doubleArguments[0] < INT_MIN))
226 || (std::isnan(doubleArguments[1]) || (doubleArguments[1] > INT_MAX) || (doubleArguments[1] < INT_MIN))
227 || (n >= 3 && (std::isnan(doubleArguments[2]) || (doubleArguments[2] > INT_MAX) || (doubleArguments[2] < INT_MIN)))
228 || (n >= 4 && (std::isnan(doubleArguments[3]) || (doubleArguments[3] > INT_MAX) || (doubleArguments[3] < INT_MIN)))
229 || (n >= 5 && (std::isnan(doubleArguments[4]) || (doubleArguments[4] > INT_MAX) || (doubleArguments[4] < INT_MIN)))
230 || (n >= 6 && (std::isnan(doubleArguments[5]) || (doubleArguments[5] > INT_MAX) || (doubleArguments[5] < INT_MIN)))
231 || (n >= 7 && (std::isnan(doubleArguments[6]) || (doubleArguments[6] > INT_MAX) || (doubleArguments[6] < INT_MIN))))
14957cd0 232 return JSValue::encode(jsNaN());
9dae56ea
A
233
234 GregorianDateTime t;
14957cd0 235 int year = JSC::toInt32(doubleArguments[0]);
93a37866
A
236 t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
237 t.setMonth(JSC::toInt32(doubleArguments[1]));
238 t.setMonthDay((n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
239 t.setHour(JSC::toInt32(doubleArguments[3]));
240 t.setMinute(JSC::toInt32(doubleArguments[4]));
241 t.setSecond(JSC::toInt32(doubleArguments[5]));
14957cd0 242 double ms = (n >= 7) ? doubleArguments[6] : 0;
ed1e77d3 243 return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec->vm(), t, ms, WTF::UTCTime))));
9dae56ea
A
244}
245
246} // namespace JSC