aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4dateobject_p.h
blob: 7debcff4e98c127e77bfe8c6c76655aa1c5c2fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QV4DATEOBJECT_P_H
#define QV4DATEOBJECT_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qv4object_p.h"
#include "qv4functionobject_p.h"
#include "qv4referenceobject_p.h"
#include <QtCore/private/qnumeric_p.h>
#include <QtCore/qdatetime.h>

QT_BEGIN_NAMESPACE

class QDateTime;

namespace QV4 {

struct Date
{
    static constexpr quint64 MaxDateVal = 8.64e15;

    void init() { storage = InvalidDateVal; }
    void init(double value);
    void init(const QDateTime &dateTime);
    void init(QDate date);
    void init(QTime time, ExecutionEngine *engine);

    Date &operator=(double value)
    {
        storage = (storage & (HasQDate | HasQTime)) | encode(value);
        return *this;
    }

    operator double() const
    {
        const quint64 raw = (storage & ~(HasQDate | HasQTime));
        if (raw == 0)
            return qt_qnan();

        if (raw > MaxDateVal)
            return double(raw - MaxDateVal - Extra);

        return double(raw) - double(MaxDateVal) - double(Extra);
    }

    QDate toQDate() const;
    QTime toQTime() const;
    QDateTime toQDateTime() const;
    QVariant toVariant() const;

    template<typename Function>
    bool withStoragePointer(Function function)
    {
        switch (storage & (HasQDate | HasQTime)) {
        case HasQDate: {
            QDate date = toQDate();
            return function(&date);
        }
        case HasQTime: {
            QTime time = toQTime();
            return function(&time);
        }
        case (HasQTime | HasQDate): {
            QDateTime dateTime = toQDateTime();
            return function(&dateTime);
        }
        default:
            return false;
        }
    }

private:
    static constexpr quint64 InvalidDateVal = 0;
    static constexpr quint64 Extra = 1;
    static constexpr quint64 HasQDate = 1ull << 63;
    static constexpr quint64 HasQTime = 1ull << 62;

    // Make all our dates fit into quint64, leaving space for the flags
    static_assert(((MaxDateVal * 2 + Extra) & (HasQDate | HasQTime)) == 0ull);

    static quint64 encode(double value);
    static quint64 encode(const QDateTime &dateTime);

    quint64 storage;
};

namespace Heap {

#define DateObjectMembers(class, Member)
DECLARE_HEAP_OBJECT(DateObject, ReferenceObject) {
    DECLARE_MARKOBJECTS(DateObject);

    void doSetLocation()
    {
        if (CppStackFrame *frame = internalClass->engine->currentStackFrame)
            setLocation(frame->v4Function, frame->statementNumber());
    }

    void init()
    {
        ReferenceObject::init(nullptr, -1, {});
        m_date.init();
    }

    void init(double dateTime)
    {
        ReferenceObject::init(nullptr, -1, {});
        m_date.init(dateTime);
    }

    void init(const QDateTime &dateTime)
    {
        ReferenceObject::init(nullptr, -1, {});
        m_date.init(dateTime);
    }

    void init(const QDateTime &dateTime, Heap::Object *parent, int property, Flags flags)
    {
        ReferenceObject::init(parent, property, flags | EnforcesLocation);
        doSetLocation();
        m_date.init(dateTime);
    };

    void init(QDate date, Heap::Object *parent, int property, Flags flags)
    {
        ReferenceObject::init(parent, property, flags | EnforcesLocation);
        doSetLocation();
        m_date.init(date);
    };

    void init(QTime time, Heap::Object *parent, int property, Flags flags)
    {
        ReferenceObject::init(parent, property, flags | EnforcesLocation);
        doSetLocation();
        m_date.init(time, internalClass->engine);
    };

    void setDate(double newDate)
    {
        m_date = newDate;
        if (isAttachedToProperty())
            writeBack();
    }

    double date() const
    {
        return m_date;
    }

    QVariant toVariant() const { return m_date.toVariant(); }
    QDateTime toQDateTime() const { return m_date.toQDateTime(); }

private:
    bool writeBack()
    {
        if (!object() || !canWriteBack())
            return false;

        QV4::Scope scope(internalClass->engine);
        QV4::ScopedObject o(scope, object());

        int flags = 0;
        int status = -1;
        if (isVariant()) {
            QVariant variant = toVariant();
            void *a[] = { &variant, nullptr, &status, &flags };
            return o->metacall(QMetaObject::WriteProperty, property(), a);
        }

        return m_date.withStoragePointer([&](void *storagePointer) {
            void *a[] = { storagePointer, nullptr, &status, &flags };
            return o->metacall(QMetaObject::WriteProperty, property(), a);
        });
    }

    Date m_date;
};


struct DateCtor : FunctionObject {
    void init(QV4::ExecutionContext *scope);
};

}

struct DateObject: ReferenceObject {
    V4_OBJECT2(DateObject, ReferenceObject)
    Q_MANAGED_TYPE(DateObject)
    V4_PROTOTYPE(datePrototype)

    void setDate(double date) { d()->setDate(date); }
    double date() const { return d()->date(); }

    Q_QML_EXPORT QDateTime toQDateTime() const;
    QString toString() const;

    static QString dateTimeToString(const QDateTime &dateTime, ExecutionEngine *engine);
    static double dateTimeToNumber(const QDateTime &dateTime);
    static QDate dateTimeToDate(const QDateTime &dateTime);
    static QDateTime stringToDateTime(const QString &string, ExecutionEngine *engine);
    static QDateTime timestampToDateTime(double timestamp, QTimeZone zone = QTimeZone::LocalTime);
    static double componentsToTimestamp(
            double year, double month, double day,
            double hours, double mins, double secs, double ms,
            ExecutionEngine *v4);
};

template<>
inline const DateObject *Value::as() const {
    return isManaged() && m()->internalClass->vtable->type == Managed::Type_DateObject ? static_cast<const DateObject *>(this) : nullptr;
}

struct DateCtor: FunctionObject
{
    V4_OBJECT2(DateCtor, FunctionObject)

    static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv, int argc, const Value *);
    static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int);
};

struct DatePrototype: Object
{
    V4_PROTOTYPE(objectPrototype)

    void init(ExecutionEngine *engine, Object *ctor);

    static double getThisDate(ExecutionEngine *v4, const Value *thisObject);

    static ReturnedValue method_parse(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_UTC(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_now(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);

    static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toDateString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toTimeString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toLocaleString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toLocaleDateString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toLocaleTimeString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_valueOf(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getTime(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getYear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getFullYear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCFullYear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getMonth(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCMonth(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getDate(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCDate(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getDay(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCDay(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getHours(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCHours(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getMinutes(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCMinutes(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getSeconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCSeconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getMilliseconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getUTCMilliseconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_getTimezoneOffset(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setTime(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setMilliseconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCMilliseconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setSeconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCSeconds(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setMinutes(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCMinutes(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setHours(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCHours(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setDate(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCDate(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setMonth(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCMonth(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setYear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setFullYear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_setUTCFullYear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toUTCString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toISOString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_toJSON(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
    static ReturnedValue method_symbolToPrimitive(const FunctionObject *f, const Value *thisObject, const Value *, int);

    static void timezoneUpdated(ExecutionEngine *e);
};

}

QT_END_NAMESPACE

#endif // QV4ECMAOBJECTS_P_H