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