diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2023-03-17 10:29:23 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2023-03-23 19:33:25 +0100 |
commit | a0867cf1f48c7caec6f22d56e0c77385b21f4d07 (patch) | |
tree | ba73c5649b419f05429f6d69d33525fc0ec03866 /src/qml/jsapi | |
parent | 24f5695d35f0ef7a8f48502de3d4810d21fb3d3f (diff) |
QJSEngine: Fix some conversions
The optimization for object-to-string conversion would never trigger
because we were checking the wrong metatype for the PointerToQObject
flag.
Furthermore, we can provide a very simple optimization for the case when
we just want a QObject*. Finally, if we have both types, we can use
is_base_of_v to optimize cases where we are converting to a base class.
Pick-to: 6.5
Task-number: QTBUG-111986
Change-Id: I731fe0398730a2a83222d8c2fdff8aa3d21f7aec
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r-- | src/qml/jsapi/qjsengine.h | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index c04e47d34e..dbeecb45ae 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -139,12 +139,21 @@ public: return toPrimitiveValue(value); if constexpr (std::is_same_v<T, QString>) { - if (targetType.flags() & QMetaType::PointerToQObject) { + if (sourceType.flags() & QMetaType::PointerToQObject) { return convertQObjectToString( *reinterpret_cast<QObject *const *>(value.constData())); } } + if constexpr (std::is_same_v<QObject, std::remove_const_t<std::remove_pointer_t<T>>>) { + if (sourceType.flags() & QMetaType::PointerToQObject) { + return *static_cast<QObject *const *>(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<QJSValue>()) return fromScriptValue<T>(*reinterpret_cast<const QJSValue *>(value.constData())); @@ -175,7 +184,7 @@ public: template<typename From, typename To> inline To coerceValue(const From &from) { - if constexpr (std::is_same_v<From, To>) + if constexpr (std::is_base_of_v<To, From>) return from; if constexpr (std::is_same_v<To, QJSValue>) |