DateConstructor.cpp 7.56 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
#include "ObjectPrototype.h"
34
#include "Operations.h"
35 36 37 38
#include <math.h>
#include <time.h>
#include <wtf/MathExtras.h>

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

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

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

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

53
namespace JSC {
54

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

59 60 61 62 63 64
}

#include "DateConstructor.lut.h"

namespace JSC {

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

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

75
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(DateConstructor);
76

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

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

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

94
// ECMA 15.9.3
95
JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
96
{
97
    VM& vm = exec->vm();
98 99 100 101 102
    int numArgs = args.size();

    double value;

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

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

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

// ECMA 15.9.2
163
static EncodedJSValue JSC_HOST_CALL callDate(ExecState* exec)
164
{
165
    VM& vm = exec->vm();
166
    GregorianDateTime ts;
167 168
    msToGregorianDateTime(vm, currentTimeMS(), false, ts);
    return JSValue::encode(jsNontrivialString(&vm, formatDateTime(ts, DateTimeFormatDateAndTime, false)));
169 170
}

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

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

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

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

    GregorianDateTime t;
209
    int year = JSC::toInt32(doubleArguments[0]);
210
    t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
211 212 213 214 215
    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]));
216
    double ms = (n >= 7) ? doubleArguments[6] : 0;
217
    return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec->vm(), t, ms, true))));
218 219
}

220
} // namespace JSC