From 985832eaf110d3decb32e7f984429e09e6bd6271 Mon Sep 17 00:00:00 2001 From: Thomas Senyk Date: Thu, 25 Nov 2010 13:16:42 +0100 Subject: QtScript variant conversion: Convert numbers to (u)int if applicable (reviewed by khansen) --- src/script/api/qscriptengine.cpp | 4 +++- tests/auto/qscriptvalue/tst_qscriptvalue.cpp | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 69abcad..1fcbd73 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -1744,7 +1744,9 @@ QVariant QScriptEnginePrivate::toVariant(JSC::ExecState *exec, JSC::JSValue valu else if (QScriptDeclarativeClass *dc = declarativeClass(value)) return dc->toVariant(declarativeObject(value)); return variantMapFromObject(exec, JSC::asObject(value)); - } else if (value.isNumber()) { + } else if (value.isInt32()) { + return QVariant(toInt32(exec, value)); + } else if (value.isDouble()) { return QVariant(toNumber(exec, value)); } else if (value.isString()) { return QVariant(toString(exec, value)); diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp index 97bbf26..5a50e49 100644 --- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp +++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp @@ -1254,6 +1254,10 @@ void tst_QScriptValue::toVariant() QCOMPARE(number.toVariant(), QVariant(123.0)); QCOMPARE(qscriptvalue_cast(number), QVariant(123.0)); + QScriptValue intNumber = QScriptValue(&eng, (qint32)123); + QCOMPARE(intNumber.toVariant().type(), QVariant((qint32)123).type()); + QCOMPARE((qscriptvalue_cast(number)).type(), QVariant((qint32)123).type()); + QScriptValue falskt = QScriptValue(&eng, false); QCOMPARE(falskt.toVariant(), QVariant(false)); QCOMPARE(qscriptvalue_cast(falskt), QVariant(false)); -- cgit v0.12