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