summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-02-18 15:46:24 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-02-19 00:29:45 +0000
commite09acc38d3af64c2136ee8b6ab4f01a83492ba17 (patch)
treef5713d97eba1f314fd1f329e89d7a2cc1607242f
parent92ebe65589ac0e173d297817b07704b2b6f897ee (diff)
QJSValue: Allow casting integers to enums
You can also cast enums to integers, after all. Change-Id: I283d3dd280eeb44ba22bb45ca9be69e5358d5781 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit 453be4e6065a323e7fc0ea93fa0bee845d2020cd) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/qml/jsruntime/qv4engine.cpp8
-rw-r--r--tests/auto/qml/qjsvalue/tst_qjsvalue.cpp17
-rw-r--r--tests/auto/qml/qjsvalue/tst_qjsvalue.h1
3 files changed, 26 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp
index 320dc15a15..21aee81985 100644
--- a/src/qml/jsruntime/qv4engine.cpp
+++ b/src/qml/jsruntime/qv4engine.cpp
@@ -2338,6 +2338,14 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, int type, void *data)
}
{
+ const QMetaType metaType(type);
+ if (metaType.flags() & QMetaType::IsEnumeration) {
+ *reinterpret_cast<int *>(data) = value.toInt32();
+ return true;
+ }
+ }
+
+ {
const QQmlValueTypeWrapper *vtw = value.as<QQmlValueTypeWrapper>();
if (vtw && vtw->typeId() == type) {
return vtw->toGadget(data);
diff --git a/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp b/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp
index fd2eb40717..2b91ca0019 100644
--- a/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp
+++ b/tests/auto/qml/qjsvalue/tst_qjsvalue.cpp
@@ -2872,4 +2872,21 @@ void tst_QJSValue::jsFunctionInVariant()
}
}
+void tst_QJSValue::integerToEnum()
+{
+ QJSEngine engine;
+
+ QJSValue enumVal = engine.toScriptValue(QQmlComponent::Error);
+ QJSValue intVal(static_cast<int>(QQmlComponent::Error));
+
+ QVERIFY(enumVal.equals(intVal));
+ QVERIFY(intVal.equals(enumVal));
+
+ QCOMPARE(qjsvalue_cast<QQmlComponent::Status>(intVal), QQmlComponent::Error);
+ QCOMPARE(qjsvalue_cast<QQmlComponent::Status>(enumVal), QQmlComponent::Error);
+
+ QCOMPARE(qjsvalue_cast<int>(intVal), static_cast<int>(QQmlComponent::Error));
+ QCOMPARE(qjsvalue_cast<int>(enumVal), static_cast<int>(QQmlComponent::Error));
+}
+
QTEST_MAIN(tst_QJSValue)
diff --git a/tests/auto/qml/qjsvalue/tst_qjsvalue.h b/tests/auto/qml/qjsvalue/tst_qjsvalue.h
index 0b35b58c58..db6ed4e413 100644
--- a/tests/auto/qml/qjsvalue/tst_qjsvalue.h
+++ b/tests/auto/qml/qjsvalue/tst_qjsvalue.h
@@ -148,6 +148,7 @@ private slots:
void deleteFromDifferentThread();
void stringAndUrl();
void jsFunctionInVariant();
+ void integerToEnum();
private:
void newEngine()