attempt to convert the object to a primitive value (possibly
resulting in an uncaught script exception).
- \sa isNumber(), toInteger(), toInt(), toUInt(), toUInt16()
+ \sa isNumber(), toInt(), toUInt(), toUInt16()
*/
double QJSValue::toNumber() const
{
return d->toBool();
}
-#ifdef QT_DEPRECATED
-
-/*!
- \obsolete
-
- Returns the integer value of this QJSValue, using the conversion
- rules described in \l{ECMA-262} section 9.4, "ToInteger".
-
- Note that if this QJSValue is an object, calling this function
- has side effects on the script engine, since the engine will call
- the object's valueOf() function (and possibly toString()) in an
- attempt to convert the object to a primitive value (possibly
- resulting in an uncaught script exception).
-
- \sa toNumber()
-*/
-double QJSValue::toInteger() const
-{
- Q_D(const QJSValue);
- QScriptIsolate api(d->engine());
- return d->toInteger();
-}
-
-#endif // QT_DEPRECATED
-
/*!
Returns the signed 32-bit integer value of this QJSValue, using
the conversion rules described in \l{ECMA-262} section 9.5, "ToInt32".
}
}
-void tst_QJSValue::toInteger()
-{
- QJSEngine eng;
-
- {
- QJSValue number = QJSValue(&eng, 123.0);
- QCOMPARE(number.toInteger(), 123.0);
-
- QJSValue number2 = QJSValue(&eng, qSNaN());
- QCOMPARE(number2.toInteger(), 0.0);
-
- QJSValue number3 = QJSValue(&eng, qInf());
- QCOMPARE(qIsInf(number3.toInteger()), true);
-
- QJSValue number4 = QJSValue(&eng, 0.5);
- QCOMPARE(number4.toInteger(), 0.0);
-
- QJSValue number5 = QJSValue(&eng, 123.5);
- QCOMPARE(number5.toInteger(), 123.0);
-
- QJSValue number6 = QJSValue(&eng, -456.5);
- QCOMPARE(number6.toInteger(), -456.0);
-
- QJSValue str = QJSValue(&eng, QLatin1String("123.0"));
- QCOMPARE(str.toInteger(), 123.0);
-
- QJSValue str2 = QJSValue(&eng, QLatin1String("NaN"));
- QCOMPARE(str2.toInteger(), 0.0);
-
- QJSValue str3 = QJSValue(&eng, QLatin1String("Infinity"));
- QCOMPARE(qIsInf(str3.toInteger()), true);
-
- QJSValue str4 = QJSValue(&eng, QLatin1String("0.5"));
- QCOMPARE(str4.toInteger(), 0.0);
-
- QJSValue str5 = QJSValue(&eng, QLatin1String("123.5"));
- QCOMPARE(str5.toInteger(), 123.0);
-
- QJSValue str6 = QJSValue(&eng, QLatin1String("-456.5"));
- QCOMPARE(str6.toInteger(), -456.0);
- }
- // V2 constructors
- {
- QJSValue number = QJSValue(123.0);
- QCOMPARE(number.toInteger(), 123.0);
-
- QJSValue number2 = QJSValue(qSNaN());
- QCOMPARE(number2.toInteger(), 0.0);
-
- QJSValue number3 = QJSValue(qInf());
- QCOMPARE(qIsInf(number3.toInteger()), true);
-
- QJSValue number4 = QJSValue(0.5);
- QCOMPARE(number4.toInteger(), 0.0);
-
- QJSValue number5 = QJSValue(123.5);
- QCOMPARE(number5.toInteger(), 123.0);
-
- QJSValue number6 = QJSValue(-456.5);
- QCOMPARE(number6.toInteger(), -456.0);
-
- QJSValue number7 = QJSValue(0x43211234);
- QCOMPARE(number7.toInteger(), qreal(0x43211234));
-
- QJSValue str = QJSValue("123.0");
- QCOMPARE(str.toInteger(), 123.0);
-
- QJSValue str2 = QJSValue("NaN");
- QCOMPARE(str2.toInteger(), 0.0);
-
- QJSValue str3 = QJSValue("Infinity");
- QCOMPARE(qIsInf(str3.toInteger()), true);
-
- QJSValue str4 = QJSValue("0.5");
- QCOMPARE(str4.toInteger(), 0.0);
-
- QJSValue str5 = QJSValue("123.5");
- QCOMPARE(str5.toInteger(), 123.0);
-
- QJSValue str6 = QJSValue("-456.5");
- QCOMPARE(str6.toInteger(), -456.0);
- }
-
- QJSValue inv;
- QCOMPARE(inv.toInteger(), 0.0);
-}
-
void tst_QJSValue::toInt()
{
QJSEngine eng;