From 4d7ce35bd2b3a7319973303f163c26cdf67c73f6 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Fri, 15 Dec 2023 16:39:45 +0100 Subject: QJSValue: convert more aggressively to QVariant Normally, we don't want to convert aggressively between JS objects and QVariant, as that is prone to losing information. However, QJSValue::toVariant is documented to attempt lossy conversions. Restore the behavior of Qt < 6.5.3 for it. This is done by replacing the boolean indicating we should wrap JS objects into QJSValue with an enum instead. That enum introduces a third state ("Aggressive"), which is only used for QJSValue::toVariant. All other users of QJSEngine::toVariant behave as before (post 6.5.3). Function objects are still not converted, as we know that this would be a futile attempt, and more importantly, to keep the behavior that existed before Qt 6.5.3. Amends 43077556550c6b17226a7d393ec844b605c9c678 which introduced the regression and afe96c4d633146df477012975824b8ab65034239 which fixed the issue only partially. Pick-to: 6.5 6.6 6.7 Fixes: QTBUG-119963 Change-Id: I07d9901437812579ac5b873a4dff4de60c8f617e Reviewed-by: Ulf Hermann Reviewed-by: Qt CI Bot --- tests/auto/qml/qjsvalue/tst_qjsvalue.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests/auto/qml/qjsvalue') diff --git a/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp b/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp index ada191d863..6c7612b77e 100644 --- a/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp +++ b/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp @@ -1113,6 +1113,25 @@ void tst_QJSValue::toVariant() QCOMPARE(func.toVariant().metaType(), QMetaType::fromType()); } + + // object with custom prototype + { + QJSValue object = eng.evaluate(R"js( + (function(){ + function Person(firstName, lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + return new Person("John", "Doe"); + })(); + )js"); + QVERIFY(object.isObject()); + auto asVariant = object.toVariant(); + QCOMPARE(asVariant.metaType(), QMetaType::fromType()); + auto variantMap = asVariant.value(); + QVERIFY(variantMap.contains("firstName")); + QCOMPARE(variantMap["firstName"].toString(), "John"); + } } void tst_QJSValue::toPrimitive_data() -- cgit v1.2.3