aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2025-12-09 13:10:16 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2025-12-13 09:13:16 +0000
commit2d94b4e6f4bf0acb207b4849957fb0a4a0d46905 (patch)
tree01e5fa5691c214c7ff6bfbf88689fbc5fcce6d4e
parentb708d35b9c5219482f98f8d42f07554c513f5b5f (diff)
QmlCompiler: Get length from strings wrapped in QJSPrimitiveValue6.10
We need to consider the original type of the value in order to see what we can do with it and we need to convert it (back) to string where necessary. Fixes: QTBUG-142550 Change-Id: Ic0eb2c7a22636cfb2d97297421b911555bd32bb1 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Sami Shalayel <sami.shalayel@qt.io> (cherry picked from commit ee6a47ab3fc0dfcfb7301562b9b6dd5807d71079) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 2b3b3988bcdeaa62584d49698a6dbf0ca3ad81a7)
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp17
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/stringLength.qml8
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp1
3 files changed, 23 insertions, 3 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index dbaec2060d..35902b889a 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -1575,7 +1575,9 @@ void QQmlJSCodeGenerator::generate_GetLookupHelper(int index)
: u"QQmlPrivate::AOTCompiledContext::InvalidStringId"_s;
const auto accumulatorIn = m_state.accumulatorIn();
const QQmlJSRegisterContent scope = m_state.accumulatorOut().scope();
- const bool isReferenceType = scope.containedType()->isReferenceType();
+ const QQmlJSRegisterContent originalScope
+ = scope.original().isValid() ? scope.original() : scope;
+ const bool isReferenceType = originalScope.containedType()->isReferenceType();
switch (m_state.accumulatorOut().variant()) {
case QQmlJSRegisterContent::Attachment: {
@@ -1637,8 +1639,9 @@ void QQmlJSCodeGenerator::generate_GetLookupHelper(int index)
const QString preparation = getLookupPreparation(
m_state.accumulatorOut(), m_state.accumulatorVariableOut, index);
generateLookup(lookup, initialization, preparation);
- } else if ((scope.containedType()->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
- || scope.contains(m_typeResolver->stringType()))
+ } else if ((originalScope.containedType()->accessSemantics()
+ == QQmlJSScope::AccessSemantics::Sequence
+ || originalScope.contains(m_typeResolver->stringType()))
&& m_jsUnitGenerator->lookupName(index) == u"length"_s) {
const QQmlJSScope::ConstPtr stored = accumulatorIn.storedType();
if (stored->isListProperty()) {
@@ -1656,6 +1659,14 @@ void QQmlJSCodeGenerator::generate_GetLookupHelper(int index)
m_state.accumulatorOut(),
m_state.accumulatorVariableIn + u".length()"_s)
+ u";\n"_s;
+ } else if (originalScope.contains(m_typeResolver->stringType())) {
+ m_body += m_state.accumulatorVariableOut + u" = "_s
+ + conversion(
+ m_typeResolver->sizeType(), m_state.accumulatorOut(),
+ conversion(m_state.accumulatorIn(), m_typeResolver->stringType(),
+ m_state.accumulatorVariableIn)
+ + u".length()"_s)
+ + u";\n"_s;
} else {
REJECT(u"access to 'length' property of sequence wrapped in non-sequence"_s);
}
diff --git a/tests/auto/qml/qmlcppcodegen/data/stringLength.qml b/tests/auto/qml/qmlcppcodegen/data/stringLength.qml
index df9bbb7f2d..d09f032d65 100644
--- a/tests/auto/qml/qmlcppcodegen/data/stringLength.qml
+++ b/tests/auto/qml/qmlcppcodegen/data/stringLength.qml
@@ -1,6 +1,14 @@
+pragma Strict
import QtQml
QtObject {
objectName: "astringb"
property int stringLength: objectName.length
+ property string a: {
+ const value = objectName;
+ if (value && value.length > 0) {
+ return value;
+ }
+ return "no"
+ }
}
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index a90e2a6050..4038376897 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -5733,6 +5733,7 @@ void tst_QmlCppCodegen::stringLength()
QScopedPointer<QObject> object(component.create());
QVERIFY(!object.isNull());
QCOMPARE(object->property("stringLength").toInt(), 8);
+ QCOMPARE(object->property("a"), u"astringb"_s);
}
void tst_QmlCppCodegen::stringToByteArray()