1/****************************************************************************
2**
3** Copyright (C) 2016 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
40#ifndef QJSVALUE_H
41#define QJSVALUE_H
42
43#include <QtQml/qtqmlglobal.h>
44#include <QtCore/qstring.h>
45#include <QtCore/qlist.h>
46#include <QtCore/qmetatype.h>
47
48QT_BEGIN_NAMESPACE
49
50class QJSValue;
51class QJSEngine;
52class QVariant;
53class QObject;
54struct QMetaObject;
55class QDateTime;
56
57typedef QList<QJSValue> QJSValueList;
58namespace QV4 {
59 struct ExecutionEngine;
60 struct Value;
61}
62
63class Q_QML_EXPORT QJSValue
64{
65public:
66 enum SpecialValue {
67 NullValue,
68 UndefinedValue
69 };
70
71 enum ErrorType {
72 NoError,
73 GenericError,
74 EvalError,
75 RangeError,
76 ReferenceError,
77 SyntaxError,
78 TypeError,
79 URIError
80 };
81
82public:
83 QJSValue(SpecialValue value = UndefinedValue);
84 ~QJSValue();
85 QJSValue(const QJSValue &other);
86
87#ifdef Q_COMPILER_RVALUE_REFS
88 inline QJSValue(QJSValue && other) : d(other.d) { other.d = 0; }
89 inline QJSValue &operator=(QJSValue &&other)
90 { qSwap(d, other.d); return *this; }
91#endif
92
93 QJSValue(bool value);
94 QJSValue(int value);
95 QJSValue(uint value);
96 QJSValue(double value);
97 QJSValue(const QString &value);
98 QJSValue(const QLatin1String &value);
99#ifndef QT_NO_CAST_FROM_ASCII
100 QT_ASCII_CAST_WARN QJSValue(const char *str);
101#endif
102
103 QJSValue &operator=(const QJSValue &other);
104
105 bool isBool() const;
106 bool isNumber() const;
107 bool isNull() const;
108 bool isString() const;
109 bool isUndefined() const;
110 bool isVariant() const;
111 bool isQObject() const;
112 bool isQMetaObject() const;
113 bool isObject() const;
114 bool isDate() const;
115 bool isRegExp() const;
116 bool isArray() const;
117 bool isError() const;
118
119 QString toString() const;
120 double toNumber() const;
121 qint32 toInt() const;
122 quint32 toUInt() const;
123 bool toBool() const;
124 QVariant toVariant() const;
125 QObject *toQObject() const;
126 const QMetaObject *toQMetaObject() const;
127 QDateTime toDateTime() const;
128
129 bool equals(const QJSValue &other) const;
130 bool strictlyEquals(const QJSValue &other) const;
131
132 QJSValue prototype() const;
133 void setPrototype(const QJSValue &prototype);
134
135 QJSValue property(const QString &name) const;
136 void setProperty(const QString &name, const QJSValue &value);
137
138 bool hasProperty(const QString &name) const;
139 bool hasOwnProperty(const QString &name) const;
140
141 QJSValue property(quint32 arrayIndex) const;
142 void setProperty(quint32 arrayIndex, const QJSValue &value);
143
144 bool deleteProperty(const QString &name);
145
146 bool isCallable() const;
147 QJSValue call(const QJSValueList &args = QJSValueList()); // ### Qt6: Make const
148 QJSValue callWithInstance(const QJSValue &instance, const QJSValueList &args = QJSValueList()); // ### Qt6: Make const
149 QJSValue callAsConstructor(const QJSValueList &args = QJSValueList()); // ### Qt6: Make const
150
151 ErrorType errorType() const;
152#ifdef QT_DEPRECATED
153 QT_DEPRECATED QJSEngine *engine() const;
154#endif
155
156 QJSValue(QV4::ExecutionEngine *e, quint64 val);
157private:
158 friend class QJSValuePrivate;
159 // force compile error, prevent QJSValue(bool) to be called
160 QJSValue(void *) Q_DECL_EQ_DELETE;
161
162 mutable quintptr d;
163};
164
165QT_END_NAMESPACE
166
167Q_DECLARE_METATYPE(QJSValue)
168
169#endif
170