1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | #ifndef QV4PROPERTYKEY_H |
40 | #define QV4PROPERTYKEY_H |
41 | |
42 | // |
43 | // W A R N I N G |
44 | // ------------- |
45 | // |
46 | // This file is not part of the Qt API. It exists purely as an |
47 | // implementation detail. This header file may change from version to |
48 | // version without notice, or even be removed. |
49 | // |
50 | // We mean it. |
51 | // |
52 | |
53 | #include <private/qv4global_p.h> |
54 | |
55 | QT_BEGIN_NAMESPACE |
56 | |
57 | class QString; |
58 | |
59 | namespace QV4 { |
60 | |
61 | struct PropertyKey |
62 | { |
63 | private: |
64 | // Property keys are Strings, Symbols or unsigned integers. |
65 | // For convenience we derive them from Values, allowing us to store them |
66 | // on the JS stack |
67 | // |
68 | // They do however behave somewhat different than a Value: |
69 | // * If the key is a String, the pointer to the string is stored in the identifier |
70 | // table and thus unique. |
71 | // * If the key is a Symbol it simply points to the referenced symbol object |
72 | // * if the key is an array index (a uint < UINT_MAX), it's encoded as an |
73 | // integer value |
74 | quint64 val; |
75 | |
76 | // Important: Always keep this in sync with the definitions for Integers and heap objects in Value |
77 | static const quint64 ArrayIndexMask = 0x3800000000000ull; |
78 | enum { |
79 | IsManagedOrUndefined_Shift = 64-15, |
80 | }; |
81 | inline bool isManaged() const { return (val >> IsManagedOrUndefined_Shift) == 0; } |
82 | inline quint32 value() const { return val & quint64(~quint32(0)); } |
83 | |
84 | #if QT_POINTER_SIZE == 8 |
85 | QML_NEARLY_ALWAYS_INLINE Heap::StringOrSymbol *m() const |
86 | { |
87 | Heap::StringOrSymbol *b; |
88 | memcpy(dest: &b, src: &val, n: 8); |
89 | return b; |
90 | } |
91 | QML_NEARLY_ALWAYS_INLINE void setM(Heap::StringOrSymbol *b) |
92 | { |
93 | memcpy(dest: &val, src: &b, n: 8); |
94 | } |
95 | #elif QT_POINTER_SIZE == 4 |
96 | QML_NEARLY_ALWAYS_INLINE Heap::StringOrSymbol *m() const |
97 | { |
98 | Q_STATIC_ASSERT(sizeof(Heap::StringOrSymbol*) == sizeof(quint32)); |
99 | Heap::StringOrSymbol *b; |
100 | quint32 v = value(); |
101 | memcpy(&b, &v, 4); |
102 | return b; |
103 | } |
104 | QML_NEARLY_ALWAYS_INLINE void setM(Heap::StringOrSymbol *b) |
105 | { |
106 | quint32 v; |
107 | memcpy(&v, &b, 4); |
108 | val = v; |
109 | } |
110 | #endif |
111 | |
112 | public: |
113 | static PropertyKey invalid() { PropertyKey key; key.val = 0; return key; } |
114 | static PropertyKey fromArrayIndex(uint idx) { PropertyKey key; key.val = ArrayIndexMask | static_cast<quint64>(idx); return key; } |
115 | bool isStringOrSymbol() const { return isManaged() && val != 0; } |
116 | uint asArrayIndex() const { Q_ASSERT(isArrayIndex()); return static_cast<uint>(val & 0xffffffff); } |
117 | uint isArrayIndex() const { return !isManaged() && val != 0; } |
118 | bool isValid() const { return val != 0; } |
119 | static PropertyKey fromStringOrSymbol(Heap::StringOrSymbol *b) |
120 | { PropertyKey key; key.setM(b); return key; } |
121 | Heap::StringOrSymbol *asStringOrSymbol() const { |
122 | if (!isManaged()) |
123 | return nullptr; |
124 | return m(); |
125 | } |
126 | |
127 | Q_QML_EXPORT bool isString() const; |
128 | bool isSymbol() const; |
129 | bool isCanonicalNumericIndexString() const; |
130 | |
131 | Q_QML_EXPORT QString toQString() const; |
132 | Heap::StringOrSymbol *toStringOrSymbol(ExecutionEngine *e); |
133 | quint64 id() const { return val; } |
134 | static PropertyKey fromId(quint64 id) { |
135 | PropertyKey key; key.val = id; return key; |
136 | } |
137 | |
138 | enum FunctionNamePrefix { |
139 | None, |
140 | Getter, |
141 | Setter |
142 | }; |
143 | Heap::String *asFunctionName(ExecutionEngine *e, FunctionNamePrefix prefix) const; |
144 | |
145 | bool operator ==(const PropertyKey &other) const { return val == other.val; } |
146 | bool operator !=(const PropertyKey &other) const { return val != other.val; } |
147 | bool operator <(const PropertyKey &other) const { return val < other.val; } |
148 | }; |
149 | |
150 | } |
151 | |
152 | QT_END_NAMESPACE |
153 | |
154 | #endif |
155 | |