aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-02-17 10:02:51 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-02-18 20:39:50 +0000
commit9b890ab5d0372e163feaf661f354ccef8f6018e2 (patch)
tree2534b8fa711b441f76b4f72f7125714badf94518
parent17f4cd3024c56364c0296b40c2cfe36fefa19b2c (diff)
QmlCompiler: Correctly encode inf/nan/-0 into C++
Fixes: QTBUG-100947 Change-Id: If0b05adac91f687daf697f3510e4cf48e7de4537 Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit 4c716dd19cf88349acc33eddae28c4e9222a3800) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp13
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt1
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/infinities.qml9
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp23
4 files changed, 46 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index 7a512f5d47..93b69474db 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -426,6 +426,19 @@ static QString toNumericString(double value)
return QString::number(i);
}
+ switch (qFpClassify(value)) {
+ case FP_INFINITE: {
+ const QString inf = u"std::numeric_limits<double>::infinity()"_qs;
+ return std::signbit(value) ? (u'-' + inf) : inf;
+ }
+ case FP_NAN:
+ return u"std::numeric_limits<double>::quiet_NaN()"_qs;
+ case FP_ZERO:
+ return std::signbit(value) ? u"-0.0"_qs : u"0"_qs;
+ default:
+ break;
+ }
+
return QString::number(value, 'f', std::numeric_limits<double>::max_digits10);
}
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
index 053c3bccc2..f51e72dcbd 100644
--- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
+++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
@@ -67,6 +67,7 @@ set(qml_files
immediateQuit.qml
imports/QmlBench/Globals.qml
importsFromImportPath.qml
+ infinities.qml
invisibleBase.qml
intEnumCompare.qml
intOverflow.qml
diff --git a/tests/auto/qml/qmlcppcodegen/data/infinities.qml b/tests/auto/qml/qmlcppcodegen/data/infinities.qml
new file mode 100644
index 0000000000..5813d7a3ac
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/infinities.qml
@@ -0,0 +1,9 @@
+import QtQml
+
+QtObject {
+ property real positiveInfinity: Infinity
+ property real negativeInfinity: -Infinity
+ property real positiveZero: 0.0
+ property real negativeZero: -0.0
+ property real naN: NaN
+}
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index 99376491fd..b04eb011b3 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -117,6 +117,7 @@ private slots:
void revisions();
void invisibleBase();
void notEqualsInt();
+ void infinities();
};
void tst_QmlCppCodegen::simpleBinding()
@@ -1753,6 +1754,28 @@ void tst_QmlCppCodegen::notEqualsInt()
QCOMPARE(t->property("text").toString(), u"Bar"_qs);
}
+void tst_QmlCppCodegen::infinities()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, QUrl(u"qrc:/TestTypes/infinities.qml"_qs));
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+ QScopedPointer<QObject> o(c.create());
+ QVERIFY(o);
+
+ QCOMPARE(o->property("positiveInfinity").toDouble(), std::numeric_limits<double>::infinity());
+ QCOMPARE(o->property("negativeInfinity").toDouble(), -std::numeric_limits<double>::infinity());
+
+ const double positiveZero = o->property("positiveZero").toDouble();
+ QCOMPARE(positiveZero, 0.0);
+ QVERIFY(!std::signbit(positiveZero));
+
+ const double negativeZero = o->property("negativeZero").toDouble();
+ QCOMPARE(negativeZero, -0.0);
+ QVERIFY(std::signbit(negativeZero));
+
+ QVERIFY(qIsNaN(o->property("naN").toDouble()));
+}
+
void tst_QmlCppCodegen::runInterpreted()
{
if (qEnvironmentVariableIsSet("QV4_FORCE_INTERPRETER"))