QtQml: Do optional chain finalization also on string-y element lookup

Amends commit 86c48761dc.

Fixes: QTBUG-120168
Change-Id: I5848d8394498bafb1e897eca865d405224eaf997
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit 413e9f9cde)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ulf Hermann 2023-12-18 14:57:57 +01:00 committed by Qt Cherry-pick Bot
parent 000538769b
commit c22f45c6a4
2 changed files with 38 additions and 0 deletions

View File

@ -1294,6 +1294,7 @@ bool Codegen::visit(ArrayMemberExpression *ast)
ast->isOptional,
&m_optionalChainsStates.top().jumpsToPatch);
setExprResult(ref);
optionalChainFinalizer(ref, isTailOfChain);
return false;
}

View File

@ -311,6 +311,8 @@ private slots:
void symbolToVariant();
void garbageCollectedObjectMethodBase();
void optionalChainWithElementLookup();
public:
Q_INVOKABLE QJSValue throwingCppMethod1();
Q_INVOKABLE void throwingCppMethod2();
@ -6292,6 +6294,41 @@ void tst_QJSEngine::garbageCollectedObjectMethodBase()
}
}
void tst_QJSEngine::optionalChainWithElementLookup()
{
QJSEngine engine;
const QString program = R"js(
(function(xxx) { return xxx?.title["en"] ?? "A" })
)js";
QJSManagedValue func = QJSManagedValue(engine.evaluate(program), &engine);
QVERIFY(func.isFunction());
QCOMPARE(func.call({QJSValue::NullValue}).toString(), "A");
QCOMPARE(func.call({QJSValue::UndefinedValue}).toString(), "A");
const QJSValue nice
= engine.toScriptValue(QVariantMap { {"title", QVariantMap { {"en", "B"} } } });
QCOMPARE(func.call({nice}).toString(), "B");
const QJSValue naughty1
= engine.toScriptValue(QVariantMap { {"title", QVariantMap { {"fr", "B"} } } });
QCOMPARE(func.call({naughty1}).toString(), "A");
const QJSValue naughty2
= engine.toScriptValue(QVariantMap { {"foos", QVariantMap { {"en", "B"} } } });
QVERIFY(func.call({naughty2}).isUndefined());
QVERIFY(engine.hasError());
QCOMPARE(engine.catchError().toString(), "TypeError: Cannot read property 'en' of undefined");
QVERIFY(!engine.hasError());
QVERIFY(func.call({ QJSValue(4) }).isUndefined());
QVERIFY(engine.hasError());
QCOMPARE(engine.catchError().toString(), "TypeError: Cannot read property 'en' of undefined");
QVERIFY(!engine.hasError());
}
QTEST_MAIN(tst_QJSEngine)
#include "tst_qjsengine.moc"