1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2008-2018 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 USA
18 *
19 */
20
21#pragma once
22
23#include "JSDestructibleObject.h"
24
25namespace JSC {
26
27class DateInstance final : public JSDestructibleObject {
28protected:
29 JS_EXPORT_PRIVATE DateInstance(VM&, Structure*);
30 void finishCreation(VM&);
31 JS_EXPORT_PRIVATE void finishCreation(VM&, double);
32
33 JS_EXPORT_PRIVATE static void destroy(JSCell*);
34
35public:
36 using Base = JSDestructibleObject;
37
38 static DateInstance* create(VM& vm, Structure* structure, double date)
39 {
40 DateInstance* instance = new (NotNull, allocateCell<DateInstance>(vm.heap)) DateInstance(vm, structure);
41 instance->finishCreation(vm, date);
42 return instance;
43 }
44
45 static DateInstance* create(VM& vm, Structure* structure)
46 {
47 DateInstance* instance = new (NotNull, allocateCell<DateInstance>(vm.heap)) DateInstance(vm, structure);
48 instance->finishCreation(vm);
49 return instance;
50 }
51
52 double internalNumber() const { return m_internalNumber; }
53 void setInternalNumber(double value) { m_internalNumber = value; }
54
55 DECLARE_EXPORT_INFO;
56
57 const GregorianDateTime* gregorianDateTime(ExecState* exec) const
58 {
59 if (m_data && m_data->m_gregorianDateTimeCachedForMS == internalNumber())
60 return &m_data->m_cachedGregorianDateTime;
61 return calculateGregorianDateTime(exec);
62 }
63
64 const GregorianDateTime* gregorianDateTimeUTC(ExecState* exec) const
65 {
66 if (m_data && m_data->m_gregorianDateTimeUTCCachedForMS == internalNumber())
67 return &m_data->m_cachedGregorianDateTimeUTC;
68 return calculateGregorianDateTimeUTC(exec);
69 }
70
71 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
72 {
73 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
74 }
75
76private:
77 JS_EXPORT_PRIVATE const GregorianDateTime* calculateGregorianDateTime(ExecState*) const;
78 JS_EXPORT_PRIVATE const GregorianDateTime* calculateGregorianDateTimeUTC(ExecState*) const;
79
80 double m_internalNumber { PNaN };
81 mutable RefPtr<DateInstanceData> m_data;
82};
83
84} // namespace JSC
85