| 1 | // Copyright (C) 2018 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 3 | #ifndef QV4PROPERTYKEY_H | 
|---|
| 4 | #define QV4PROPERTYKEY_H | 
|---|
| 5 |  | 
|---|
| 6 | // | 
|---|
| 7 | //  W A R N I N G | 
|---|
| 8 | //  ------------- | 
|---|
| 9 | // | 
|---|
| 10 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 11 | // implementation detail.  This header file may change from version to | 
|---|
| 12 | // version without notice, or even be removed. | 
|---|
| 13 | // | 
|---|
| 14 | // We mean it. | 
|---|
| 15 | // | 
|---|
| 16 |  | 
|---|
| 17 | #include <private/qv4writebarrier_p.h> | 
|---|
| 18 | #include <private/qv4global_p.h> | 
|---|
| 19 | #include <private/qv4staticvalue_p.h> | 
|---|
| 20 | #include <QtCore/qhashfunctions.h> | 
|---|
| 21 |  | 
|---|
| 22 | QT_BEGIN_NAMESPACE | 
|---|
| 23 |  | 
|---|
| 24 | class QString; | 
|---|
| 25 |  | 
|---|
| 26 | namespace QV4 { | 
|---|
| 27 |  | 
|---|
| 28 | struct PropertyKey | 
|---|
| 29 | { | 
|---|
| 30 | private: | 
|---|
| 31 | // Property keys are Strings, Symbols or unsigned integers. | 
|---|
| 32 | // For convenience we derive them from Values, allowing us to store them | 
|---|
| 33 | // on the JS stack | 
|---|
| 34 | // | 
|---|
| 35 | // They do however behave somewhat different than a Value: | 
|---|
| 36 | // * If the key is a String, the pointer to the string is stored in the identifier | 
|---|
| 37 | // table and thus unique. | 
|---|
| 38 | // * If the key is a Symbol it simply points to the referenced symbol object | 
|---|
| 39 | // * if the key is an array index (a uint < UINT_MAX), it's encoded as an | 
|---|
| 40 | // integer value | 
|---|
| 41 | QV4::StaticValue val; | 
|---|
| 42 |  | 
|---|
| 43 | inline bool isManaged() const { return val.isManaged(); } | 
|---|
| 44 | inline quint32 value() const { return val.value(); } | 
|---|
| 45 |  | 
|---|
| 46 | public: | 
|---|
| 47 | static PropertyKey invalid() | 
|---|
| 48 | { | 
|---|
| 49 | PropertyKey key; | 
|---|
| 50 | key.val = StaticValue::undefinedValue(); | 
|---|
| 51 | return key; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | static PropertyKey fromArrayIndex(uint idx) | 
|---|
| 55 | { | 
|---|
| 56 | PropertyKey key; | 
|---|
| 57 | key.val.setInt_32(idx); | 
|---|
| 58 | return key; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | bool isStringOrSymbol() const { return isManaged(); } | 
|---|
| 62 | uint asArrayIndex() const | 
|---|
| 63 | { | 
|---|
| 64 | Q_ASSERT(isArrayIndex()); | 
|---|
| 65 | return value(); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | bool isArrayIndex() const { return val.isInteger(); } | 
|---|
| 69 | bool isValid() const { return !val.isUndefined(); } | 
|---|
| 70 |  | 
|---|
| 71 | // We cannot #include the declaration of Heap::StringOrSymbol here. | 
|---|
| 72 | // Therefore we do some gymnastics to enforce the type safety. | 
|---|
| 73 |  | 
|---|
| 74 | template<typename StringOrSymbol = Heap::StringOrSymbol, typename Engine = QV4::EngineBase> | 
|---|
| 75 | static PropertyKey fromStringOrSymbol(Engine *engine, StringOrSymbol *b) | 
|---|
| 76 | { | 
|---|
| 77 | static_assert(std::is_base_of_v<Heap::StringOrSymbol, StringOrSymbol>); | 
|---|
| 78 | PropertyKey key; | 
|---|
| 79 | QV4::WriteBarrier::markCustom(engine, [&](QV4::MarkStack *stack) { | 
|---|
| 80 | if constexpr (QV4::WriteBarrier::isInsertionBarrier) { | 
|---|
| 81 | // treat this as an insertion - the StringOrSymbol becomes reachable | 
|---|
| 82 | // via the propertykey, so we consequently need to mark it durnig gc | 
|---|
| 83 | b->mark(stack); | 
|---|
| 84 | } | 
|---|
| 85 | }); | 
|---|
| 86 | key.val.setM(b); | 
|---|
| 87 | Q_ASSERT(key.isManaged()); | 
|---|
| 88 | return key; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | template<typename StringOrSymbol = Heap::StringOrSymbol> | 
|---|
| 92 | StringOrSymbol *asStringOrSymbol() const | 
|---|
| 93 | { | 
|---|
| 94 | static_assert(std::is_base_of_v<Heap::StringOrSymbol, StringOrSymbol>); | 
|---|
| 95 | if (!isManaged()) | 
|---|
| 96 | return nullptr; | 
|---|
| 97 | return static_cast<StringOrSymbol *>(val.m()); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | Q_QML_EXPORT bool isString() const; | 
|---|
| 101 | Q_QML_EXPORT bool isSymbol() const; | 
|---|
| 102 | bool isCanonicalNumericIndexString() const; | 
|---|
| 103 |  | 
|---|
| 104 | Q_QML_EXPORT QString toQString() const; | 
|---|
| 105 | Heap::StringOrSymbol *toStringOrSymbol(ExecutionEngine *e); | 
|---|
| 106 | quint64 id() const { return val._val; } | 
|---|
| 107 | static PropertyKey fromId(quint64 id) { | 
|---|
| 108 | PropertyKey key; key.val._val = id; return key; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | enum FunctionNamePrefix { | 
|---|
| 112 | None, | 
|---|
| 113 | Getter, | 
|---|
| 114 | Setter | 
|---|
| 115 | }; | 
|---|
| 116 | Heap::String *asFunctionName(ExecutionEngine *e, FunctionNamePrefix prefix) const; | 
|---|
| 117 |  | 
|---|
| 118 | bool operator ==(const PropertyKey &other) const { return val._val == other.val._val; } | 
|---|
| 119 | bool operator !=(const PropertyKey &other) const { return val._val != other.val._val; } | 
|---|
| 120 | bool operator <(const PropertyKey &other) const { return val._val < other.val._val; } | 
|---|
| 121 | friend size_t qHash(const PropertyKey &key, size_t seed = 0) { return qHash(key: key.val._val, seed); } | 
|---|
| 122 | }; | 
|---|
| 123 |  | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | QT_END_NAMESPACE | 
|---|
| 127 |  | 
|---|
| 128 | #endif | 
|---|
| 129 |  | 
|---|