Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qjsengine.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QJSENGINE_H
6#define QJSENGINE_H
7
8#include <QtCore/qmetatype.h>
9
10#include <QtCore/qvariant.h>
11#include <QtCore/qsharedpointer.h>
12#include <QtCore/qobject.h>
13#include <QtCore/qtimezone.h>
14#include <QtQml/qjsvalue.h>
15#include <QtQml/qjsmanagedvalue.h>
16#include <QtQml/qqmldebug.h>
17
18QT_BEGIN_NAMESPACE
19
20
21template <typename T>
22inline T qjsvalue_cast(const QJSValue &);
23
24class QJSEnginePrivate;
25class Q_QML_EXPORT QJSEngine
26 : public QObject
27{
28 Q_OBJECT
29 Q_PROPERTY(QString uiLanguage READ uiLanguage WRITE setUiLanguage NOTIFY uiLanguageChanged)
30public:
31 QJSEngine();
32 explicit QJSEngine(QObject *parent);
33 ~QJSEngine() override;
34
35 QJSValue globalObject() const;
36
37 QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1, QStringList *exceptionStackTrace = nullptr);
38
39 QJSValue importModule(const QString &fileName);
40 bool registerModule(const QString &moduleName, const QJSValue &value);
41
42 QJSValue newObject();
43 QJSValue newSymbol(const QString &name);
44 QJSValue newArray(uint length = 0);
45
46 QJSValue newQObject(QObject *object);
47
48 QJSValue newQMetaObject(const QMetaObject* metaObject);
49
50 template <typename T>
51 QJSValue newQMetaObject()
52 {
53 return newQMetaObject(&T::staticMetaObject);
54 }
55
56 QJSValue newErrorObject(QJSValue::ErrorType errorType, const QString &message = QString());
57
58 template <typename T>
59 inline QJSValue toScriptValue(const T &value)
60 {
61 return create(QMetaType::fromType<T>(), &value);
62 }
63
64 template <typename T>
65 inline QJSManagedValue toManagedValue(const T &value)
66 {
67 return createManaged(QMetaType::fromType<T>(), &value);
68 }
69
70 template <typename T>
71 inline QJSPrimitiveValue toPrimitiveValue(const T &value)
72 {
73 // In the common case that the argument fits into QJSPrimitiveValue, use it.
74 if constexpr (std::disjunction_v<
75 std::is_same<T, int>,
76 std::is_same<T, bool>,
77 std::is_same<T, double>,
78 std::is_same<T, QString>>) {
79 return QJSPrimitiveValue(value);
80 } else {
81 return createPrimitive(QMetaType::fromType<T>(), &value);
82 }
83 }
84
85 template <typename T>
86 inline T fromScriptValue(const QJSValue &value)
87 {
88 return qjsvalue_cast<T>(value);
89 }
90
91 template <typename T>
92 inline T fromManagedValue(const QJSManagedValue &value)
93 {
94 return qjsvalue_cast<T>(value);
95 }
96
97 template <typename T>
98 inline T fromPrimitiveValue(const QJSPrimitiveValue &value)
99 {
100 if constexpr (std::is_same_v<T, int>)
101 return value.toInteger();
102 if constexpr (std::is_same_v<T, bool>)
103 return value.toBoolean();
104 if constexpr (std::is_same_v<T, double>)
105 return value.toDouble();
106 if constexpr (std::is_same_v<T, QString>)
107 return value.toString();
108 if constexpr (std::is_same_v<T, QVariant>)
109 return value.toVariant();
110 if constexpr (std::is_pointer_v<T>)
111 return nullptr;
112 return qjsvalue_cast<T>(value);
113 }
114
115 template <typename T>
116 inline T fromVariant(const QVariant &value)
117 {
118 // MSVC doesn't like the constexpr chains and complains about "unreachable code"
119 QT_WARNING_PUSH
120 QT_WARNING_DISABLE_MSVC(4702)
121
122 if constexpr (std::is_same_v<T, QVariant>)
123 return value;
124
125 const QMetaType sourceType = value.metaType();
126 const QMetaType targetType = QMetaType::fromType<T>();
127 if (sourceType == targetType)
128 return *reinterpret_cast<const T *>(value.constData());
129
130 if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>> const *>) {
131 using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *;
132 const QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>();
133 if (value.metaType() == nonConstTargetType)
134 return *reinterpret_cast<const nonConstT *>(value.constData());
135 }
136
137 if constexpr (std::is_same_v<T, QJSValue>)
138 return toScriptValue(value);
139
140 if constexpr (std::is_same_v<T, QJSManagedValue>)
141 return toManagedValue(value);
142
143 if constexpr (std::is_same_v<T, QJSPrimitiveValue>)
144 return toPrimitiveValue(value);
145
146 if constexpr (std::is_same_v<T, QString>) {
147 if (!sourceType.isValid())
148 return QStringLiteral("undefined");
149
150 if (sourceType.flags() & QMetaType::PointerToQObject) {
151 return convertQObjectToString(
152 *reinterpret_cast<QObject *const *>(value.constData()));
153 }
154 }
155
156 if constexpr (std::is_same_v<T, double> || std::is_same_v<T, float>) {
157 if (!sourceType.isValid())
158 return std::numeric_limits<T>::quiet_NaN();
159 }
160
161 if constexpr (std::is_same_v<T, double>) {
162 if (sourceType == QMetaType::fromType<int>())
163 return double(*static_cast<const int *>(value.constData()));
164 }
165
166 if constexpr (std::is_same_v<T, int>) {
167 if (sourceType == QMetaType::fromType<double>()) {
168 return QJSNumberCoercion::toInteger(
169 *static_cast<const double *>(value.constData()));
170 }
171 }
172
173 if constexpr (std::is_same_v<QObject, std::remove_const_t<std::remove_pointer_t<T>>>) {
174 if (sourceType.flags() & QMetaType::PointerToQObject) {
175 return *static_cast<QObject *const *>(value.constData());
176
177 // We should not access source->metaObject() here since that may trigger some
178 // rather involved code. convertVariant() can do this using property caches.
179 }
180 }
181
182 if (sourceType == QMetaType::fromType<QJSValue>())
183 return fromScriptValue<T>(*reinterpret_cast<const QJSValue *>(value.constData()));
184
185 if (sourceType == QMetaType::fromType<QJSManagedValue>()) {
186 return fromManagedValue<T>(
187 *reinterpret_cast<const QJSManagedValue *>(value.constData()));
188 }
189
190 if (sourceType == QMetaType::fromType<QJSPrimitiveValue>()) {
191 return fromPrimitiveValue<T>(
192 *reinterpret_cast<const QJSPrimitiveValue *>(value.constData()));
193 }
194
195 return [&] {
196 T t{};
197 if (value.metaType() == QMetaType::fromType<QString>()) {
198 if (convertString(value.toString(), targetType, &t))
199 return t;
200 } else if (convertVariant(value, targetType, &t)) {
201 return t;
202 }
203
204 QMetaType::convert(value.metaType(), value.constData(), targetType, &t);
205 return t;
206 }();
207
208 QT_WARNING_POP
209 }
210
211 template<typename From, typename To>
212 inline To coerceValue(const From &from)
213 {
214 if constexpr (std::is_base_of_v<To, From>)
215 return from;
216
217 if constexpr (std::is_same_v<To, QJSValue>)
218 return toScriptValue(from);
219
220 if constexpr (std::is_same_v<From, QJSValue>)
221 return fromScriptValue<To>(from);
222
223 if constexpr (std::is_same_v<To, QJSManagedValue>)
224 return toManagedValue(from);
225
226 if constexpr (std::is_same_v<From, QJSManagedValue>)
227 return fromManagedValue<To>(from);
228
229 if constexpr (std::is_same_v<To, QJSPrimitiveValue>)
230 return toPrimitiveValue(from);
231
232 if constexpr (std::is_same_v<From, QJSPrimitiveValue>)
233 return fromPrimitiveValue<To>(from);
234
235 if constexpr (std::is_same_v<From, QVariant>)
236 return fromVariant<To>(from);
237
238 if constexpr (std::is_same_v<To, QVariant>)
239 return QVariant::fromValue(from);
240
241 if constexpr (std::is_same_v<To, QString>) {
242 if constexpr (std::is_base_of_v<QObject, std::remove_const_t<std::remove_pointer_t<From>>>)
243 return convertQObjectToString(from);
244 }
245
246 if constexpr (std::is_same_v<From, QDateTime>) {
247 if constexpr (std::is_same_v<To, QDate>)
248 return convertDateTimeToDate(from.toLocalTime());
249 if constexpr (std::is_same_v<To, QTime>)
250 return from.toLocalTime().time();
251 if constexpr (std::is_same_v<To, QString>)
252 return convertDateTimeToString(from.toLocalTime());
253 if constexpr (std::is_same_v<To, double>)
254 return convertDateTimeToNumber(from.toLocalTime());
255 }
256
257 if constexpr (std::is_same_v<From, QDate>) {
258 if constexpr (std::is_same_v<To, QDateTime>)
259 return from.startOfDay(QTimeZone::UTC);
260 if constexpr (std::is_same_v<To, QTime>) {
261 // This is the current time zone offset, for better or worse
262 return coerceValue<QDateTime, QTime>(coerceValue<QDate, QDateTime>(from));
263 }
264 if constexpr (std::is_same_v<To, QString>)
265 return convertDateTimeToString(coerceValue<QDate, QDateTime>(from));
266 if constexpr (std::is_same_v<To, double>)
267 return convertDateTimeToNumber(coerceValue<QDate, QDateTime>(from));
268 }
269
270 if constexpr (std::is_same_v<From, QTime>) {
271 if constexpr (std::is_same_v<To, QDate>) {
272 // Yes. April Fools' 1971. See qv4dateobject.cpp.
273 return from.isValid() ? QDate(1971, 4, 1) : QDate();
274 }
275
276 if constexpr (std::is_same_v<To, QDateTime>)
277 return QDateTime(coerceValue<QTime, QDate>(from), from, QTimeZone::LocalTime);
278 if constexpr (std::is_same_v<To, QString>)
279 return convertDateTimeToString(coerceValue<QTime, QDateTime>(from));
280 if constexpr (std::is_same_v<To, double>)
281 return convertDateTimeToNumber(coerceValue<QTime, QDateTime>(from));
282 }
283
284 if constexpr (std::is_same_v<To, std::remove_const_t<std::remove_pointer_t<To>> const *>) {
285 using nonConstTo = std::remove_const_t<std::remove_pointer_t<To>> *;
286 if constexpr (std::is_same_v<From, nonConstTo>)
287 return from;
288 }
289
290 if constexpr (std::is_same_v<To, double>) {
291 if constexpr (std::is_same_v<From, int>)
292 return double(from);
293 }
294
295 if constexpr (std::is_same_v<To, int>) {
296 if constexpr (std::is_same_v<From, double>)
297 return QJSNumberCoercion::toInteger(from);
298 }
299
300 return [&] {
301 const QMetaType sourceType = QMetaType::fromType<From>();
302 const QMetaType targetType = QMetaType::fromType<To>();
303 To to{};
304 if constexpr (std::is_same_v<From, QString>) {
305 if (convertString(from, targetType, &to))
306 return to;
307 } else if (convertMetaType(sourceType, &from, targetType, &to)) {
308 return to;
309 }
310
311 QMetaType::convert(sourceType, &from, targetType, &to);
312 return to;
313 }();
314 }
315
316 void collectGarbage();
317
318 enum ObjectOwnership { CppOwnership, JavaScriptOwnership };
319 static void setObjectOwnership(QObject *, ObjectOwnership);
320 static ObjectOwnership objectOwnership(QObject *);
321
322 enum Extension {
323 TranslationExtension = 0x1,
324 ConsoleExtension = 0x2,
325 GarbageCollectionExtension = 0x4,
326 AllExtensions = 0xffffffff
327 };
328 Q_DECLARE_FLAGS(Extensions, Extension)
329
330 void installExtensions(Extensions extensions, const QJSValue &object = QJSValue());
331
332 void setInterrupted(bool interrupted);
333 bool isInterrupted() const;
334
335 QV4::ExecutionEngine *handle() const { return m_v4Engine; }
336
337 void throwError(const QString &message);
338 void throwError(QJSValue::ErrorType errorType, const QString &message = QString());
339 void throwError(const QJSValue &error);
340 bool hasError() const;
341 QJSValue catchError();
342
343 QString uiLanguage() const;
344 void setUiLanguage(const QString &language);
345
346Q_SIGNALS:
347 void uiLanguageChanged();
348
349private:
350 QJSPrimitiveValue createPrimitive(QMetaType type, const void *ptr);
351 QJSManagedValue createManaged(QMetaType type, const void *ptr);
352 QJSValue create(QMetaType type, const void *ptr);
353#if QT_QML_REMOVED_SINCE(6, 5)
354 QJSValue create(int id, const void *ptr); // only there for BC reasons
355#endif
356
357 static bool convertPrimitive(const QJSPrimitiveValue &value, QMetaType type, void *ptr);
358 static bool convertManaged(const QJSManagedValue &value, int type, void *ptr);
359 static bool convertManaged(const QJSManagedValue &value, QMetaType type, void *ptr);
360#if QT_QML_REMOVED_SINCE(6, 5)
361 static bool convertV2(const QJSValue &value, int type, void *ptr); // only there for BC reasons
362#endif
363 static bool convertV2(const QJSValue &value, QMetaType metaType, void *ptr);
364 static bool convertString(const QString &string, QMetaType metaType, void *ptr);
365
366 bool convertVariant(const QVariant &value, QMetaType metaType, void *ptr);
367 bool convertMetaType(QMetaType fromType, const void *from, QMetaType toType, void *to);
368
369 QString convertQObjectToString(QObject *object);
370 QString convertDateTimeToString(const QDateTime &dateTime);
371 double convertDateTimeToNumber(const QDateTime &dateTime);
372 static QDate convertDateTimeToDate(const QDateTime &dateTime);
373
374 template<typename T>
375 friend inline T qjsvalue_cast(const QJSValue &);
376
377 template<typename T>
378 friend inline T qjsvalue_cast(const QJSManagedValue &);
379
380 template<typename T>
381 friend inline T qjsvalue_cast(const QJSPrimitiveValue &);
382
383protected:
384 QJSEngine(QJSEnginePrivate &dd, QObject *parent = nullptr);
385
386private:
387 QV4::ExecutionEngine *m_v4Engine;
388 Q_DISABLE_COPY(QJSEngine)
389 Q_DECLARE_PRIVATE(QJSEngine)
390};
391
392Q_DECLARE_OPERATORS_FOR_FLAGS(QJSEngine::Extensions)
393
394template<typename T>
395T qjsvalue_cast(const QJSValue &value)
396{
397 if (T t; QJSEngine::convertV2(value, QMetaType::fromType<T>(), &t))
398 return t;
399 return qvariant_cast<T>(value.toVariant());
400}
401
402template<typename T>
403T qjsvalue_cast(const QJSManagedValue &value)
404{
405 if (T t; QJSEngine::convertManaged(value, QMetaType::fromType<T>(), &t))
406 return t;
407
408 return qvariant_cast<T>(value.toVariant());
409}
410
411template<typename T>
412T qjsvalue_cast(const QJSPrimitiveValue &value)
413{
414 if (T t; QJSEngine::convertPrimitive(value, QMetaType::fromType<T>(), &t))
415 return t;
416
417 return qvariant_cast<T>(value.toVariant());
418}
419
420template <>
421inline QVariant qjsvalue_cast<QVariant>(const QJSValue &value)
422{
423 return value.toVariant();
424}
425
426template <>
427inline QVariant qjsvalue_cast<QVariant>(const QJSManagedValue &value)
428{
429 return value.toVariant();
430}
431
432template <>
433inline QVariant qjsvalue_cast<QVariant>(const QJSPrimitiveValue &value)
434{
435 return value.toVariant();
436}
437
438Q_QML_EXPORT QJSEngine *qjsEngine(const QObject *);
439
440QT_END_NAMESPACE
441
442#endif // QJSENGINE_H
The QJSEngine class provides an environment for evaluating JavaScript code.
Definition qjsengine.h:27
QJSEngine * qjsEngine(const QObject *object)
T qjsvalue_cast(const QJSManagedValue &value)
Definition qjsengine.h:403