aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qjsvalue
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2023-12-15 16:39:45 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2023-12-19 15:39:18 +0100
commit4d7ce35bd2b3a7319973303f163c26cdf67c73f6 (patch)
tree25cb9c5da99ba81ad9972a2984aef21cb63b2928 /tests/auto/qml/qjsvalue
parentaf27f1ce3bf737f1b3e3d0413f1ff02cc5e32790 (diff)
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 <ulf.hermann@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'tests/auto/qml/qjsvalue')
-rw-r--r--tests/auto/qml/qjsvalue/tst_qjsvalue.cpp19
1 files changed, 19 insertions, 0 deletions
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<QJSValue>());
}
+
+ // 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<QVariantMap>());
+ auto variantMap = asVariant.value<QVariantMap>();
+ QVERIFY(variantMap.contains("firstName"));
+ QCOMPARE(variantMap["firstName"].toString(), "John");
+ }
}
void tst_QJSValue::toPrimitive_data()