]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
3 | * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011 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 ENABLE(WEB_REPLAY) | |
39 | #include "InputCursor.h" | |
40 | #include "JSReplayInputs.h" | |
41 | #endif | |
42 | ||
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 | ||
51 | using namespace WTF; | |
52 | ||
53 | namespace JSC { | |
54 | ||
55 | EncodedJSValue JSC_HOST_CALL dateParse(ExecState*); | |
56 | EncodedJSValue JSC_HOST_CALL dateNow(ExecState*); | |
57 | EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*); | |
58 | ||
59 | } | |
60 | ||
61 | #include "DateConstructor.lut.h" | |
62 | ||
63 | namespace JSC { | |
64 | ||
65 | const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, &dateConstructorTable, CREATE_METHOD_TABLE(DateConstructor) }; | |
66 | ||
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 | */ | |
74 | ||
75 | #if ENABLE(WEB_REPLAY) | |
76 | static double deterministicCurrentTime(JSGlobalObject* globalObject) | |
77 | { | |
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; | |
88 | } | |
89 | #endif | |
90 | ||
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 | ||
97 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DateConstructor); | |
98 | ||
99 | DateConstructor::DateConstructor(VM& vm, Structure* structure) | |
100 | : InternalFunction(vm, structure) | |
101 | { | |
102 | } | |
103 | ||
104 | void DateConstructor::finishCreation(VM& vm, DatePrototype* datePrototype) | |
105 | { | |
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); | |
109 | } | |
110 | ||
111 | bool DateConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot) | |
112 | { | |
113 | return getStaticFunctionSlot<InternalFunction>(exec, dateConstructorTable, jsCast<DateConstructor*>(object), propertyName, slot); | |
114 | } | |
115 | ||
116 | // ECMA 15.9.3 | |
117 | JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args) | |
118 | { | |
119 | VM& vm = exec->vm(); | |
120 | int numArgs = args.size(); | |
121 | ||
122 | double value; | |
123 | ||
124 | if (numArgs == 0) // new Date() ECMA 15.9.3.3 | |
125 | value = NORMAL_OR_DETERMINISTIC_FUNCTION(jsCurrentTime(), deterministicCurrentTime(globalObject)); | |
126 | else if (numArgs == 1) { | |
127 | if (args.at(0).inherits(DateInstance::info())) | |
128 | value = asDateInstance(args.at(0))->internalNumber(); | |
129 | else { | |
130 | JSValue primitive = args.at(0).toPrimitive(exec); | |
131 | if (primitive.isString()) | |
132 | value = parseDate(vm, primitive.getString(exec)); | |
133 | else | |
134 | value = primitive.toNumber(exec); | |
135 | } | |
136 | } else { | |
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 | }; | |
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; | |
154 | else { | |
155 | GregorianDateTime t; | |
156 | int year = JSC::toInt32(doubleArguments[0]); | |
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); | |
164 | double ms = (numArgs >= 7) ? doubleArguments[6] : 0; | |
165 | value = gregorianDateTimeToMS(vm, t, ms, WTF::LocalTime); | |
166 | } | |
167 | } | |
168 | ||
169 | return DateInstance::create(vm, globalObject->dateStructure(), value); | |
170 | } | |
171 | ||
172 | static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec) | |
173 | { | |
174 | ArgList args(exec); | |
175 | return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args)); | |
176 | } | |
177 | ||
178 | ConstructType DateConstructor::getConstructData(JSCell*, ConstructData& constructData) | |
179 | { | |
180 | constructData.native.function = constructWithDateConstructor; | |
181 | return ConstructTypeHost; | |
182 | } | |
183 | ||
184 | // ECMA 15.9.2 | |
185 | static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec) | |
186 | { | |
187 | VM& vm = exec->vm(); | |
188 | GregorianDateTime ts; | |
189 | msToGregorianDateTime(vm, currentTimeMS(), WTF::LocalTime, ts); | |
190 | return JSValue::encode(jsNontrivialString(&vm, formatDateTime(ts, DateTimeFormatDateAndTime, false))); | |
191 | } | |
192 | ||
193 | CallType DateConstructor::getCallData(JSCell*, CallData& callData) | |
194 | { | |
195 | callData.native.function = callDate; | |
196 | return CallTypeHost; | |
197 | } | |
198 | ||
199 | EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec) | |
200 | { | |
201 | return JSValue::encode(jsNumber(parseDate(exec->vm(), exec->argument(0).toString(exec)->value(exec)))); | |
202 | } | |
203 | ||
204 | EncodedJSValue JSC_HOST_CALL dateNow(ExecState* exec) | |
205 | { | |
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())))); | |
211 | } | |
212 | ||
213 | EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec) | |
214 | { | |
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(); | |
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)))) | |
232 | return JSValue::encode(jsNaN()); | |
233 | ||
234 | GregorianDateTime t; | |
235 | int year = JSC::toInt32(doubleArguments[0]); | |
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])); | |
242 | double ms = (n >= 7) ? doubleArguments[6] : 0; | |
243 | return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec->vm(), t, ms, WTF::UTCTime)))); | |
244 | } | |
245 | ||
246 | } // namespace JSC |