From a0867cf1f48c7caec6f22d56e0c77385b21f4d07 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 17 Mar 2023 10:29:23 +0100 Subject: 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 Reviewed-by: Qt CI Bot --- src/qml/jsapi/qjsengine.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/qml/jsapi') 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) { - if (targetType.flags() & QMetaType::PointerToQObject) { + 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())); @@ -175,7 +184,7 @@ public: template inline To coerceValue(const From &from) { - if constexpr (std::is_same_v) + if constexpr (std::is_base_of_v) return from; if constexpr (std::is_same_v) -- cgit v1.2.3