]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/DateConstructor.cpp
JavaScriptCore-1218.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"
4e4e5a6f 32#include "JSStringBuilder.h"
9dae56ea 33#include "ObjectPrototype.h"
93a37866 34#include "Operations.h"
9dae56ea
A
35#include <math.h>
36#include <time.h>
37#include <wtf/MathExtras.h>
38
f9bf01c6
A
39#if OS(WINCE) && !PLATFORM(QT)
40extern "C" time_t time(time_t* timer); // Provided by libce.
ba379fdc
A
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
14957cd0
A
55static EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
56static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*);
57static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
58
59}
60
61#include "DateConstructor.lut.h"
62
63namespace JSC {
64
6fe7ccc8 65const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::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
6fe7ccc8 75ASSERT_HAS_TRIVIAL_DESTRUCTOR(DateConstructor);
14957cd0 76
6fe7ccc8
A
77DateConstructor::DateConstructor(JSGlobalObject* globalObject, Structure* structure)
78 : InternalFunction(globalObject, structure)
9dae56ea 79{
6fe7ccc8
A
80}
81
82void DateConstructor::finishCreation(ExecState* exec, DatePrototype* datePrototype)
83{
93a37866
A
84 Base::finishCreation(exec->vm(), datePrototype->classInfo()->className);
85 putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, datePrototype, DontEnum | DontDelete | ReadOnly);
86 putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(7), ReadOnly | DontEnum | DontDelete);
14957cd0 87}
9dae56ea 88
93a37866 89bool DateConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
14957cd0 90{
6fe7ccc8 91 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::dateConstructorTable(exec), jsCast<DateConstructor*>(cell), propertyName, slot);
14957cd0 92}
9dae56ea 93
93a37866 94bool DateConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
14957cd0 95{
6fe7ccc8 96 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::dateConstructorTable(exec), jsCast<DateConstructor*>(object), propertyName, descriptor);
9dae56ea
A
97}
98
99// ECMA 15.9.3
14957cd0 100JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
9dae56ea
A
101{
102 int numArgs = args.size();
103
104 double value;
105
106 if (numArgs == 0) // new Date() ECMA 15.9.3.3
f9bf01c6 107 value = jsCurrentTime();
9dae56ea 108 else if (numArgs == 1) {
14957cd0 109 if (args.at(0).inherits(&DateInstance::s_info))
ba379fdc 110 value = asDateInstance(args.at(0))->internalNumber();
9dae56ea 111 else {
ba379fdc 112 JSValue primitive = args.at(0).toPrimitive(exec);
9dae56ea 113 if (primitive.isString())
f9bf01c6 114 value = parseDate(exec, primitive.getString(exec));
9dae56ea
A
115 else
116 value = primitive.toNumber(exec);
117 }
118 } else {
14957cd0
A
119 double doubleArguments[7] = {
120 args.at(0).toNumber(exec),
121 args.at(1).toNumber(exec),
122 args.at(2).toNumber(exec),
123 args.at(3).toNumber(exec),
124 args.at(4).toNumber(exec),
125 args.at(5).toNumber(exec),
126 args.at(6).toNumber(exec)
127 };
93a37866
A
128 if (!std::isfinite(doubleArguments[0])
129 || !std::isfinite(doubleArguments[1])
130 || (numArgs >= 3 && !std::isfinite(doubleArguments[2]))
131 || (numArgs >= 4 && !std::isfinite(doubleArguments[3]))
132 || (numArgs >= 5 && !std::isfinite(doubleArguments[4]))
133 || (numArgs >= 6 && !std::isfinite(doubleArguments[5]))
134 || (numArgs >= 7 && !std::isfinite(doubleArguments[6])))
135 value = QNaN;
9dae56ea 136 else {
f9bf01c6 137 GregorianDateTime t;
14957cd0 138 int year = JSC::toInt32(doubleArguments[0]);
93a37866
A
139 t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
140 t.setMonth(JSC::toInt32(doubleArguments[1]));
141 t.setMonthDay((numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
142 t.setHour(JSC::toInt32(doubleArguments[3]));
143 t.setMinute(JSC::toInt32(doubleArguments[4]));
144 t.setSecond(JSC::toInt32(doubleArguments[5]));
145 t.setIsDST(-1);
14957cd0 146 double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
f9bf01c6 147 value = gregorianDateTimeToMS(exec, t, ms, false);
9dae56ea
A
148 }
149 }
150
6fe7ccc8 151 return DateInstance::create(exec, globalObject->dateStructure(), value);
9dae56ea
A
152}
153
14957cd0 154static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
9dae56ea 155{
14957cd0
A
156 ArgList args(exec);
157 return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args));
9dae56ea
A
158}
159
6fe7ccc8 160ConstructType DateConstructor::getConstructData(JSCell*, ConstructData& constructData)
9dae56ea
A
161{
162 constructData.native.function = constructWithDateConstructor;
163 return ConstructTypeHost;
164}
165
166// ECMA 15.9.2
14957cd0 167static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
9dae56ea 168{
6fe7ccc8
A
169 GregorianDateTime ts;
170 msToGregorianDateTime(exec, currentTimeMS(), false, ts);
93a37866 171 return JSValue::encode(jsNontrivialString(exec, formatDateTime(ts, DateTimeFormatDateAndTime, false)));
9dae56ea
A
172}
173
6fe7ccc8 174CallType DateConstructor::getCallData(JSCell*, CallData& callData)
9dae56ea
A
175{
176 callData.native.function = callDate;
177 return CallTypeHost;
178}
179
14957cd0 180static EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
9dae56ea 181{
6fe7ccc8 182 return JSValue::encode(jsNumber(parseDate(exec, exec->argument(0).toString(exec)->value(exec))));
9dae56ea
A
183}
184
14957cd0 185static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*)
9dae56ea 186{
14957cd0 187 return JSValue::encode(jsNumber(jsCurrentTime()));
9dae56ea
A
188}
189
14957cd0 190static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec)
9dae56ea 191{
14957cd0
A
192 double doubleArguments[7] = {
193 exec->argument(0).toNumber(exec),
194 exec->argument(1).toNumber(exec),
195 exec->argument(2).toNumber(exec),
196 exec->argument(3).toNumber(exec),
197 exec->argument(4).toNumber(exec),
198 exec->argument(5).toNumber(exec),
199 exec->argument(6).toNumber(exec)
200 };
201 int n = exec->argumentCount();
93a37866
A
202 if (std::isnan(doubleArguments[0])
203 || std::isnan(doubleArguments[1])
204 || (n >= 3 && std::isnan(doubleArguments[2]))
205 || (n >= 4 && std::isnan(doubleArguments[3]))
206 || (n >= 5 && std::isnan(doubleArguments[4]))
207 || (n >= 6 && std::isnan(doubleArguments[5]))
208 || (n >= 7 && std::isnan(doubleArguments[6])))
14957cd0 209 return JSValue::encode(jsNaN());
9dae56ea
A
210
211 GregorianDateTime t;
14957cd0 212 int year = JSC::toInt32(doubleArguments[0]);
93a37866
A
213 t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
214 t.setMonth(JSC::toInt32(doubleArguments[1]));
215 t.setMonthDay((n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
216 t.setHour(JSC::toInt32(doubleArguments[3]));
217 t.setMinute(JSC::toInt32(doubleArguments[4]));
218 t.setSecond(JSC::toInt32(doubleArguments[5]));
14957cd0
A
219 double ms = (n >= 7) ? doubleArguments[6] : 0;
220 return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec, t, ms, true))));
9dae56ea
A
221}
222
223} // namespace JSC