DateConstructor.cpp 7.79 KB
Newer Older
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 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 *  USA
 *
 */

#include "config.h"
#include "DateConstructor.h"

dglazkov@chromium.org's avatar
dglazkov@chromium.org committed
25
#include "DateConversion.h"
26
#include "DateInstance.h"
27
#include "DatePrototype.h"
28
#include "JSDateMath.h"
oliver@apple.com's avatar
oliver@apple.com committed
29
#include "JSFunction.h"
30
#include "JSGlobalObject.h"
31
#include "JSString.h"
32
#include "JSStringBuilder.h"
33 34 35 36 37
#include "ObjectPrototype.h"
#include <math.h>
#include <time.h>
#include <wtf/MathExtras.h>

38
#if OS(WINCE) && !PLATFORM(QT)
39
extern "C" time_t time(time_t* timer); // Provided by libce.
40 41
#endif

42 43 44 45 46 47 48 49
#if HAVE(SYS_TIME_H)
#include <sys/time.h>
#endif

#if HAVE(SYS_TIMEB_H)
#include <sys/timeb.h>
#endif

50
using namespace WTF;
dglazkov@chromium.org's avatar
dglazkov@chromium.org committed
51

52
namespace JSC {
53

54 55 56
static EncodedJSValue JSC_HOST_CALL dateParse(ExecState*);
static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*);
static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState*);
57

58 59 60 61 62 63
}

#include "DateConstructor.lut.h"

namespace JSC {

64
const ClassInfo DateConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::dateConstructorTable, CREATE_METHOD_TABLE(DateConstructor) };
65 66 67 68 69 70 71 72 73

/* Source for DateConstructor.lut.h
@begin dateConstructorTable
  parse     dateParse   DontEnum|Function 1
  UTC       dateUTC     DontEnum|Function 7
  now       dateNow     DontEnum|Function 0
@end
*/

74
ASSERT_HAS_TRIVIAL_DESTRUCTOR(DateConstructor);
75

76
DateConstructor::DateConstructor(JSGlobalObject* globalObject, Structure* structure)
77
    : InternalFunction(globalObject, structure) 
78
{
79 80 81 82
}

void DateConstructor::finishCreation(ExecState* exec, DatePrototype* datePrototype)
{
83
    Base::finishCreation(exec->globalData(), datePrototype->classInfo()->className);
84
    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, datePrototype, DontEnum | DontDelete | ReadOnly);
85 86
    putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(7), ReadOnly | DontEnum | DontDelete);
}
87

88
bool DateConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
89
{
90
    return getStaticFunctionSlot<InternalFunction>(exec, ExecState::dateConstructorTable(exec), jsCast<DateConstructor*>(cell), propertyName, slot);
91
}
92

93
bool DateConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
94
{
95
    return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::dateConstructorTable(exec), jsCast<DateConstructor*>(object), propertyName, descriptor);
96 97 98
}

// ECMA 15.9.3
99
JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
100
{
101 102 103 104 105
    int numArgs = args.size();

    double value;

    if (numArgs == 0) // new Date() ECMA 15.9.3.3
106
        value = jsCurrentTime();
107
    else if (numArgs == 1) {
108
        if (args.at(0).inherits(&DateInstance::s_info))
ggaren@apple.com's avatar
ggaren@apple.com committed
109
            value = asDateInstance(args.at(0))->internalNumber();
110
        else {
ggaren@apple.com's avatar
ggaren@apple.com committed
111
            JSValue primitive = args.at(0).toPrimitive(exec);
weinig@apple.com's avatar
weinig@apple.com committed
112
            if (primitive.isString())
113
                value = parseDate(exec, primitive.getString(exec));
114
            else
weinig@apple.com's avatar
weinig@apple.com committed
115
                value = primitive.toNumber(exec);
116
        }
117
    } else {
118 119 120 121 122 123 124 125 126
        double doubleArguments[7] = {
            args.at(0).toNumber(exec), 
            args.at(1).toNumber(exec), 
            args.at(2).toNumber(exec), 
            args.at(3).toNumber(exec), 
            args.at(4).toNumber(exec), 
            args.at(5).toNumber(exec), 
            args.at(6).toNumber(exec)
        };
127 128 129 130 131 132 133
        if (!isfinite(doubleArguments[0])
            || !isfinite(doubleArguments[1])
            || (numArgs >= 3 && !isfinite(doubleArguments[2]))
            || (numArgs >= 4 && !isfinite(doubleArguments[3]))
            || (numArgs >= 5 && !isfinite(doubleArguments[4]))
            || (numArgs >= 6 && !isfinite(doubleArguments[5]))
            || (numArgs >= 7 && !isfinite(doubleArguments[6])))
134
            value = QNaN;
135
        else {
136
            GregorianDateTime t;
137
            int year = JSC::toInt32(doubleArguments[0]);
138
            t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
139 140 141 142 143 144
            t.setMonth(JSC::toInt32(doubleArguments[1]));
            t.setMonthDay((numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
            t.setHour(JSC::toInt32(doubleArguments[3]));
            t.setMinute(JSC::toInt32(doubleArguments[4]));
            t.setSecond(JSC::toInt32(doubleArguments[5]));
            t.setIsDST(-1);
145
            double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
146
            value = gregorianDateTimeToMS(exec, t, ms, false);
147
        }
148
    }
149

150
    return DateInstance::create(exec, globalObject->dateStructure(), value);
151
}
152
    
153
static EncodedJSValue JSC_HOST_CALL constructWithDateConstructor(ExecState* exec)
154
{
155
    ArgList args(exec);
156
    return JSValue::encode(constructDate(exec, asInternalFunction(exec->callee())->globalObject(), args));
157
}
158

159
ConstructType DateConstructor::getConstructData(JSCell*, ConstructData& constructData)
160
{
161
    constructData.native.function = constructWithDateConstructor;
ggaren@apple.com's avatar
ggaren@apple.com committed
162
    return ConstructTypeHost;
163 164 165
}

// ECMA 15.9.2
166
static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
167
{
168 169
    GregorianDateTime ts;
    msToGregorianDateTime(exec, currentTimeMS(), false, ts);
170
    return JSValue::encode(jsNontrivialString(exec, formatDateTime(ts, DateTimeFormatDateAndTime, false)));
171 172
}

173
CallType DateConstructor::getCallData(JSCell*, CallData& callData)
174 175
{
    callData.native.function = callDate;
ggaren@apple.com's avatar
ggaren@apple.com committed
176
    return CallTypeHost;
177 178
}

179
static EncodedJSValue JSC_HOST_CALL dateParse(ExecState* exec)
180
{
181
    return JSValue::encode(jsNumber(parseDate(exec, exec->argument(0).toString(exec)->value(exec))));
182 183
}

184
static EncodedJSValue JSC_HOST_CALL dateNow(ExecState*)
185
{
186
    return JSValue::encode(jsNumber(jsCurrentTime()));
187 188
}

189
static EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec) 
190
{
191 192 193 194 195 196 197 198 199
    double doubleArguments[7] = {
        exec->argument(0).toNumber(exec), 
        exec->argument(1).toNumber(exec), 
        exec->argument(2).toNumber(exec), 
        exec->argument(3).toNumber(exec), 
        exec->argument(4).toNumber(exec), 
        exec->argument(5).toNumber(exec), 
        exec->argument(6).toNumber(exec)
    };
200
    int n = exec->argumentCount();
201 202 203 204 205 206 207
    if (isnan(doubleArguments[0])
            || isnan(doubleArguments[1])
            || (n >= 3 && isnan(doubleArguments[2]))
            || (n >= 4 && isnan(doubleArguments[3]))
            || (n >= 5 && isnan(doubleArguments[4]))
            || (n >= 6 && isnan(doubleArguments[5]))
            || (n >= 7 && isnan(doubleArguments[6])))
208
        return JSValue::encode(jsNaN());
209 210

    GregorianDateTime t;
211
    int year = JSC::toInt32(doubleArguments[0]);
212
    t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
213 214 215 216 217
    t.setMonth(JSC::toInt32(doubleArguments[1]));
    t.setMonthDay((n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
    t.setHour(JSC::toInt32(doubleArguments[3]));
    t.setMinute(JSC::toInt32(doubleArguments[4]));
    t.setSecond(JSC::toInt32(doubleArguments[5]));
218
    double ms = (n >= 7) ? doubleArguments[6] : 0;
219
    return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec, t, ms, true))));
220 221
}

222
} // namespace JSC