Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4numberobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6#include "qv4runtime_p.h"
7
8#include <QtCore/qnumeric.h>
9#include <QtCore/qmath.h>
10#include <QtCore/QDebug>
11#include <cassert>
12#include <limits>
13
14using namespace QV4;
15
18
19namespace {
20struct NumberLocaleHolder : public NumberLocale
21{
22 NumberLocaleHolder() {}
23};
24
25Q_GLOBAL_STATIC(NumberLocaleHolder, numberLocaleHolder)
26} // namespace
27
28NumberLocale::NumberLocale() : QLocale(QLocale::C),
29 // -128 means shortest string that can accurately represent the number.
30 defaultDoublePrecision(0xffffff80)
31{
32 setNumberOptions(QLocale::OmitGroupSeparator |
33 QLocale::OmitLeadingZeroInExponent |
34 QLocale::IncludeTrailingZeroesAfterDot);
35}
36
37const NumberLocale *NumberLocale::instance()
38{
39 return numberLocaleHolder();
40}
41
42void Heap::NumberCtor::init(QV4::ExecutionEngine *engine)
43{
44 Heap::FunctionObject::init(engine, QStringLiteral("Number"));
45}
46
47ReturnedValue NumberCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
48{
49 auto v4 = f->engine();
50 double dbl = argc ? argv[0].toNumber() : 0.;
51
52 ReturnedValue o = Encode(f->engine()->newNumberObject(dbl));
53 if (!newTarget)
54 return o;
55 Scope scope(v4);
56 ScopedObject obj(scope, o);
57 obj->setProtoFromNewTarget(newTarget);
58 return obj->asReturnedValue();
59}
60
61ReturnedValue NumberCtor::virtualCall(const FunctionObject *, const Value *, const Value *argv, int argc)
62{
63 double dbl = argc ? argv[0].toNumber() : 0.;
64 return Encode(dbl);
65}
66
67void NumberPrototype::init(ExecutionEngine *engine, Object *ctor)
68{
69 Scope scope(engine);
70 ScopedObject o(scope);
71 ctor->defineReadonlyProperty(engine->id_prototype(), (o = this));
72 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(1));
73
74 ctor->defineReadonlyProperty(QStringLiteral("NaN"), Value::fromDouble(qt_qnan()));
75 ctor->defineReadonlyProperty(QStringLiteral("NEGATIVE_INFINITY"), Value::fromDouble(-qInf()));
76 ctor->defineReadonlyProperty(QStringLiteral("POSITIVE_INFINITY"), Value::fromDouble(qInf()));
77 ctor->defineReadonlyProperty(QStringLiteral("MAX_VALUE"), Value::fromDouble(1.7976931348623158e+308));
78 ctor->defineReadonlyProperty(QStringLiteral("EPSILON"), Value::fromDouble(std::numeric_limits<double>::epsilon()));
79 ctor->defineReadonlyProperty(QStringLiteral("MAX_SAFE_INTEGER"), Value::fromDouble(9007199254740991));
80 ctor->defineReadonlyProperty(QStringLiteral("MIN_SAFE_INTEGER"), Value::fromDouble(-9007199254740991));
81
82QT_WARNING_PUSH
83QT_WARNING_DISABLE_INTEL(239)
84 ctor->defineReadonlyProperty(QStringLiteral("MIN_VALUE"), Value::fromDouble(5e-324));
85QT_WARNING_POP
86
87 ctor->defineDefaultProperty(QStringLiteral("isFinite"), method_isFinite, 1);
88 ctor->defineDefaultProperty(QStringLiteral("isInteger"), method_isInteger, 1);
89 ctor->defineDefaultProperty(QStringLiteral("isSafeInteger"), method_isSafeInteger, 1);
90 ctor->defineDefaultProperty(QStringLiteral("isNaN"), method_isNaN, 1);
91
92 defineDefaultProperty(QStringLiteral("constructor"), (o = ctor));
93 defineDefaultProperty(engine->id_toString(), method_toString, 1);
94 defineDefaultProperty(engine->id_toLocaleString(), method_toLocaleString);
95 defineDefaultProperty(engine->id_valueOf(), method_valueOf);
96 defineDefaultProperty(QStringLiteral("toFixed"), method_toFixed, 1);
97 defineDefaultProperty(QStringLiteral("toExponential"), method_toExponential, 1);
98 defineDefaultProperty(QStringLiteral("toPrecision"), method_toPrecision, 1);
99}
100
101inline ReturnedValue thisNumberValue(ExecutionEngine *v4, const Value *thisObject)
102{
103 if (thisObject->isNumber())
104 return thisObject->asReturnedValue();
105 const NumberObject *n = thisObject->as<NumberObject>();
106 if (!n) {
107 v4->throwTypeError();
108 return Encode::undefined();
109 }
110 return Encode(n->value());
111}
112
113inline double thisNumber(ExecutionEngine *engine, const Value *thisObject)
114{
115 if (thisObject->isNumber())
116 return thisObject->asDouble();
117 const NumberObject *n = thisObject->as<NumberObject>();
118 if (!n) {
119 engine->throwTypeError();
120 return 0;
121 }
122 return n->value();
123}
124
125ReturnedValue NumberPrototype::method_isFinite(const FunctionObject *, const Value *, const Value *argv, int argc)
126{
127 if (!argc || !argv[0].isNumber())
128 return Encode(false);
129
130 double v = argv[0].toNumber();
131 return Encode(!std::isnan(v) && !qt_is_inf(v));
132}
133
134ReturnedValue NumberPrototype::method_isInteger(const FunctionObject *, const Value *, const Value *argv, int argc)
135{
136 if (!argc)
137 return Encode(false);
138
139 const Value &v = argv[0];
140 if (!v.isNumber())
141 return Encode(false);
142
143 double dv = v.toNumber();
144 if (std::isnan(dv) || qt_is_inf(dv))
145 return Encode(false);
146
147 double iv = v.toInteger();
148 return Encode(dv == iv);
149}
150
151ReturnedValue NumberPrototype::method_isSafeInteger(const FunctionObject *, const Value *, const Value *argv, int argc)
152{
153 if (!argc)
154 return Encode(false);
155
156 const Value &v = argv[0];
157 if (!v.isNumber())
158 return Encode(false);
159
160 double dv = v.toNumber();
161 if (std::isnan(dv) || qt_is_inf(dv))
162 return Encode(false);
163
164 double iv = v.toInteger();
165 return Encode(dv == iv && std::fabs(iv) <= (1LL << 53) - 1);
166}
167
168ReturnedValue NumberPrototype::method_isNaN(const FunctionObject *, const Value *, const Value *argv, int argc)
169{
170 if (!argc || !argv[0].isNumber())
171 return Encode(false);
172
173 double v = argv[0].toNumber();
174 // cast to bool explicitly as std::isnan() may give us ::isnan(), which
175 // sometimes returns an int and we don't want the Encode(int) overload.
176 return Encode(bool(std::isnan(v)));
177}
178
179ReturnedValue NumberPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
180{
181 ExecutionEngine *v4 = b->engine();
182 double num = thisNumber(v4, thisObject);
183 if (v4->hasException)
184 return QV4::Encode::undefined();
185
186 if (argc && !argv[0].isUndefined()) {
187 int radix = argv[0].toInt32();
188 if (radix < 2 || radix > 36) {
189 return v4->throwError(QStringLiteral("Number.prototype.toString: %0 is not a valid radix").arg(radix));
190 }
191
192 QString str;
193 RuntimeHelpers::numberToString(&str, num, radix);
194 return Encode(v4->newString(str));
195 }
196
197 return Encode(Value::fromDouble(num).toString(v4));
198}
199
200ReturnedValue NumberPrototype::method_toLocaleString(const FunctionObject *b, const Value *thisObject, const Value *, int)
201{
202 Scope scope(b);
203 ScopedValue v(scope, thisNumberValue(b->engine(), thisObject));
204 return Encode(v->toString(scope.engine));
205}
206
207ReturnedValue NumberPrototype::method_valueOf(const FunctionObject *b, const Value *thisObject, const Value *, int)
208{
209 return thisNumberValue(b->engine(), thisObject);
210}
211
212ReturnedValue NumberPrototype::method_toFixed(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
213{
214 ExecutionEngine *v4 = b->engine();
215 double v = thisNumber(v4, thisObject);
216 if (v4->hasException)
217 return QV4::Encode::undefined();
218
219 double fdigits = 0;
220
221 if (argc > 0)
222 fdigits = argv[0].toInteger();
223
224 if (std::isnan(fdigits))
225 fdigits = 0;
226
227 if (fdigits < 0 || fdigits > 100)
228 return v4->throwRangeError(*thisObject);
229
230 QString str;
231 if (std::isnan(v))
232 str = QStringLiteral("NaN");
233 else if (qt_is_inf(v))
234 str = QString::fromLatin1(v < 0 ? "-Infinity" : "Infinity");
235 else if (v < 1.e21)
236 str = NumberLocale::instance()->toString(v, 'f', int(fdigits));
237 else {
238 return Encode(RuntimeHelpers::stringFromNumber(v4, v));
239 }
240 return Encode(v4->newString(str));
241}
242
243ReturnedValue NumberPrototype::method_toExponential(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
244{
245 ExecutionEngine *v4 = b->engine();
246 double d = thisNumber(v4, thisObject);
247 if (v4->hasException)
248 return QV4::Encode::undefined();
249
250 bool defaultDigits = !argc || argv[0].isUndefined();
251 int fdigits = !defaultDigits ? argv[0].toInteger() : NumberLocale::instance()->defaultDoublePrecision;
252 if (v4->hasException)
253 return QV4::Encode::undefined();
254
255 if (std::isnan(d))
256 return Encode(v4->newString(QLatin1String("NaN")));
257
258 if (qIsInf(d))
259 return Encode(v4->newString(QLatin1String(d < 0 ? "-Infinity" : "Infinity")));
260
261 if (!defaultDigits && (fdigits < 0 || fdigits > 100)) {
262 Scope scope(v4);
263 ScopedString error(scope, v4->newString(QStringLiteral("Number.prototype.toExponential: fractionDigits out of range")));
264 return v4->throwRangeError(error);
265 }
266
267 QString result = NumberLocale::instance()->toString(d, 'e', fdigits);
268 return Encode(v4->newString(result));
269}
270
271ReturnedValue NumberPrototype::method_toPrecision(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
272{
273 Scope scope(b);
274 ScopedValue v(scope, thisNumberValue(scope.engine, thisObject));
275 if (scope.hasException())
276 return QV4::Encode::undefined();
277 double d = v->asDouble();
278
279 if (!argc || argv[0].isUndefined())
280 return Encode(v->toString(scope.engine));
281
282 int precision = argv[0].toInt32();
283 if (scope.hasException())
284 return QV4::Encode::undefined();
285
286 if (std::isnan(d))
287 return Encode(scope.engine->newString(QLatin1String("NaN")));
288
289 if (qIsInf(d))
290 return Encode(scope.engine->newString(QLatin1String(d < 0 ? "-Infinity" : "Infinity")));
291
292 if (precision < 1 || precision > 100) {
293 ScopedString error(scope, scope.engine->newString(QStringLiteral("Number.prototype.toPrecision: precision out of range")));
294 return scope.engine->throwRangeError(error);
295 }
296
297 QString result = NumberLocale::instance()->toString(d, 'g', precision);
298 return Encode(scope.engine->newString(result));
299}
Definition qjsvalue.h:23
DEFINE_OBJECT_VTABLE(NumberCtor)
DEFINE_OBJECT_VTABLE(NumberObject)