diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-02-17 20:38:04 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-02-19 00:29:46 +0000 |
commit | e9e5f26c58d26a07e8e731886029fbe3bb21b98d (patch) | |
tree | bafb93a04cc91307a603d82878239c1a78c4aebb /src | |
parent | e09acc38d3af64c2136ee8b6ab4f01a83492ba17 (diff) |
QJSPrimitiveValue: Implement modulo operator
The modulo operator has special semantics in JavaScript. We need to
mirror those.
Task-number: QTBUG-84369
Change-Id: I5a4d63a01e232686832c83f2def0faf57e7359c2
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 84cf29933cee44e09590fc89ae800dd453f664ca)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/jsapi/qjsprimitivevalue.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsprimitivevalue.h b/src/qml/jsapi/qjsprimitivevalue.h index f9cc9f3089..5a82175783 100644 --- a/src/qml/jsapi/qjsprimitivevalue.h +++ b/src/qml/jsapi/qjsprimitivevalue.h @@ -48,6 +48,7 @@ #include <QtCore/qvariant.h> #include <variant> +#include <cmath> QT_BEGIN_NAMESPACE @@ -296,6 +297,33 @@ public: return operate<DivOperators>(lhs, rhs); } + friend inline QJSPrimitiveValue operator%(const QJSPrimitiveValue &lhs, + const QJSPrimitiveValue &rhs) + { + switch (lhs.type()) { + case Null: + case Boolean: + case Integer: + switch (rhs.type()) { + case Boolean: + case Integer: { + const int leftInt = lhs.toInteger(); + const int rightInt = rhs.toInteger(); + if (leftInt >= 0 && rightInt > 0) + return leftInt % rightInt; + Q_FALLTHROUGH(); + } + default: + break; + } + Q_FALLTHROUGH(); + default: + break; + } + + return std::fmod(lhs.toDouble(), rhs.toDouble()); + } + constexpr bool strictlyEquals(const QJSPrimitiveValue &other) const { const Type myType = type(); |