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