| 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 | |
| 13 | using namespace QV4; |
| 14 | |
| 15 | DEFINE_OBJECT_VTABLE(NumberCtor); |
| 16 | DEFINE_OBJECT_VTABLE(NumberObject); |
| 17 | |
| 18 | struct NumberLocaleHolder : public NumberLocale |
| 19 | { |
| 20 | NumberLocaleHolder() {} |
| 21 | }; |
| 22 | |
| 23 | Q_GLOBAL_STATIC(NumberLocaleHolder, numberLocaleHolder) |
| 24 | |
| 25 | NumberLocale::NumberLocale() : QLocale(QLocale::C), |
| 26 | // -128 means shortest string that can accurately represent the number. |
| 27 | defaultDoublePrecision(0xffffff80) |
| 28 | { |
| 29 | setNumberOptions(QLocale::OmitGroupSeparator | |
| 30 | QLocale::OmitLeadingZeroInExponent | |
| 31 | QLocale::IncludeTrailingZeroesAfterDot); |
| 32 | } |
| 33 | |
| 34 | const NumberLocale *NumberLocale::instance() |
| 35 | { |
| 36 | return numberLocaleHolder(); |
| 37 | } |
| 38 | |
| 39 | void Heap::NumberCtor::init(QV4::ExecutionEngine *engine) |
| 40 | { |
| 41 | Heap::FunctionObject::init(engine, QStringLiteral("Number" )); |
| 42 | } |
| 43 | |
| 44 | ReturnedValue 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(value: 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 | |
| 58 | ReturnedValue 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 | |
| 64 | void NumberPrototype::init(ExecutionEngine *engine, Object *ctor) |
| 65 | { |
| 66 | Scope scope(engine); |
| 67 | ScopedObject o(scope); |
| 68 | ctor->defineReadonlyProperty(name: engine->id_prototype(), value: (o = this)); |
| 69 | ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 1)); |
| 70 | |
| 71 | ctor->defineReadonlyProperty(QStringLiteral("NaN" ), value: Value::fromDouble(d: qt_qnan())); |
| 72 | ctor->defineReadonlyProperty(QStringLiteral("NEGATIVE_INFINITY" ), value: Value::fromDouble(d: -qInf())); |
| 73 | ctor->defineReadonlyProperty(QStringLiteral("POSITIVE_INFINITY" ), value: Value::fromDouble(d: qInf())); |
| 74 | ctor->defineReadonlyProperty(QStringLiteral("MAX_VALUE" ), value: Value::fromDouble(d: 1.7976931348623158e+308)); |
| 75 | ctor->defineReadonlyProperty(QStringLiteral("EPSILON" ), value: Value::fromDouble(d: std::numeric_limits<double>::epsilon())); |
| 76 | ctor->defineReadonlyProperty(QStringLiteral("MAX_SAFE_INTEGER" ), value: Value::fromDouble(d: 9007199254740991)); |
| 77 | ctor->defineReadonlyProperty(QStringLiteral("MIN_SAFE_INTEGER" ), value: Value::fromDouble(d: -9007199254740991)); |
| 78 | |
| 79 | QT_WARNING_PUSH |
| 80 | QT_WARNING_DISABLE_INTEL(239) |
| 81 | ctor->defineReadonlyProperty(QStringLiteral("MIN_VALUE" ), value: Value::fromDouble(d: 5e-324)); |
| 82 | QT_WARNING_POP |
| 83 | |
| 84 | ctor->defineDefaultProperty(QStringLiteral("isFinite" ), code: method_isFinite, argumentCount: 1); |
| 85 | ctor->defineDefaultProperty(QStringLiteral("isInteger" ), code: method_isInteger, argumentCount: 1); |
| 86 | ctor->defineDefaultProperty(QStringLiteral("isSafeInteger" ), code: method_isSafeInteger, argumentCount: 1); |
| 87 | ctor->defineDefaultProperty(QStringLiteral("isNaN" ), code: method_isNaN, argumentCount: 1); |
| 88 | |
| 89 | defineDefaultProperty(QStringLiteral("constructor" ), value: (o = ctor)); |
| 90 | defineDefaultProperty(name: engine->id_toString(), code: method_toString, argumentCount: 1); |
| 91 | defineDefaultProperty(name: engine->id_toLocaleString(), code: method_toLocaleString); |
| 92 | defineDefaultProperty(name: engine->id_valueOf(), code: method_valueOf); |
| 93 | defineDefaultProperty(QStringLiteral("toFixed" ), code: method_toFixed, argumentCount: 1); |
| 94 | defineDefaultProperty(QStringLiteral("toExponential" ), code: method_toExponential, argumentCount: 1); |
| 95 | defineDefaultProperty(QStringLiteral("toPrecision" ), code: method_toPrecision, argumentCount: 1); |
| 96 | } |
| 97 | |
| 98 | inline 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 | |
| 110 | inline 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 | |
| 122 | ReturnedValue NumberPrototype::method_isFinite(const FunctionObject *, const Value *, const Value *argv, int argc) |
| 123 | { |
| 124 | if (!argc || !argv[0].isNumber()) |
| 125 | return Encode(false); |
| 126 | |
| 127 | double v = argv[0].toNumber(); |
| 128 | return Encode(!std::isnan(x: v) && !qt_is_inf(d: v)); |
| 129 | } |
| 130 | |
| 131 | ReturnedValue NumberPrototype::method_isInteger(const FunctionObject *, const Value *, const Value *argv, int argc) |
| 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(x: dv) || qt_is_inf(d: dv)) |
| 142 | return Encode(false); |
| 143 | |
| 144 | double iv = v.toInteger(); |
| 145 | return Encode(dv == iv); |
| 146 | } |
| 147 | |
| 148 | ReturnedValue NumberPrototype::method_isSafeInteger(const FunctionObject *, const Value *, const Value *argv, int argc) |
| 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(x: dv) || qt_is_inf(d: dv)) |
| 159 | return Encode(false); |
| 160 | |
| 161 | double iv = v.toInteger(); |
| 162 | return Encode(dv == iv && std::fabs(x: iv) <= (1LL << 53) - 1); |
| 163 | } |
| 164 | |
| 165 | ReturnedValue NumberPrototype::method_isNaN(const FunctionObject *, const Value *, const Value *argv, int argc) |
| 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(x: v))); |
| 174 | } |
| 175 | |
| 176 | ReturnedValue NumberPrototype::method_toString(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 177 | { |
| 178 | ExecutionEngine *v4 = b->engine(); |
| 179 | double num = thisNumber(engine: 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(a: radix)); |
| 187 | } |
| 188 | |
| 189 | QString str; |
| 190 | RuntimeHelpers::numberToString(result: &str, num, radix); |
| 191 | return Encode(v4->newString(s: str)); |
| 192 | } |
| 193 | |
| 194 | return Encode(Value::fromDouble(d: num).toString(e: v4)); |
| 195 | } |
| 196 | |
| 197 | ReturnedValue NumberPrototype::method_toLocaleString(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 198 | { |
| 199 | Scope scope(b); |
| 200 | ScopedValue v(scope, thisNumberValue(v4: b->engine(), thisObject)); |
| 201 | return Encode(v->toString(e: scope.engine)); |
| 202 | } |
| 203 | |
| 204 | ReturnedValue NumberPrototype::method_valueOf(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 205 | { |
| 206 | return thisNumberValue(v4: b->engine(), thisObject); |
| 207 | } |
| 208 | |
| 209 | ReturnedValue NumberPrototype::method_toFixed(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 210 | { |
| 211 | ExecutionEngine *v4 = b->engine(); |
| 212 | double v = thisNumber(engine: 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(x: fdigits)) |
| 222 | fdigits = 0; |
| 223 | |
| 224 | if (fdigits < 0 || fdigits > 100) |
| 225 | return v4->throwRangeError(value: *thisObject); |
| 226 | |
| 227 | QString str; |
| 228 | if (std::isnan(x: v)) |
| 229 | str = QStringLiteral("NaN" ); |
| 230 | else if (qt_is_inf(d: v)) |
| 231 | str = QString::fromLatin1(ba: v < 0 ? "-Infinity" : "Infinity" ); |
| 232 | else if (v < 1.e21) |
| 233 | str = NumberLocale::instance()->toString(f: v, format: 'f', precision: int(fdigits)); |
| 234 | else { |
| 235 | return Encode(RuntimeHelpers::stringFromNumber(engine: v4, number: v)); |
| 236 | } |
| 237 | return Encode(v4->newString(s: str)); |
| 238 | } |
| 239 | |
| 240 | ReturnedValue NumberPrototype::method_toExponential(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 241 | { |
| 242 | ExecutionEngine *v4 = b->engine(); |
| 243 | double d = thisNumber(engine: 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(x: d)) |
| 253 | return Encode(v4->newString(s: QLatin1String("NaN" ))); |
| 254 | |
| 255 | if (qIsInf(d)) |
| 256 | return Encode(v4->newString(s: 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(value: error); |
| 262 | } |
| 263 | |
| 264 | QString result = NumberLocale::instance()->toString(f: d, format: 'e', precision: fdigits); |
| 265 | return Encode(v4->newString(s: result)); |
| 266 | } |
| 267 | |
| 268 | ReturnedValue NumberPrototype::method_toPrecision(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 269 | { |
| 270 | Scope scope(b); |
| 271 | ScopedValue v(scope, thisNumberValue(v4: 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(e: scope.engine)); |
| 278 | |
| 279 | int precision = argv[0].toInt32(); |
| 280 | if (scope.hasException()) |
| 281 | return QV4::Encode::undefined(); |
| 282 | |
| 283 | if (std::isnan(x: d)) |
| 284 | return Encode(scope.engine->newString(s: QLatin1String("NaN" ))); |
| 285 | |
| 286 | if (qIsInf(d)) |
| 287 | return Encode(scope.engine->newString(s: 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(value: error); |
| 292 | } |
| 293 | |
| 294 | QString result = NumberLocale::instance()->toString(f: d, format: 'g', precision); |
| 295 | return Encode(scope.engine->newString(s: result)); |
| 296 | } |
| 297 | |