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 QJSENGINE_H
41#define QJSENGINE_H
42
43#include <QtCore/qmetatype.h>
44
45#include <QtCore/qvariant.h>
46#include <QtCore/qsharedpointer.h>
47#include <QtCore/qobject.h>
48#include <QtQml/qjsvalue.h>
49
50#include <QtQml/qqmldebug.h>
51
52QT_BEGIN_NAMESPACE
53
54
55template <typename T>
56inline T qjsvalue_cast(const QJSValue &);
57
58class QJSEnginePrivate;
59class Q_QML_EXPORT QJSEngine
60 : public QObject
61{
62 Q_OBJECT
63public:
64 QJSEngine();
65 explicit QJSEngine(QObject *parent);
66 ~QJSEngine() override;
67
68 QJSValue globalObject() const;
69
70 QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1);
71
72 QJSValue importModule(const QString &fileName);
73
74 QJSValue newObject();
75 QJSValue newArray(uint length = 0);
76
77 QJSValue newQObject(QObject *object);
78
79 QJSValue newQMetaObject(const QMetaObject* metaObject);
80
81 template <typename T>
82 QJSValue newQMetaObject()
83 {
84 return newQMetaObject(&T::staticMetaObject);
85 }
86
87 QJSValue newErrorObject(QJSValue::ErrorType errorType, const QString &message = QString());
88
89 template <typename T>
90 inline QJSValue toScriptValue(const T &value)
91 {
92 return create(qMetaTypeId<T>(), &value);
93 }
94 template <typename T>
95 inline T fromScriptValue(const QJSValue &value)
96 {
97 return qjsvalue_cast<T>(value);
98 }
99
100 void collectGarbage();
101
102#if QT_DEPRECATED_SINCE(5, 6)
103 QT_DEPRECATED void installTranslatorFunctions(const QJSValue &object = QJSValue());
104#endif
105
106 enum Extension {
107 TranslationExtension = 0x1,
108 ConsoleExtension = 0x2,
109 GarbageCollectionExtension = 0x4,
110 AllExtensions = 0xffffffff
111 };
112 Q_DECLARE_FLAGS(Extensions, Extension)
113
114 void installExtensions(Extensions extensions, const QJSValue &object = QJSValue());
115
116 void setInterrupted(bool interrupted);
117 bool isInterrupted() const;
118
119 QV4::ExecutionEngine *handle() const { return m_v4Engine; }
120
121 void throwError(const QString &message);
122 void throwError(QJSValue::ErrorType errorType, const QString &message = QString());
123
124private:
125 QJSValue create(int type, const void *ptr);
126
127 static bool convertV2(const QJSValue &value, int type, void *ptr);
128
129 friend inline bool qjsvalue_cast_helper(const QJSValue &, int, void *);
130
131protected:
132 QJSEngine(QJSEnginePrivate &dd, QObject *parent = nullptr);
133
134private:
135 QV4::ExecutionEngine *m_v4Engine;
136 Q_DISABLE_COPY(QJSEngine)
137 Q_DECLARE_PRIVATE(QJSEngine)
138};
139
140Q_DECLARE_OPERATORS_FOR_FLAGS(QJSEngine::Extensions)
141
142inline bool qjsvalue_cast_helper(const QJSValue &value, int type, void *ptr)
143{
144 return QJSEngine::convertV2(value, type, ptr);
145}
146
147template<typename T>
148T qjsvalue_cast(const QJSValue &value)
149{
150 T t;
151 const int id = qMetaTypeId<T>();
152
153 if (qjsvalue_cast_helper(value, id, &t))
154 return t;
155 else if (value.isVariant())
156 return qvariant_cast<T>(value.toVariant());
157
158 return T();
159}
160
161template <>
162inline QVariant qjsvalue_cast<QVariant>(const QJSValue &value)
163{
164 return value.toVariant();
165}
166
167Q_QML_EXPORT QJSEngine *qjsEngine(const QObject *);
168
169QT_END_NAMESPACE
170
171#endif // QJSENGINE_H
172