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
12QT_BEGIN_NAMESPACE
13
14class QJSValue;
15class QJSEngine;
16class QVariant;
17class QObject;
18struct QMetaObject;
19class QDateTime;
20class QJSPrimitiveValue;
21
22typedef QList<QJSValue> QJSValueList;
23namespace QV4 {
24 struct ExecutionEngine;
25}
26
27class QJSPrimitiveValue;
28class QJSManagedValue;
29
30class Q_QML_EXPORT QJSValue
31{
32public:
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
54public:
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 bool isVariant() const;
84 bool isQObject() const;
85 bool isQMetaObject() const;
86 bool isObject() const;
87 bool isDate() const;
88 bool isRegExp() const;
89 bool isArray() const;
90 bool isError() const;
91 bool isUrl() const;
92
93 QString toString() const;
94 double toNumber() const;
95 qint32 toInt() const;
96 quint32 toUInt() const;
97 bool toBool() const;
98
99 QVariant toVariant() const;
100 QVariant toVariant(ObjectConversionBehavior behavior) const;
101 QJSPrimitiveValue toPrimitive() const;
102
103 QObject *toQObject() const;
104 const QMetaObject *toQMetaObject() const;
105 QDateTime toDateTime() const;
106
107 bool equals(const QJSValue &other) const;
108 bool strictlyEquals(const QJSValue &other) const;
109
110 QJSValue prototype() const;
111 void setPrototype(const QJSValue &prototype);
112
113 QJSValue property(const QString &name) const;
114 void setProperty(const QString &name, const QJSValue &value);
115
116 bool hasProperty(const QString &name) const;
117 bool hasOwnProperty(const QString &name) const;
118
119 QJSValue property(quint32 arrayIndex) const;
120 void setProperty(quint32 arrayIndex, const QJSValue &value);
121
122 bool deleteProperty(const QString &name);
123
124 bool isCallable() const;
125 QJSValue call(const QJSValueList &args = QJSValueList()) const;
126 QJSValue callWithInstance(const QJSValue &instance, const QJSValueList &args = QJSValueList()) const;
127 QJSValue callAsConstructor(const QJSValueList &args = QJSValueList()) const;
128
129 ErrorType errorType() const;
130
131private:
132 friend class QJSValuePrivate;
133 // force compile error, prevent QJSValue(bool) to be called
134 QJSValue(void *) = delete;
135
136 quint64 d;
137};
138
139#ifndef QT_NO_DATASTREAM
140Q_QML_EXPORT QDataStream &operator<<(QDataStream &, const QJSValue &);
141Q_QML_EXPORT QDataStream &operator>>(QDataStream &, QJSValue &);
142#endif
143
144QT_END_NAMESPACE
145
146Q_DECLARE_METATYPE(QJSValue)
147
148#endif
149

source code of qtdeclarative/src/qml/jsapi/qjsvalue.h