1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qv4numberobject_p.h"
41#include "qv4runtime_p.h"
42#include "qv4string_p.h"
43
44#include <QtCore/qnumeric.h>
45#include <QtCore/qmath.h>
46#include <QtCore/QDebug>
47#include <cassert>
48#include <limits>
49
50using namespace QV4;
51
52DEFINE_OBJECT_VTABLE(NumberCtor);
53DEFINE_OBJECT_VTABLE(NumberObject);
54
55struct NumberLocaleHolder : public NumberLocale
56{
57 NumberLocaleHolder() {}
58};
59
60Q_GLOBAL_STATIC(NumberLocaleHolder, numberLocaleHolder)
61
62NumberLocale::NumberLocale() : QLocale(QLocale::C),
63 // -128 means shortest string that can accurately represent the number.
64 defaultDoublePrecision(0xffffff80)
65{
66 setNumberOptions(QLocale::OmitGroupSeparator |
67 QLocale::OmitLeadingZeroInExponent |
68 QLocale::IncludeTrailingZeroesAfterDot);
69}
70
71const NumberLocale *NumberLocale::instance()
72{
73 return numberLocaleHolder();
74}
75
76void Heap::NumberCtor::init(QV4::ExecutionContext *scope)
77{
78 Heap::FunctionObject::init(scope, QStringLiteral("Number"));
79}
80
81ReturnedValue NumberCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
82{
83 auto v4 = f->engine();
84 double dbl = argc ? argv[0].toNumber() : 0.;
85
86 ReturnedValue o = Encode(f->engine()->newNumberObject(value: dbl));
87 if (!newTarget)
88 return o;
89 Scope scope(v4);
90 ScopedObject obj(scope, o);
91 obj->setProtoFromNewTarget(newTarget);
92 return obj->asReturnedValue();
93}
94
95ReturnedValue NumberCtor::virtualCall(const FunctionObject *, const Value *, const Value *argv, int argc)
96{
97 double dbl = argc ? argv[0].toNumber() : 0.;
98 return Encode(dbl);
99}
100
101void NumberPrototype::init(ExecutionEngine *engine, Object *ctor)
102{
103 Scope scope(engine);
104 ScopedObject o(scope);
105 ctor->defineReadonlyProperty(name: engine->id_prototype(), value: (o = this));
106 ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1));
107
108 ctor->defineReadonlyProperty(QStringLiteral("NaN"), value: Value::fromDouble(d: qt_qnan()));
109 ctor->defineReadonlyProperty(QStringLiteral("NEGATIVE_INFINITY"), value: Value::fromDouble(d: -qInf()));
110 ctor->defineReadonlyProperty(QStringLiteral("POSITIVE_INFINITY"), value: Value::fromDouble(d: qInf()));
111 ctor->defineReadonlyProperty(QStringLiteral("MAX_VALUE"), value: Value::fromDouble(d: 1.7976931348623158e+308));
112 ctor->defineReadonlyProperty(QStringLiteral("EPSILON"), value: Value::fromDouble(d: std::numeric_limits<double>::epsilon()));
113 ctor->defineReadonlyProperty(QStringLiteral("MAX_SAFE_INTEGER"), value: Value::fromDouble(d: 9007199254740991));
114 ctor->defineReadonlyProperty(QStringLiteral("MIN_SAFE_INTEGER"), value: Value::fromDouble(d: -9007199254740991));
115
116QT_WARNING_PUSH
117QT_WARNING_DISABLE_INTEL(239)
118 ctor->defineReadonlyProperty(QStringLiteral("MIN_VALUE"), value: Value::fromDouble(d: 5e-324));
119QT_WARNING_POP
120
121 ctor->defineDefaultProperty(QStringLiteral("isFinite"), code: method_isFinite, argumentCount: 1);
122 ctor->defineDefaultProperty(QStringLiteral("isInteger"), code: method_isInteger, argumentCount: 1);
123 ctor->defineDefaultProperty(QStringLiteral("isSafeInteger"), code: method_isSafeInteger, argumentCount: 1);
124 ctor->defineDefaultProperty(QStringLiteral("isNaN"), code: method_isNaN, argumentCount: 1);
125
126 defineDefaultProperty(QStringLiteral("constructor"), value: (o = ctor));
127 defineDefaultProperty(name: engine->id_toString(), code: method_toString, argumentCount: 1);
128 defineDefaultProperty(name: engine->id_toLocaleString(), code: method_toLocaleString);
129 defineDefaultProperty(name: engine->id_valueOf(), code: method_valueOf);
130 defineDefaultProperty(QStringLiteral("toFixed"), code: method_toFixed, argumentCount: 1);
131 defineDefaultProperty(QStringLiteral("toExponential"), code: method_toExponential, argumentCount: 1);
132 defineDefaultProperty(QStringLiteral("toPrecision"), code: method_toPrecision, argumentCount: 1);
133}
134
135inline ReturnedValue thisNumberValue(ExecutionEngine *v4, const Value *thisObject)
136{
137 if (thisObject->isNumber())
138 return thisObject->asReturnedValue();
139 const NumberObject *n = thisObject->as<NumberObject>();
140 if (!n) {
141 v4->throwTypeError();
142 return Encode::undefined();
143 }
144 return Encode(n->value());
145}
146
147inline double thisNumber(ExecutionEngine *engine, const Value *thisObject)
148{
149 if (thisObject->isNumber())
150 return thisObject->asDouble();
151 const NumberObject *n = thisObject->as<NumberObject>();
152 if (!n) {
153 engine->throwTypeError();
154 return 0;
155 }
156 return n->value();
157}
158
159ReturnedValue NumberPrototype::method_isFinite(const FunctionObject *, const Value *, const Value *argv, int argc)
160{
161 if (!argc || !argv[0].isNumber())
162 return Encode(false);
163
164 double v = argv[0].toNumber();
165 return Encode(!std::isnan(x: v) && !qt_is_inf(d: v));
166}
167
168ReturnedValue NumberPrototype::method_isInteger(const FunctionObject *, const Value *, const Value *argv, int argc)
169{
170 if (!argc)
171 return Encode(false);
172
173 const Value &v = argv[0];
174 if (!v.isNumber())
175 return Encode(false);
176
177 double dv = v.toNumber();
178 if (std::isnan(x: dv) || qt_is_inf(d: dv))
179 return Encode(false);
180
181 double iv = v.toInteger();
182 return Encode(dv == iv);
183}
184
185ReturnedValue NumberPrototype::method_isSafeInteger(const FunctionObject *, const Value *, const Value *argv, int argc)
186{
187 if (!argc)
188 return Encode(false);
189
190 const Value &v = argv[0];
191 if (!v.isNumber())
192 return Encode(false);
193
194 double dv = v.toNumber();
195 if (std::isnan(x: dv) || qt_is_inf(d: dv))
196 return Encode(false);
197
198 double iv = v.toInteger();
199 return Encode(dv == iv && std::fabs(x: iv) <= (1LL << 53) - 1);
200}
201
202ReturnedValue NumberPrototype::method_isNaN(const FunctionObject *, const Value *, const Value *argv, int argc)
203{
204 if (!argc || !argv[0].isNumber())
205 return Encode(false);
206
207 double v = argv[0].toNumber();
208 // cast to bool explicitly as std::isnan() may give us ::isnan(), which
209 // sometimes returns an int and we don't want the Encode(int) overload.
210 return Encode(bool(std::isnan(x: v)));
211}
212
213ReturnedValue NumberPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
214{
215 ExecutionEngine *v4 = b->engine();
216 double num = thisNumber(engine: v4, thisObject);
217 if (v4->hasException)
218 return QV4::Encode::undefined();
219
220 if (argc && !argv[0].isUndefined()) {
221 int radix = argv[0].toInt32();
222 if (radix < 2 || radix > 36) {
223 return v4->throwError(QStringLiteral("Number.prototype.toString: %0 is not a valid radix").arg(a: radix));
224 }
225
226 QString str;
227 RuntimeHelpers::numberToString(result: &str, num, radix);
228 return Encode(v4->newString(s: str));
229 }
230
231 return Encode(Value::fromDouble(d: num).toString(e: v4));
232}
233
234ReturnedValue NumberPrototype::method_toLocaleString(const FunctionObject *b, const Value *thisObject, const Value *, int)
235{
236 Scope scope(b);
237 ScopedValue v(scope, thisNumberValue(v4: b->engine(), thisObject));
238 return Encode(v->toString(e: scope.engine));
239}
240
241ReturnedValue NumberPrototype::method_valueOf(const FunctionObject *b, const Value *thisObject, const Value *, int)
242{
243 return thisNumberValue(v4: b->engine(), thisObject);
244}
245
246ReturnedValue NumberPrototype::method_toFixed(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
247{
248 ExecutionEngine *v4 = b->engine();
249 double v = thisNumber(engine: v4, thisObject);
250 if (v4->hasException)
251 return QV4::Encode::undefined();
252
253 double fdigits = 0;
254
255 if (argc > 0)
256 fdigits = argv[0].toInteger();
257
258 if (std::isnan(x: fdigits))
259 fdigits = 0;
260
261 if (fdigits < 0 || fdigits > 100)
262 return v4->throwRangeError(value: *thisObject);
263
264 QString str;
265 if (std::isnan(x: v))
266 str = QStringLiteral("NaN");
267 else if (qt_is_inf(d: v))
268 str = QString::fromLatin1(str: v < 0 ? "-Infinity" : "Infinity");
269 else if (v < 1.e21)
270 str = NumberLocale::instance()->toString(i: v, f: 'f', prec: int(fdigits));
271 else {
272 return Encode(RuntimeHelpers::stringFromNumber(engine: v4, number: v));
273 }
274 return Encode(v4->newString(s: str));
275}
276
277ReturnedValue NumberPrototype::method_toExponential(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
278{
279 ExecutionEngine *v4 = b->engine();
280 double d = thisNumber(engine: v4, thisObject);
281 if (v4->hasException)
282 return QV4::Encode::undefined();
283
284 bool defaultDigits = !argc || argv[0].isUndefined();
285 int fdigits = !defaultDigits ? argv[0].toInteger() : NumberLocale::instance()->defaultDoublePrecision;
286 if (v4->hasException)
287 return QV4::Encode::undefined();
288
289 if (std::isnan(x: d))
290 return Encode(v4->newString(s: QLatin1String("NaN")));
291
292 if (qIsInf(d))
293 return Encode(v4->newString(s: QLatin1String(d < 0 ? "-Infinity" : "Infinity")));
294
295 if (!defaultDigits && (fdigits < 0 || fdigits > 100)) {
296 Scope scope(v4);
297 ScopedString error(scope, v4->newString(QStringLiteral("Number.prototype.toExponential: fractionDigits out of range")));
298 return v4->throwRangeError(value: error);
299 }
300
301 QString result = NumberLocale::instance()->toString(i: d, f: 'e', prec: fdigits);
302 return Encode(v4->newString(s: result));
303}
304
305ReturnedValue NumberPrototype::method_toPrecision(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
306{
307 Scope scope(b);
308 ScopedValue v(scope, thisNumberValue(v4: scope.engine, thisObject));
309 if (scope.engine->hasException)
310 return QV4::Encode::undefined();
311 double d = v->asDouble();
312
313 if (!argc || argv[0].isUndefined())
314 return Encode(v->toString(e: scope.engine));
315
316 int precision = argv[0].toInt32();
317 if (scope.engine->hasException)
318 return QV4::Encode::undefined();
319
320 if (std::isnan(x: d))
321 return Encode(scope.engine->newString(s: QLatin1String("NaN")));
322
323 if (qIsInf(d))
324 return Encode(scope.engine->newString(s: QLatin1String(d < 0 ? "-Infinity" : "Infinity")));
325
326 if (precision < 1 || precision > 100) {
327 ScopedString error(scope, scope.engine->newString(QStringLiteral("Number.prototype.toPrecision: precision out of range")));
328 return scope.engine->throwRangeError(value: error);
329 }
330
331 QString result = NumberLocale::instance()->toString(i: d, f: 'g', prec: precision);
332 return Encode(scope.engine->newString(s: result));
333}
334

source code of qtdeclarative/src/qml/jsruntime/qv4numberobject.cpp