V4 Engine: Don't try to convert JS functions to other types

When converting a JS value to a variant, if we notice that we get a
QJSValue again, there is no point in trying to convert it further. We'll
just run into infinite recursion.

Pick-to: 6.3
Fixes: QTBUG-102545
Change-Id: I0a40e21287e5460e5e214101aabe8d2b4bf0afad
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2022-05-06 14:07:29 +02:00
parent 4770ace5e4
commit 2bed3d5f0f
2 changed files with 8 additions and 1 deletions

View File

@ -168,7 +168,10 @@ ReturnedValue throwTypeError(const FunctionObject *b, const QV4::Value *, const
template <typename ReturnType>
ReturnType convertJSValueToVariantType(const QJSValue &value)
{
return value.toVariant().value<ReturnType>();
const QVariant variant = value.toVariant();
return variant.metaType() == QMetaType::fromType<QJSValue>()
? ReturnType()
: variant.value<ReturnType>();
}
struct JSArrayIterator {

View File

@ -2871,6 +2871,10 @@ void tst_QJSValue::jsFunctionInVariant()
QTest::ignoreMessage(QtDebugMsg, "direct call");
log.callWithInstance(console, {"direct call"});
}
const QVariant var = log.toVariant();
QCOMPARE(var.metaType(), QMetaType::fromType<QJSValue>());
QCOMPARE(var.value<QVariantMap>(), QVariantMap()); // Does not recurse infinitely
}
void tst_QJSValue::integerToEnum()