1 | // Copyright (C) 2020 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 QJSMANAGEDVALUE_H |
5 | #define QJSMANAGEDVALUE_H |
6 | |
7 | #include <QtQml/qtqmlglobal.h> |
8 | #include <QtQml/qjsprimitivevalue.h> |
9 | #include <QtQml/qjsvalue.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | namespace QV4 { |
14 | struct Value; |
15 | struct ExecutionEngine; |
16 | } |
17 | |
18 | class QJSEngine; |
19 | class Q_QML_EXPORT QJSManagedValue |
20 | { |
21 | Q_DISABLE_COPY(QJSManagedValue) |
22 | public: |
23 | enum Type { |
24 | Undefined, |
25 | Boolean, |
26 | Number, |
27 | String, |
28 | Object, |
29 | Symbol, |
30 | Function |
31 | }; |
32 | |
33 | QJSManagedValue() = default; |
34 | QJSManagedValue(QJSValue value, QJSEngine *engine); |
35 | QJSManagedValue(const QJSPrimitiveValue &value, QJSEngine *engine); |
36 | QJSManagedValue(const QVariant &variant, QJSEngine *engine); |
37 | QJSManagedValue(const QString &string, QJSEngine *engine); |
38 | |
39 | ~QJSManagedValue(); |
40 | QJSManagedValue(QJSManagedValue &&other); |
41 | QJSManagedValue &operator=(QJSManagedValue &&other); |
42 | |
43 | bool equals(const QJSManagedValue &other) const; |
44 | bool strictlyEquals(const QJSManagedValue &other) const; |
45 | |
46 | QJSEngine *engine() const; |
47 | |
48 | QJSManagedValue prototype() const; |
49 | void setPrototype(const QJSManagedValue &prototype); |
50 | |
51 | Type type() const; |
52 | |
53 | // Compatibility with QJSValue |
54 | bool isUndefined() const { return type() == Undefined; } |
55 | bool isBoolean() const { return type() == Boolean; } |
56 | bool isNumber() const { return type() == Number; } |
57 | bool isString() const { return type() == String; } |
58 | bool isObject() const { return type() == Object; } |
59 | bool isSymbol() const { return type() == Symbol; } |
60 | bool isFunction() const { return type() == Function; } |
61 | |
62 | // Special case of Number |
63 | bool isInteger() const; |
64 | |
65 | // Selected special cases of Object |
66 | bool isNull() const; |
67 | bool isRegularExpression() const; |
68 | bool isArray() const; |
69 | bool isUrl() const; |
70 | bool isVariant() const; |
71 | bool isQObject() const; |
72 | bool isQMetaObject() const; |
73 | bool isDate() const; |
74 | bool isError() const; |
75 | bool isJsMetaType() const; |
76 | |
77 | // Native type transformations |
78 | QString toString() const; |
79 | double toNumber() const; |
80 | bool toBoolean() const; |
81 | |
82 | // Variant-like type transformations |
83 | QJSPrimitiveValue toPrimitive() const; |
84 | QJSValue toJSValue() const; |
85 | QVariant toVariant() const; |
86 | |
87 | // Special cases |
88 | int toInteger() const; |
89 | QRegularExpression toRegularExpression() const; |
90 | QUrl toUrl() const; |
91 | QObject *toQObject() const; |
92 | const QMetaObject *toQMetaObject() const; |
93 | QDateTime toDateTime() const; |
94 | |
95 | // Properties of objects |
96 | bool hasProperty(const QString &name) const; |
97 | bool hasOwnProperty(const QString &name) const; |
98 | QJSValue property(const QString &name) const; |
99 | void setProperty(const QString &name, const QJSValue &value); |
100 | bool deleteProperty(const QString &name); |
101 | |
102 | // ### Qt 7 use qsizetype instead. |
103 | // Array indexing |
104 | bool hasProperty(quint32 arrayIndex) const; |
105 | bool hasOwnProperty(quint32 arrayIndex) const; |
106 | QJSValue property(quint32 arrayIndex) const; |
107 | void setProperty(quint32 arrayIndex, const QJSValue &value); |
108 | bool deleteProperty(quint32 arrayIndex); |
109 | |
110 | // Calling functions |
111 | QJSValue call(const QJSValueList &arguments = {}) const; |
112 | QJSValue callWithInstance(const QJSValue &instance, const QJSValueList &arguments = {}) const; |
113 | QJSValue callAsConstructor(const QJSValueList &arguments = {}) const; |
114 | |
115 | // JavaScript metatypes |
116 | QJSManagedValue jsMetaType() const; |
117 | QStringList jsMetaMembers() const; |
118 | QJSManagedValue jsMetaInstantiate(const QJSValueList &values = {}) const; |
119 | |
120 | private: |
121 | friend class QJSValue; |
122 | friend class QJSEngine; |
123 | |
124 | QJSManagedValue(QV4::ExecutionEngine *engine); |
125 | QV4::Value *d = nullptr; |
126 | }; |
127 | |
128 | QT_END_NAMESPACE |
129 | |
130 | #endif |
131 | |