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