aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@qt.io>2017-06-22 11:16:11 +0200
committerErik Verbruggen <erik.verbruggen@qt.io>2017-06-22 09:53:25 +0000
commit9cc9101f0416ebc352d157534b7c2ce009a44705 (patch)
tree2cbf7404bc3c25058eb91aa187ac3827d73459ef
parent4ac85039a2e3cd092ad51ae726e01d1eef46e37a (diff)
Encode numeric literals as int when possible
Change-Id: I5ecc406f06a193b470eb9ac376da6b9f752d01cb Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r--src/qml/compiler/qv4codegen.cpp2
-rw-r--r--src/qml/compiler/qv4instr_moth.cpp16
-rw-r--r--src/qml/jsruntime/qv4value_p.h8
3 files changed, 22 insertions, 4 deletions
diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp
index ae0faf3b72..b251938db0 100644
--- a/src/qml/compiler/qv4codegen.cpp
+++ b/src/qml/compiler/qv4codegen.cpp
@@ -2071,7 +2071,7 @@ bool Codegen::visit(NumericLiteral *ast)
if (hasError)
return false;
- _expr.result = Reference::fromConst(this, QV4::Encode(ast->value));
+ _expr.result = Reference::fromConst(this, QV4::Encode::smallestNumber(ast->value));
return false;
}
diff --git a/src/qml/compiler/qv4instr_moth.cpp b/src/qml/compiler/qv4instr_moth.cpp
index 8cf79930a5..c83437800e 100644
--- a/src/qml/compiler/qv4instr_moth.cpp
+++ b/src/qml/compiler/qv4instr_moth.cpp
@@ -52,20 +52,30 @@ int Instr::size(Type type)
#undef MOTH_RETURN_INSTR_SIZE
}
-QByteArray alignedNumber(int n) {
+static QByteArray alignedNumber(int n) {
QByteArray number = QByteArray::number(n);
while (number.size() < 12)
number.prepend(' ');
return number;
}
-QString toString(QV4::ReturnedValue v)
+static QString toString(QV4::ReturnedValue v)
{
#ifdef V4_BOOTSTRAP
return QStringLiteral("string-const(%1)").arg(v);
#else // !V4_BOOTSTRAP
Value val = Value::fromReturnedValue(v);
- return QLatin1String("const(") + (val.isEmpty() ? QStringLiteral("empty") : val.toQStringNoThrow()) + QLatin1String(")");
+ QString result;
+ if (val.isInt32())
+ result = QLatin1String("int ");
+ else if (val.isDouble())
+ result = QLatin1String("double ");
+ result += QLatin1String("const(");
+ if (val.isEmpty())
+ result += QLatin1String("empty");
+ else
+ result += val.toQStringNoThrow();
+ return result + QLatin1String(")");
#endif // V4_BOOTSTRAP
}
diff --git a/src/qml/jsruntime/qv4value_p.h b/src/qml/jsruntime/qv4value_p.h
index 50cecb6598..0e7fc844fa 100644
--- a/src/qml/jsruntime/qv4value_p.h
+++ b/src/qml/jsruntime/qv4value_p.h
@@ -51,6 +51,7 @@
//
#include <limits.h>
+#include <cmath>
#include <QtCore/QString>
#include "qv4global_p.h"
@@ -687,6 +688,13 @@ struct Encode {
val = Value::fromHeapObject(o).asReturnedValue();
}
+ static ReturnedValue smallestNumber(double d) {
+ if (static_cast<int>(d) == d && !(d == 0. && std::signbit(d)))
+ return Encode(static_cast<int>(d));
+ else
+ return Encode(d);
+ }
+
operator ReturnedValue() const {
return val;
}