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
50 using namespace QV4;
51
52 DEFINE_OBJECT_VTABLE(NumberCtor);
53 DEFINE_OBJECT_VTABLE(NumberObject);
54
55 struct NumberLocaleHolder : public NumberLocale
56 {
NumberLocaleHolderNumberLocaleHolder57 NumberLocaleHolder() {}
58 };
59
Q_GLOBAL_STATIC(NumberLocaleHolder,numberLocaleHolder)60 Q_GLOBAL_STATIC(NumberLocaleHolder, numberLocaleHolder)
61
62 NumberLocale::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
instance()71 const NumberLocale *NumberLocale::instance()
72 {
73 return numberLocaleHolder();
74 }
75
init(QV4::ExecutionContext * scope)76 void Heap::NumberCtor::init(QV4::ExecutionContext *scope)
77 {
78 Heap::FunctionObject::init(scope, QStringLiteral("Number"));
79 }
80
virtualCallAsConstructor(const FunctionObject * f,const Value * argv,int argc,const Value * newTarget)81 ReturnedValue 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(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
virtualCall(const FunctionObject *,const Value *,const Value * argv,int argc)95 ReturnedValue 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
init(ExecutionEngine * engine,Object * ctor)101 void NumberPrototype::init(ExecutionEngine *engine, Object *ctor)
102 {
103 Scope scope(engine);
104 ScopedObject o(scope);
105 ctor->defineReadonlyProperty(engine->id_prototype(), (o = this));
106 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(1));
107
108 ctor->defineReadonlyProperty(QStringLiteral("NaN"), Value::fromDouble(qt_qnan()));
109 ctor->defineReadonlyProperty(QStringLiteral("NEGATIVE_INFINITY"), Value::fromDouble(-qInf()));
110 ctor->defineReadonlyProperty(QStringLiteral("POSITIVE_INFINITY"), Value::fromDouble(qInf()));
111 ctor->defineReadonlyProperty(QStringLiteral("MAX_VALUE"), Value::fromDouble(1.7976931348623158e+308));
112 ctor->defineReadonlyProperty(QStringLiteral("EPSILON"), Value::fromDouble(std::numeric_limits<double>::epsilon()));
113 ctor->defineReadonlyProperty(QStringLiteral("MAX_SAFE_INTEGER"), Value::fromDouble(9007199254740991));
114 ctor->defineReadonlyProperty(QStringLiteral("MIN_SAFE_INTEGER"), Value::fromDouble(-9007199254740991));
115
116 QT_WARNING_PUSH
117 QT_WARNING_DISABLE_INTEL(239)
118 ctor->defineReadonlyProperty(QStringLiteral("MIN_VALUE"), Value::fromDouble(5e-324));
119 QT_WARNING_POP
120
121 ctor->defineDefaultProperty(QStringLiteral("isFinite"), method_isFinite, 1);
122 ctor->defineDefaultProperty(QStringLiteral("isInteger"), method_isInteger, 1);
123 ctor->defineDefaultProperty(QStringLiteral("isSafeInteger"), method_isSafeInteger, 1);
124 ctor->defineDefaultProperty(QStringLiteral("isNaN"), method_isNaN, 1);
125
126 defineDefaultProperty(QStringLiteral("constructor"), (o = ctor));
127 defineDefaultProperty(engine->id_toString(), method_toString, 1);
128 defineDefaultProperty(engine->id_toLocaleString(), method_toLocaleString);
129 defineDefaultProperty(engine->id_valueOf(), method_valueOf);
130 defineDefaultProperty(QStringLiteral("toFixed"), method_toFixed, 1);
131 defineDefaultProperty(QStringLiteral("toExponential"), method_toExponential, 1);
132 defineDefaultProperty(QStringLiteral("toPrecision"), method_toPrecision, 1);
133 }
134
thisNumberValue(ExecutionEngine * v4,const Value * thisObject)135 inline 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
thisNumber(ExecutionEngine * engine,const Value * thisObject)147 inline 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
method_isFinite(const FunctionObject *,const Value *,const Value * argv,int argc)159 ReturnedValue 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(v) && !qt_is_inf(v));
166 }
167
method_isInteger(const FunctionObject *,const Value *,const Value * argv,int argc)168 ReturnedValue 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(dv) || qt_is_inf(dv))
179 return Encode(false);
180
181 double iv = v.toInteger();
182 return Encode(dv == iv);
183 }
184
method_isSafeInteger(const FunctionObject *,const Value *,const Value * argv,int argc)185 ReturnedValue 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(dv) || qt_is_inf(dv))
196 return Encode(false);
197
198 double iv = v.toInteger();
199 return Encode(dv == iv && std::fabs(iv) <= (1LL << 53) - 1);
200 }
201
method_isNaN(const FunctionObject *,const Value *,const Value * argv,int argc)202 ReturnedValue 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(v)));
211 }
212
method_toString(const FunctionObject * b,const Value * thisObject,const Value * argv,int argc)213 ReturnedValue NumberPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
214 {
215 ExecutionEngine *v4 = b->engine();
216 double num = thisNumber(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(radix));
224 }
225
226 QString str;
227 RuntimeHelpers::numberToString(&str, num, radix);
228 return Encode(v4->newString(str));
229 }
230
231 return Encode(Value::fromDouble(num).toString(v4));
232 }
233
method_toLocaleString(const FunctionObject * b,const Value * thisObject,const Value *,int)234 ReturnedValue NumberPrototype::method_toLocaleString(const FunctionObject *b, const Value *thisObject, const Value *, int)
235 {
236 Scope scope(b);
237 ScopedValue v(scope, thisNumberValue(b->engine(), thisObject));
238 return Encode(v->toString(scope.engine));
239 }
240
method_valueOf(const FunctionObject * b,const Value * thisObject,const Value *,int)241 ReturnedValue NumberPrototype::method_valueOf(const FunctionObject *b, const Value *thisObject, const Value *, int)
242 {
243 return thisNumberValue(b->engine(), thisObject);
244 }
245
method_toFixed(const FunctionObject * b,const Value * thisObject,const Value * argv,int argc)246 ReturnedValue NumberPrototype::method_toFixed(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
247 {
248 ExecutionEngine *v4 = b->engine();
249 double v = thisNumber(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(fdigits))
259 fdigits = 0;
260
261 if (fdigits < 0 || fdigits > 100)
262 return v4->throwRangeError(*thisObject);
263
264 QString str;
265 if (std::isnan(v))
266 str = QStringLiteral("NaN");
267 else if (qt_is_inf(v))
268 str = QString::fromLatin1(v < 0 ? "-Infinity" : "Infinity");
269 else if (v < 1.e21)
270 str = NumberLocale::instance()->toString(v, 'f', int(fdigits));
271 else {
272 return Encode(RuntimeHelpers::stringFromNumber(v4, v));
273 }
274 return Encode(v4->newString(str));
275 }
276
method_toExponential(const FunctionObject * b,const Value * thisObject,const Value * argv,int argc)277 ReturnedValue NumberPrototype::method_toExponential(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
278 {
279 ExecutionEngine *v4 = b->engine();
280 double d = thisNumber(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(d))
290 return Encode(v4->newString(QLatin1String("NaN")));
291
292 if (qIsInf(d))
293 return Encode(v4->newString(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(error);
299 }
300
301 QString result = NumberLocale::instance()->toString(d, 'e', fdigits);
302 return Encode(v4->newString(result));
303 }
304
method_toPrecision(const FunctionObject * b,const Value * thisObject,const Value * argv,int argc)305 ReturnedValue NumberPrototype::method_toPrecision(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
306 {
307 Scope scope(b);
308 ScopedValue v(scope, thisNumberValue(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(scope.engine));
315
316 int precision = argv[0].toInt32();
317 if (scope.engine->hasException)
318 return QV4::Encode::undefined();
319
320 if (std::isnan(d))
321 return Encode(scope.engine->newString(QLatin1String("NaN")));
322
323 if (qIsInf(d))
324 return Encode(scope.engine->newString(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(error);
329 }
330
331 QString result = NumberLocale::instance()->toString(d, 'g', precision);
332 return Encode(scope.engine->newString(result));
333 }
334