aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-07-29 16:13:43 +0200
committerUlf Hermann <ulf.hermann@qt.io>2022-11-24 10:45:45 +0100
commit0fbb39435e04e02ecff4a95983be102d57d29e94 (patch)
treeda456c0585de1148ac796e5d9fa3dcfaf0838d19
parentb4760d8c4c8252df2ec772c5d5d4b44d937f3df4 (diff)
QmlCompiler: Generate correct code for non-double constants
This can actually not happen because we only use this instruction for doubles. However, it's clearly intended to be compatible with other types. Fixes: QTBUG-108820 Change-Id: I50618913373560eee6be2aeb2a9be387ff8ba753 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit ed3da31fd0248d48d960de566293cd9e8d12b9f4)
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp40
-rw-r--r--src/qmlcompiler/qqmljstyperesolver.cpp2
2 files changed, 33 insertions, 9 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index e288050f5c..be0a937fc0 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -309,17 +309,41 @@ void QQmlJSCodeGenerator::generate_LoadConst(int index)
{
INJECT_TRACE_INFO(generate_LoadConst);
- auto encodedConst = m_jsUnitGenerator->constant(index);
- double value = QV4::StaticValue::fromReturnedValue(encodedConst).doubleValue();
- m_body += m_state.accumulatorVariableOut;
+ // You cannot actually get it to generate LoadConst for anything but double. We have
+ // a numer of specialized instructions for the other types, after all. However, let's
+ // play it safe.
- // ### handle other types?
- m_body += u" = "_s + conversion(
- m_typeResolver->intType(), m_state.accumulatorOut().storedType(),
- toNumericString(value));
+ const QV4::ReturnedValue encodedConst = m_jsUnitGenerator->constant(index);
+ const QV4::StaticValue value = QV4::StaticValue::fromReturnedValue(encodedConst);
+ const QQmlJSScope::ConstPtr type = m_typeResolver->typeForConst(encodedConst);
+
+ m_body += m_state.accumulatorVariableOut + u" = "_s;
+ if (type == m_typeResolver->realType()) {
+ m_body += conversion(
+ type, m_state.accumulatorOut().storedType(),
+ toNumericString(value.doubleValue()));
+ } else if (type == m_typeResolver->intType()) {
+ m_body += conversion(
+ type, m_state.accumulatorOut().storedType(),
+ QString::number(value.integerValue()));
+ } else if (type == m_typeResolver->boolType()) {
+ m_body += conversion(
+ type, m_state.accumulatorOut().storedType(),
+ value.booleanValue() ? u"true"_s : u"false"_s);
+ } else if (type == m_typeResolver->voidType()) {
+ m_body += conversion(
+ type, m_state.accumulatorOut().storedType(),
+ QString());
+ } else if (type == m_typeResolver->nullType()) {
+ m_body += conversion(
+ type, m_state.accumulatorOut().storedType(),
+ u"nullptr"_s);
+ } else {
+ reject(u"unsupported constant type"_s);
+ }
m_body += u";\n"_s;
- generateOutputVariantConversion(m_typeResolver->intType());
+ generateOutputVariantConversion(type);
}
void QQmlJSCodeGenerator::generate_LoadZero()
diff --git a/src/qmlcompiler/qqmljstyperesolver.cpp b/src/qmlcompiler/qqmljstyperesolver.cpp
index 3cba571e44..5f5119efce 100644
--- a/src/qmlcompiler/qqmljstyperesolver.cpp
+++ b/src/qmlcompiler/qqmljstyperesolver.cpp
@@ -169,7 +169,7 @@ QQmlJSScope::ConstPtr QQmlJSTypeResolver::typeForConst(QV4::ReturnedValue rv) co
return realType();
if (value.isNull())
- return jsPrimitiveType();
+ return nullType();
return {};
}