diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-09-03 14:35:26 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-09-06 18:31:11 +0200 |
commit | dbcc2e7d56fca7cfe9209d1cb5b0640a0d3203d0 (patch) | |
tree | 0a573cce94df71b5bcbb13037849cd3a924a1322 | |
parent | bfe3e183d5817a09f04fc58b0b9ac59341111755 (diff) |
Use value type providers in metaTypeFromJS
Task-number: QTBUG-96144
Change-Id: If12f669ae33bb8ae4768ca79e4ca985f70a2651d
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 9d9df8073dcee685ce47074e93022e3e2b75b2b8)
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 28 | ||||
-rw-r--r-- | tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 26 |
2 files changed, 35 insertions, 19 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 6883c219bc..fde5079137 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -2462,25 +2462,6 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi } } -#if 0 - if (isQtVariant(value)) { - const QVariant &var = variantValue(value); - // ### Enable once constructInPlace() is in qt master. - if (var.userType() == type) { - QMetaType::constructInPlace(type, data, var.constData()); - return true; - } - if (var.canConvert(type)) { - QVariant vv = var; - vv.convert(type); - Q_ASSERT(vv.userType() == type); - QMetaType::constructInPlace(type, data, vv.constData()); - return true; - } - - } -#endif - // Try to use magic; for compatibility with qjsvalue_cast. if (convertToNativeQObject(value, metaType, reinterpret_cast<void **>(data))) @@ -2531,6 +2512,15 @@ bool ExecutionEngine::metaTypeFromJS(const Value &value, QMetaType metaType, voi } else if (metaType == QMetaType::fromType<QJSValue>()) { QJSValuePrivate::setValue(reinterpret_cast<QJSValue*>(data), value.asReturnedValue()); return true; + } else if (!isPointer) { + QVariant val; + if (QQml_valueTypeProvider()->createValueType( + metaType.id(), QJSValuePrivate::fromReturnedValue(value.asReturnedValue()), val)) { + Q_ASSERT(val.metaType() == metaType); + metaType.destruct(data); + metaType.construct(data, val.constData()); + return true; + } } return false; diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index e1dff63e03..e74be6929c 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -91,6 +91,7 @@ private slots: void listWrapperAsListReference(); void attachedObjectAsObject(); void listPropertyAsQJSValue(); + void stringToColor(); public slots: QObject *createAQObjectForOwnershipTest () @@ -1410,6 +1411,31 @@ void tst_qqmlengine::listPropertyAsQJSValue() QCOMPARE(prop.at(&prop, 0), &c); } +void tst_qqmlengine::stringToColor() +{ + QQmlEngine engine; + + // Make it import QtQuick, so that color becomes available. + QQmlComponent c(&engine); + c.setData("import QtQuick\nItem {}", QUrl()); + QVERIFY(c.isReady()); + QScopedPointer<QObject> o(c.create()); + + const QMetaType metaType(QMetaType::QColor); + QVariant color(metaType); + QVERIFY(engine.handle()->metaTypeFromJS( + engine.handle()->newString(QStringLiteral("#abcdef"))->asReturnedValue(), + metaType, color.data())); + QVERIFY(color.isValid()); + QCOMPARE(color.metaType(), metaType); + + QVariant variant(QStringLiteral("#abcdef")); + QVERIFY(variant.convert(metaType)); + QCOMPARE(variant.metaType(), metaType); + + QCOMPARE(color, variant); +} + QTEST_MAIN(tst_qqmlengine) #include "tst_qqmlengine.moc" |