28NumberLocale::NumberLocale() : QLocale(QLocale::C),
30 defaultDoublePrecision(0xffffff80)
32 setNumberOptions(QLocale::OmitGroupSeparator |
33 QLocale::OmitLeadingZeroInExponent |
34 QLocale::IncludeTrailingZeroesAfterDot);
47ReturnedValue NumberCtor::virtualCallAsConstructor(
const FunctionObject *f,
const Value *argv,
int argc,
const Value *newTarget)
49 auto v4 = f->engine();
50 double dbl = argc ? argv[0].toNumber() : 0.;
52 ReturnedValue o = Encode(f->engine()->newNumberObject(dbl));
56 ScopedObject obj(scope, o);
57 obj->setProtoFromNewTarget(newTarget);
58 return obj->asReturnedValue();
67void NumberPrototype::init(ExecutionEngine *engine, Object *ctor)
70 ScopedObject o(scope);
71 ctor->defineReadonlyProperty(engine->id_prototype(), (o =
this));
72 ctor->defineReadonlyConfigurableProperty(engine->id_length(), Value::fromInt32(1));
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));
83QT_WARNING_DISABLE_INTEL(239)
84 ctor->defineReadonlyProperty(QStringLiteral(
"MIN_VALUE"), Value::fromDouble(5e-324));
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);
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);
179ReturnedValue NumberPrototype::method_toString(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
181 ExecutionEngine *v4 = b->engine();
182 double num = thisNumber(v4, thisObject);
183 if (v4->hasException)
184 return QV4::Encode::undefined();
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));
193 RuntimeHelpers::numberToString(&str, num, radix);
194 return Encode(v4->newString(str));
197 return Encode(Value::fromDouble(num).toString(v4));
212ReturnedValue NumberPrototype::method_toFixed(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
214 ExecutionEngine *v4 = b->engine();
215 double v = thisNumber(v4, thisObject);
216 if (v4->hasException)
217 return QV4::Encode::undefined();
222 fdigits = argv[0].toInteger();
224 if (std::isnan(fdigits))
227 if (fdigits < 0 || fdigits > 100)
228 return v4->throwRangeError(*thisObject);
232 str = QStringLiteral(
"NaN");
233 else if (qt_is_inf(v))
234 str = QString::fromLatin1(v < 0 ?
"-Infinity" :
"Infinity");
236 str = NumberLocale::instance()->toString(v,
'f',
int(fdigits));
238 return Encode(RuntimeHelpers::stringFromNumber(v4, v));
240 return Encode(v4->newString(str));
243ReturnedValue NumberPrototype::method_toExponential(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
245 ExecutionEngine *v4 = b->engine();
246 double d = thisNumber(v4, thisObject);
247 if (v4->hasException)
248 return QV4::Encode::undefined();
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();
256 return Encode(v4->newString(QLatin1String(
"NaN")));
259 return Encode(v4->newString(QLatin1String(d < 0 ?
"-Infinity" :
"Infinity")));
261 if (!defaultDigits && (fdigits < 0 || fdigits > 100)) {
263 ScopedString error(scope, v4->newString(QStringLiteral(
"Number.prototype.toExponential: fractionDigits out of range")));
264 return v4->throwRangeError(error);
267 QString result = NumberLocale::instance()->toString(d,
'e', fdigits);
268 return Encode(v4->newString(result));
271ReturnedValue NumberPrototype::method_toPrecision(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
274 ScopedValue v(scope, thisNumberValue(scope.engine, thisObject));
275 if (scope.hasException())
276 return QV4::Encode::undefined();
277 double d = v->asDouble();
279 if (!argc || argv[0].isUndefined())
280 return Encode(v->toString(scope.engine));
282 int precision = argv[0].toInt32();
283 if (scope.hasException())
284 return QV4::Encode::undefined();
287 return Encode(scope.engine->newString(QLatin1String(
"NaN")));
290 return Encode(scope.engine->newString(QLatin1String(d < 0 ?
"-Infinity" :
"Infinity")));
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);
297 QString result = NumberLocale::instance()->toString(d,
'g', precision);
298 return Encode(scope.engine->newString(result));