diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-11 14:23:42 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-11-14 10:19:08 +0100 |
commit | 1d3d7437bab7c3a0c3222de3a00bcf2c9437ae76 (patch) | |
tree | f01eb6c6a415cbdb2e0ff5b031624228a9282eed /tests/auto/qml/qjsengine | |
parent | 0dbb801920ea04d99834f7e89e02241f12bd54f8 (diff) |
QML: Fix the most blatant TDZ violations
When reading a let or const register before its declaration we can be
sure this is invalid.
Task-number: QTBUG-108362
Fixes: QTBUG-77428
Change-Id: I7e8f8b46079860f00c051c1a91f773dc8cdd5595
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qjsengine')
-rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index 928e9f4e99..1bd6786944 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -268,6 +268,9 @@ private slots: void staticInNestedClasses(); void callElement(); + void writeTdzBeforeDeclaration_data(); + void writeTdzBeforeDeclaration(); + public: Q_INVOKABLE QJSValue throwingCppMethod1(); Q_INVOKABLE void throwingCppMethod2(); @@ -5692,6 +5695,58 @@ void tst_QJSEngine::callElement() QCOMPARE(engine.evaluate(program).toString(), u"a"_s); } +void tst_QJSEngine::writeTdzBeforeDeclaration_data() +{ + QTest::addColumn<QString>("type"); + QTest::addRow("let") << u"let"_s; + QTest::addRow("const") << u"const"_s; +} + +void tst_QJSEngine::writeTdzBeforeDeclaration() +{ + QFETCH(QString, type); + type.resize(8, u' '); // pad with some spaces, so that the columns match. + + QJSEngine engine; + const QString program1 = uR"( + (function() { + a = 5; + %1 a = 1; + return a; + })(); + )"_s.arg(type); + + QTest::ignoreMessage( + QtWarningMsg, + ":3:13 Variable \"a\" is used before its declaration at 4:22."); + + const QJSValue result1 = engine.evaluate(program1); + QVERIFY(result1.isError()); + QCOMPARE(result1.toString(), u"ReferenceError: a is not defined"_s); + + const QString program2 = uR"( + (function() { + function stringify(x) { return x + "" } + var c = ""; + for (var a = 0; a < 10; ++a) { + if (a > 0) { + c += stringify(b); + } + %1 b = 10; + } + return c; + })(); + )"_s.arg(type); + + QTest::ignoreMessage( + QtWarningMsg, + ":7:36 Variable \"b\" is used before its declaration at 9:26."); + + const QJSValue result2 = engine.evaluate(program2); + QVERIFY(result2.isError()); + QCOMPARE(result2.toString(), u"ReferenceError: b is not defined"_s); +} + QTEST_MAIN(tst_QJSEngine) #include "tst_qjsengine.moc" |