• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2004, 2005, 2006, 2007, 2008 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 "DateInstance.h"
26 #include "DateMath.h"
27 #include "DatePrototype.h"
28 #include "JSGlobalObject.h"
29 #include "JSString.h"
30 #include "ObjectPrototype.h"
31 #include "PrototypeFunction.h"
32 #include <math.h>
33 #include <time.h>
34 #include <wtf/MathExtras.h>
35 
36 #if HAVE(SYS_TIME_H)
37 #include <sys/time.h>
38 #endif
39 
40 #if HAVE(SYS_TIMEB_H)
41 #include <sys/timeb.h>
42 #endif
43 
44 namespace JSC {
45 
46 // TODO: MakeTime (15.9.11.1) etc. ?
47 
48 ASSERT_CLASS_FITS_IN_CELL(DateConstructor);
49 
50 static JSValuePtr dateParse(ExecState*, JSObject*, JSValuePtr, const ArgList&);
51 static JSValuePtr dateNow(ExecState*, JSObject*, JSValuePtr, const ArgList&);
52 static JSValuePtr dateUTC(ExecState*, JSObject*, JSValuePtr, const ArgList&);
53 
DateConstructor(ExecState * exec,PassRefPtr<Structure> structure,Structure * prototypeFunctionStructure,DatePrototype * datePrototype)54 DateConstructor::DateConstructor(ExecState* exec, PassRefPtr<Structure> structure, Structure* prototypeFunctionStructure, DatePrototype* datePrototype)
55     : InternalFunction(&exec->globalData(), structure, Identifier(exec, datePrototype->classInfo()->className))
56 {
57       putDirectWithoutTransition(exec->propertyNames().prototype, datePrototype, DontEnum|DontDelete|ReadOnly);
58 
59       putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 1, exec->propertyNames().parse, dateParse), DontEnum);
60       putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 7, exec->propertyNames().UTC, dateUTC), DontEnum);
61       putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().now, dateNow), DontEnum);
62 
63       putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 7), ReadOnly | DontEnum | DontDelete);
64 }
65 
66 // ECMA 15.9.3
constructDate(ExecState * exec,const ArgList & args)67 JSObject* constructDate(ExecState* exec, const ArgList& args)
68 {
69     int numArgs = args.size();
70 
71     double value;
72 
73     if (numArgs == 0) // new Date() ECMA 15.9.3.3
74         value = getCurrentUTCTime();
75     else if (numArgs == 1) {
76         if (args.at(exec, 0).isObject(&DateInstance::info))
77             value = asDateInstance(args.at(exec, 0))->internalNumber();
78         else {
79             JSValuePtr primitive = args.at(exec, 0).toPrimitive(exec);
80             if (primitive.isString())
81                 value = parseDate(primitive.getString());
82             else
83                 value = primitive.toNumber(exec);
84         }
85     } else {
86         if (isnan(args.at(exec, 0).toNumber(exec))
87                 || isnan(args.at(exec, 1).toNumber(exec))
88                 || (numArgs >= 3 && isnan(args.at(exec, 2).toNumber(exec)))
89                 || (numArgs >= 4 && isnan(args.at(exec, 3).toNumber(exec)))
90                 || (numArgs >= 5 && isnan(args.at(exec, 4).toNumber(exec)))
91                 || (numArgs >= 6 && isnan(args.at(exec, 5).toNumber(exec)))
92                 || (numArgs >= 7 && isnan(args.at(exec, 6).toNumber(exec))))
93             value = NaN;
94         else {
95           GregorianDateTime t;
96           int year = args.at(exec, 0).toInt32(exec);
97           t.year = (year >= 0 && year <= 99) ? year : year - 1900;
98           t.month = args.at(exec, 1).toInt32(exec);
99           t.monthDay = (numArgs >= 3) ? args.at(exec, 2).toInt32(exec) : 1;
100           t.hour = args.at(exec, 3).toInt32(exec);
101           t.minute = args.at(exec, 4).toInt32(exec);
102           t.second = args.at(exec, 5).toInt32(exec);
103           t.isDST = -1;
104           double ms = (numArgs >= 7) ? args.at(exec, 6).toNumber(exec) : 0;
105           value = gregorianDateTimeToMS(t, ms, false);
106         }
107     }
108 
109     DateInstance* result = new (exec) DateInstance(exec->lexicalGlobalObject()->dateStructure());
110     result->setInternalValue(jsNumber(exec, timeClip(value)));
111     return result;
112 }
113 
constructWithDateConstructor(ExecState * exec,JSObject *,const ArgList & args)114 static JSObject* constructWithDateConstructor(ExecState* exec, JSObject*, const ArgList& args)
115 {
116     return constructDate(exec, args);
117 }
118 
getConstructData(ConstructData & constructData)119 ConstructType DateConstructor::getConstructData(ConstructData& constructData)
120 {
121     constructData.native.function = constructWithDateConstructor;
122     return ConstructTypeHost;
123 }
124 
125 // ECMA 15.9.2
callDate(ExecState * exec,JSObject *,JSValuePtr,const ArgList &)126 static JSValuePtr callDate(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
127 {
128     time_t localTime = time(0);
129     tm localTM;
130     getLocalTime(&localTime, &localTM);
131     GregorianDateTime ts(localTM);
132     return jsNontrivialString(exec, formatDate(ts) + " " + formatTime(ts, false));
133 }
134 
getCallData(CallData & callData)135 CallType DateConstructor::getCallData(CallData& callData)
136 {
137     callData.native.function = callDate;
138     return CallTypeHost;
139 }
140 
dateParse(ExecState * exec,JSObject *,JSValuePtr,const ArgList & args)141 static JSValuePtr dateParse(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
142 {
143     return jsNumber(exec, parseDate(args.at(exec, 0).toString(exec)));
144 }
145 
dateNow(ExecState * exec,JSObject *,JSValuePtr,const ArgList &)146 static JSValuePtr dateNow(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
147 {
148     return jsNumber(exec, getCurrentUTCTime());
149 }
150 
dateUTC(ExecState * exec,JSObject *,JSValuePtr,const ArgList & args)151 static JSValuePtr dateUTC(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
152 {
153     int n = args.size();
154     if (isnan(args.at(exec, 0).toNumber(exec))
155             || isnan(args.at(exec, 1).toNumber(exec))
156             || (n >= 3 && isnan(args.at(exec, 2).toNumber(exec)))
157             || (n >= 4 && isnan(args.at(exec, 3).toNumber(exec)))
158             || (n >= 5 && isnan(args.at(exec, 4).toNumber(exec)))
159             || (n >= 6 && isnan(args.at(exec, 5).toNumber(exec)))
160             || (n >= 7 && isnan(args.at(exec, 6).toNumber(exec))))
161         return jsNaN(exec);
162 
163     GregorianDateTime t;
164     int year = args.at(exec, 0).toInt32(exec);
165     t.year = (year >= 0 && year <= 99) ? year : year - 1900;
166     t.month = args.at(exec, 1).toInt32(exec);
167     t.monthDay = (n >= 3) ? args.at(exec, 2).toInt32(exec) : 1;
168     t.hour = args.at(exec, 3).toInt32(exec);
169     t.minute = args.at(exec, 4).toInt32(exec);
170     t.second = args.at(exec, 5).toInt32(exec);
171     double ms = (n >= 7) ? args.at(exec, 6).toNumber(exec) : 0;
172     return jsNumber(exec, gregorianDateTimeToMS(t, ms, true));
173 }
174 
175 } // namespace JSC
176