| 1 | // Copyright (C) 2016 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 | |
| 4 | #ifndef QJSVALUE_H |
| 5 | #define QJSVALUE_H |
| 6 | |
| 7 | #include <QtQml/qtqmlglobal.h> |
| 8 | #include <QtCore/qstring.h> |
| 9 | #include <QtCore/qlist.h> |
| 10 | #include <QtCore/qmetatype.h> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | class QJSValue; |
| 15 | class QJSEngine; |
| 16 | class QVariant; |
| 17 | class QObject; |
| 18 | struct QMetaObject; |
| 19 | class QDateTime; |
| 20 | class QJSPrimitiveValue; |
| 21 | |
| 22 | typedef QList<QJSValue> QJSValueList; |
| 23 | namespace QV4 { |
| 24 | struct ExecutionEngine; |
| 25 | } |
| 26 | |
| 27 | class QJSPrimitiveValue; |
| 28 | class QJSManagedValue; |
| 29 | |
| 30 | class Q_QML_EXPORT QJSValue |
| 31 | { |
| 32 | public: |
| 33 | enum SpecialValue { |
| 34 | NullValue, |
| 35 | UndefinedValue |
| 36 | }; |
| 37 | |
| 38 | enum ErrorType { |
| 39 | NoError, |
| 40 | GenericError, |
| 41 | EvalError, |
| 42 | RangeError, |
| 43 | ReferenceError, |
| 44 | SyntaxError, |
| 45 | TypeError, |
| 46 | URIError |
| 47 | }; |
| 48 | |
| 49 | enum ObjectConversionBehavior { |
| 50 | ConvertJSObjects, |
| 51 | RetainJSObjects |
| 52 | }; |
| 53 | |
| 54 | public: |
| 55 | QJSValue(SpecialValue value = UndefinedValue); |
| 56 | ~QJSValue(); |
| 57 | QJSValue(const QJSValue &other); |
| 58 | |
| 59 | inline QJSValue(QJSValue && other) : d(other.d) { other.d = 0; } |
| 60 | inline QJSValue &operator=(QJSValue &&other) |
| 61 | { std::swap(a&: d, b&: other.d); return *this; } |
| 62 | |
| 63 | QJSValue(bool value); |
| 64 | QJSValue(int value); |
| 65 | QJSValue(uint value); |
| 66 | QJSValue(double value); |
| 67 | QJSValue(const QString &value); |
| 68 | QJSValue(const QLatin1String &value); |
| 69 | #ifndef QT_NO_CAST_FROM_ASCII |
| 70 | QT_ASCII_CAST_WARN QJSValue(const char *str); |
| 71 | #endif |
| 72 | |
| 73 | QJSValue &operator=(const QJSValue &other); |
| 74 | |
| 75 | explicit QJSValue(QJSPrimitiveValue &&value); |
| 76 | explicit QJSValue(QJSManagedValue &&value); |
| 77 | |
| 78 | bool isBool() const; |
| 79 | bool isNumber() const; |
| 80 | bool isNull() const; |
| 81 | bool isString() const; |
| 82 | bool isUndefined() const; |
| 83 | #if QT_DEPRECATED_SINCE(6, 9) |
| 84 | QT_DEPRECATED_VERSION_X_6_9("This might return unexpected results; consult documentation for more information" ) |
| 85 | bool isVariant() const; |
| 86 | #endif |
| 87 | bool isQObject() const; |
| 88 | bool isQMetaObject() const; |
| 89 | bool isObject() const; |
| 90 | bool isDate() const; |
| 91 | bool isRegExp() const; |
| 92 | bool isArray() const; |
| 93 | bool isError() const; |
| 94 | bool isUrl() const; |
| 95 | |
| 96 | QString toString() const; |
| 97 | double toNumber() const; |
| 98 | qint32 toInt() const; |
| 99 | quint32 toUInt() const; |
| 100 | bool toBool() const; |
| 101 | |
| 102 | QVariant toVariant() const; |
| 103 | QVariant toVariant(ObjectConversionBehavior behavior) const; |
| 104 | QJSPrimitiveValue toPrimitive() const; |
| 105 | |
| 106 | QObject *toQObject() const; |
| 107 | const QMetaObject *toQMetaObject() const; |
| 108 | QDateTime toDateTime() const; |
| 109 | |
| 110 | bool equals(const QJSValue &other) const; |
| 111 | bool strictlyEquals(const QJSValue &other) const; |
| 112 | |
| 113 | QJSValue prototype() const; |
| 114 | void setPrototype(const QJSValue &prototype); |
| 115 | |
| 116 | QJSValue property(const QString &name) const; |
| 117 | void setProperty(const QString &name, const QJSValue &value); |
| 118 | |
| 119 | bool hasProperty(const QString &name) const; |
| 120 | bool hasOwnProperty(const QString &name) const; |
| 121 | |
| 122 | QJSValue property(quint32 arrayIndex) const; |
| 123 | void setProperty(quint32 arrayIndex, const QJSValue &value); |
| 124 | |
| 125 | bool deleteProperty(const QString &name); |
| 126 | |
| 127 | bool isCallable() const; |
| 128 | QJSValue call(const QJSValueList &args = QJSValueList()) const; |
| 129 | QJSValue callWithInstance(const QJSValue &instance, const QJSValueList &args = QJSValueList()) const; |
| 130 | QJSValue callAsConstructor(const QJSValueList &args = QJSValueList()) const; |
| 131 | |
| 132 | ErrorType errorType() const; |
| 133 | |
| 134 | private: |
| 135 | friend class QJSValuePrivate; |
| 136 | // force compile error, prevent QJSValue(bool) to be called |
| 137 | QJSValue(void *) = delete; |
| 138 | |
| 139 | quint64 d; |
| 140 | }; |
| 141 | |
| 142 | #ifndef QT_NO_DATASTREAM |
| 143 | Q_QML_EXPORT QDataStream &operator<<(QDataStream &, const QJSValue &); |
| 144 | Q_QML_EXPORT QDataStream &operator>>(QDataStream &, QJSValue &); |
| 145 | #endif |
| 146 | |
| 147 | QT_END_NAMESPACE |
| 148 | |
| 149 | Q_DECLARE_METATYPE(QJSValue) |
| 150 | |
| 151 | #endif |
| 152 | |