// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QJSENGINE_H #define QJSENGINE_H #include #include #include #include #include #include #include #include QT_BEGIN_NAMESPACE template inline T qjsvalue_cast(const QJSValue &); class QJSEnginePrivate; class Q_QML_EXPORT QJSEngine : public QObject { Q_OBJECT Q_PROPERTY(QString uiLanguage READ uiLanguage WRITE setUiLanguage NOTIFY uiLanguageChanged) public: QJSEngine(); explicit QJSEngine(QObject *parent); ~QJSEngine() override; QJSValue globalObject() const; QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1, QStringList *exceptionStackTrace = nullptr); QJSValue importModule(const QString &fileName); bool registerModule(const QString &moduleName, const QJSValue &value); QJSValue newObject(); QJSValue newSymbol(const QString &name); QJSValue newArray(uint length = 0); QJSValue newQObject(QObject *object); QJSValue newQMetaObject(const QMetaObject* metaObject); template QJSValue newQMetaObject() { return newQMetaObject(&T::staticMetaObject); } QJSValue newErrorObject(QJSValue::ErrorType errorType, const QString &message = QString()); template inline QJSValue toScriptValue(const T &value) { return create(QMetaType::fromType(), &value); } template inline QJSManagedValue toManagedValue(const T &value) { return createManaged(QMetaType::fromType(), &value); } template inline QJSPrimitiveValue toPrimitiveValue(const T &value) { // In the common case that the argument fits into QJSPrimitiveValue, use it. if constexpr (std::disjunction_v< std::is_same, std::is_same, std::is_same, std::is_same>) { return QJSPrimitiveValue(value); } else { return createPrimitive(QMetaType::fromType(), &value); } } template inline T fromScriptValue(const QJSValue &value) { return qjsvalue_cast(value); } template inline T fromManagedValue(const QJSManagedValue &value) { return qjsvalue_cast(value); } template inline T fromPrimitiveValue(const QJSPrimitiveValue &value) { if constexpr (std::is_same_v) return value.toInteger(); if constexpr (std::is_same_v) return value.toBoolean(); if constexpr (std::is_same_v) return value.toDouble(); if constexpr (std::is_same_v) return value.toString(); if constexpr (std::is_same_v) return value.toVariant(); if constexpr (std::is_pointer_v) return nullptr; return qjsvalue_cast(value); } template inline T fromVariant(const QVariant &value) { if constexpr (std::is_same_v) return value; const QMetaType sourceType = value.metaType(); const QMetaType targetType = QMetaType::fromType(); if (sourceType == targetType) return *reinterpret_cast(value.constData()); if constexpr (std::is_same_v> const *>) { using nonConstT = std::remove_const_t> *; const QMetaType nonConstTargetType = QMetaType::fromType(); if (value.metaType() == nonConstTargetType) return *reinterpret_cast(value.constData()); } if constexpr (std::is_same_v) return toScriptValue(value); if constexpr (std::is_same_v) return toManagedValue(value); if constexpr (std::is_same_v) return toPrimitiveValue(value); if constexpr (std::is_same_v) { if (sourceType.flags() & QMetaType::PointerToQObject) { return convertQObjectToString( *reinterpret_cast(value.constData())); } } if constexpr (std::is_same_v>>) { if (sourceType.flags() & QMetaType::PointerToQObject) { return *static_cast(value.constData()); // We should not access source->metaObject() here since that may trigger some // rather involved code. convertVariant() can do this using property caches. } } if (sourceType == QMetaType::fromType()) return fromScriptValue(*reinterpret_cast(value.constData())); if (sourceType == QMetaType::fromType()) { return fromManagedValue( *reinterpret_cast(value.constData())); } if (sourceType == QMetaType::fromType()) { return fromPrimitiveValue( *reinterpret_cast(value.constData())); } { T t{}; if (value.metaType() == QMetaType::fromType()) { if (convertString(value.toString(), targetType, &t)) return t; } else if (convertVariant(value, targetType, &t)) { return t; } QMetaType::convert(value.metaType(), value.constData(), targetType, &t); return t; } } template inline To coerceValue(const From &from) { if constexpr (std::is_base_of_v) return from; if constexpr (std::is_same_v) return toScriptValue(from); if constexpr (std::is_same_v) return fromScriptValue(from); if constexpr (std::is_same_v) return toManagedValue(from); if constexpr (std::is_same_v) return fromManagedValue(from); if constexpr (std::is_same_v) return toPrimitiveValue(from); if constexpr (std::is_same_v) return fromPrimitiveValue(from); if constexpr (std::is_same_v) return fromVariant(from); if constexpr (std::is_same_v) return QVariant::fromValue(from); if constexpr (std::is_same_v) { if constexpr (std::is_base_of_v>>) return convertQObjectToString(from); } if constexpr (std::is_same_v) { if constexpr (std::is_same_v) return convertDateTimeToDate(from.toLocalTime()); if constexpr (std::is_same_v) return from.toLocalTime().time(); if constexpr (std::is_same_v) return convertDateTimeToString(from.toLocalTime()); if constexpr (std::is_same_v) return convertDateTimeToNumber(from.toLocalTime()); } if constexpr (std::is_same_v) { if constexpr (std::is_same_v) return from.startOfDay(QTimeZone::UTC); if constexpr (std::is_same_v) { // This is the current time zone offset, for better or worse return coerceValue(coerceValue(from)); } if constexpr (std::is_same_v) return convertDateTimeToString(coerceValue(from)); if constexpr (std::is_same_v) return convertDateTimeToNumber(coerceValue(from)); } if constexpr (std::is_same_v) { if constexpr (std::is_same_v) { // Yes. April Fools' 1971. See qv4dateobject.cpp. return from.isValid() ? QDate(1971, 4, 1) : QDate(); } if constexpr (std::is_same_v) return QDateTime(coerceValue(from), from, QTimeZone::LocalTime); if constexpr (std::is_same_v) return convertDateTimeToString(coerceValue(from)); if constexpr (std::is_same_v) return convertDateTimeToNumber(coerceValue(from)); } if constexpr (std::is_same_v> const *>) { using nonConstTo = std::remove_const_t> *; if constexpr (std::is_same_v) return from; } { const QMetaType sourceType = QMetaType::fromType(); const QMetaType targetType = QMetaType::fromType(); To to{}; if constexpr (std::is_same_v) { if (convertString(from, targetType, &to)) return to; } else if (convertMetaType(sourceType, &from, targetType, &to)) { return to; } QMetaType::convert(sourceType, &from, targetType, &to); return to; } } void collectGarbage(); enum ObjectOwnership { CppOwnership, JavaScriptOwnership }; static void setObjectOwnership(QObject *, ObjectOwnership); static ObjectOwnership objectOwnership(QObject *); enum Extension { TranslationExtension = 0x1, ConsoleExtension = 0x2, GarbageCollectionExtension = 0x4, AllExtensions = 0xffffffff }; Q_DECLARE_FLAGS(Extensions, Extension) void installExtensions(Extensions extensions, const QJSValue &object = QJSValue()); void setInterrupted(bool interrupted); bool isInterrupted() const; QV4::ExecutionEngine *handle() const { return m_v4Engine; } void throwError(const QString &message); void throwError(QJSValue::ErrorType errorType, const QString &message = QString()); void throwError(const QJSValue &error); bool hasError() const; QJSValue catchError(); QString uiLanguage() const; void setUiLanguage(const QString &language); Q_SIGNALS: void uiLanguageChanged(); private: QJSPrimitiveValue createPrimitive(QMetaType type, const void *ptr); QJSManagedValue createManaged(QMetaType type, const void *ptr); QJSValue create(QMetaType type, const void *ptr); #if QT_QML_REMOVED_SINCE(6, 5) QJSValue create(int id, const void *ptr); // only there for BC reasons #endif static bool convertPrimitive(const QJSPrimitiveValue &value, QMetaType type, void *ptr); static bool convertManaged(const QJSManagedValue &value, int type, void *ptr); static bool convertManaged(const QJSManagedValue &value, QMetaType type, void *ptr); #if QT_QML_REMOVED_SINCE(6, 5) static bool convertV2(const QJSValue &value, int type, void *ptr); // only there for BC reasons #endif static bool convertV2(const QJSValue &value, QMetaType metaType, void *ptr); static bool convertString(const QString &string, QMetaType metaType, void *ptr); bool convertVariant(const QVariant &value, QMetaType metaType, void *ptr); bool convertMetaType(QMetaType fromType, const void *from, QMetaType toType, void *to); QString convertQObjectToString(QObject *object); QString convertDateTimeToString(const QDateTime &dateTime); double convertDateTimeToNumber(const QDateTime &dateTime); static QDate convertDateTimeToDate(const QDateTime &dateTime); template friend inline T qjsvalue_cast(const QJSValue &); template friend inline T qjsvalue_cast(const QJSManagedValue &); template friend inline T qjsvalue_cast(const QJSPrimitiveValue &); protected: QJSEngine(QJSEnginePrivate &dd, QObject *parent = nullptr); private: QV4::ExecutionEngine *m_v4Engine; Q_DISABLE_COPY(QJSEngine) Q_DECLARE_PRIVATE(QJSEngine) }; Q_DECLARE_OPERATORS_FOR_FLAGS(QJSEngine::Extensions) template T qjsvalue_cast(const QJSValue &value) { if (T t; QJSEngine::convertV2(value, QMetaType::fromType(), &t)) return t; return qvariant_cast(value.toVariant()); } template T qjsvalue_cast(const QJSManagedValue &value) { if (T t; QJSEngine::convertManaged(value, QMetaType::fromType(), &t)) return t; return qvariant_cast(value.toVariant()); } template T qjsvalue_cast(const QJSPrimitiveValue &value) { if (T t; QJSEngine::convertPrimitive(value, QMetaType::fromType(), &t)) return t; return qvariant_cast(value.toVariant()); } template <> inline QVariant qjsvalue_cast(const QJSValue &value) { return value.toVariant(); } template <> inline QVariant qjsvalue_cast(const QJSManagedValue &value) { return value.toVariant(); } template <> inline QVariant qjsvalue_cast(const QJSPrimitiveValue &value) { return value.toVariant(); } Q_QML_EXPORT QJSEngine *qjsEngine(const QObject *); QT_END_NAMESPACE #endif // QJSENGINE_H