diff options
-rw-r--r-- | src/script/api/qscriptengine.cpp | 22 | ||||
-rw-r--r-- | src/script/doc/src/scripting.qdoc | 8 | ||||
-rw-r--r-- | tests/auto/qscriptengine/tst_qscriptengine.cpp | 34 | ||||
-rw-r--r-- | tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp | 30 |
4 files changed, 94 insertions, 0 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index d8eaf3f..5dc4e2d 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -3046,6 +3046,10 @@ JSC::JSValue QScriptEnginePrivate::create(JSC::ExecState *exec, int type, const return JSC::jsNumber(exec, *reinterpret_cast<const int*>(ptr)); case QMetaType::UInt: return JSC::jsNumber(exec, *reinterpret_cast<const uint*>(ptr)); + case QMetaType::Long: + return JSC::jsNumber(exec, *reinterpret_cast<const long*>(ptr)); + case QMetaType::ULong: + return JSC::jsNumber(exec, *reinterpret_cast<const ulong*>(ptr)); case QMetaType::LongLong: return JSC::jsNumber(exec, qsreal(*reinterpret_cast<const qlonglong*>(ptr))); case QMetaType::ULongLong: @@ -3153,6 +3157,12 @@ bool QScriptEnginePrivate::convertValue(JSC::ExecState *exec, JSC::JSValue value case QMetaType::UInt: *reinterpret_cast<uint*>(ptr) = toUInt32(exec, value); return true; + case QMetaType::Long: + *reinterpret_cast<long*>(ptr) = long(toInteger(exec, value)); + return true; + case QMetaType::ULong: + *reinterpret_cast<ulong*>(ptr) = ulong(toInteger(exec, value)); + return true; case QMetaType::LongLong: *reinterpret_cast<qlonglong*>(ptr) = qlonglong(toInteger(exec, value)); return true; @@ -3330,6 +3340,12 @@ bool QScriptEnginePrivate::convertNumber(qsreal value, int type, void *ptr) case QMetaType::UInt: *reinterpret_cast<uint*>(ptr) = QScript::ToUInt32(value); return true; + case QMetaType::Long: + *reinterpret_cast<long*>(ptr) = long(QScript::ToInteger(value)); + return true; + case QMetaType::ULong: + *reinterpret_cast<ulong*>(ptr) = ulong(QScript::ToInteger(value)); + return true; case QMetaType::LongLong: *reinterpret_cast<qlonglong*>(ptr) = qlonglong(QScript::ToInteger(value)); return true; @@ -3378,6 +3394,12 @@ bool QScriptEnginePrivate::convertString(const QString &value, int type, void *p case QMetaType::UInt: *reinterpret_cast<uint*>(ptr) = QScript::ToUInt32(value); return true; + case QMetaType::Long: + *reinterpret_cast<long*>(ptr) = long(QScript::ToInteger(value)); + return true; + case QMetaType::ULong: + *reinterpret_cast<ulong*>(ptr) = ulong(QScript::ToInteger(value)); + return true; case QMetaType::LongLong: *reinterpret_cast<qlonglong*>(ptr) = qlonglong(QScript::ToInteger(value)); return true; diff --git a/src/script/doc/src/scripting.qdoc b/src/script/doc/src/scripting.qdoc index 70e51b1..2e9093e 100644 --- a/src/script/doc/src/scripting.qdoc +++ b/src/script/doc/src/scripting.qdoc @@ -478,6 +478,8 @@ \row \li ushort \li QScriptValue::toUInt16() \row \li char \li char(QScriptValue::toInt32()) \row \li uchar \li unsigned char(QScriptValue::toInt32()) + \row \li long \li long(QScriptValue::toInteger()) + \row \li ulong \li ulong(QScriptValue::toInteger()) \row \li qlonglong \li qlonglong(QScriptValue::toInteger()) \row \li qulonglong \li qulonglong(QScriptValue::toInteger()) \row \li QString \li An empty string if the QScriptValue is null @@ -549,6 +551,12 @@ \row \li char \li QScriptValue(engine, value) \row \li uchar \li QScriptValue(engine, value) \row \li QString \li QScriptValue(engine, value) + \row \li long \li If the input fits in an int, QScriptValue(engine, int(value)); + otherwise, QScriptValue(engine, double(value)). Note that the latter + conversion can be lossy. + \row \li ulong \li If the input fits in a uint, QScriptValue(engine, uint(value)); + otherwise, QScriptValue(engine, double(value)). Note that the latter + conversion can be lossy. \row \li qlonglong \li QScriptValue(engine, qsreal(value)). Note that the conversion may lead to loss of precision, since not all 64-bit integers can be represented using the qsreal type. diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index 3be9d62..5cb1f08 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -132,6 +132,7 @@ private slots: void valueConversion_basic2(); void valueConversion_dateTime(); void valueConversion_regExp(); + void valueConversion_long(); void qScriptValueFromValue_noEngine(); void importExtension(); void infiniteRecursion(); @@ -2503,6 +2504,39 @@ void tst_QScriptEngine::valueConversion_regExp() } } +void tst_QScriptEngine::valueConversion_long() +{ + QScriptEngine eng; + { + QScriptValue num(&eng, 123); + QCOMPARE(qscriptvalue_cast<long>(num), long(123)); + QCOMPARE(qscriptvalue_cast<ulong>(num), ulong(123)); + } + { + QScriptValue num(456); + QCOMPARE(qscriptvalue_cast<long>(num), long(456)); + QCOMPARE(qscriptvalue_cast<ulong>(num), ulong(456)); + } + { + QScriptValue str(&eng, "123"); + QCOMPARE(qscriptvalue_cast<long>(str), long(123)); + QCOMPARE(qscriptvalue_cast<ulong>(str), ulong(123)); + } + { + QScriptValue str("456"); + QCOMPARE(qscriptvalue_cast<long>(str), long(456)); + QCOMPARE(qscriptvalue_cast<ulong>(str), ulong(456)); + } + { + QScriptValue num = qScriptValueFromValue<long>(&eng, long(123)); + QCOMPARE(num.toInt32(), 123); + } + { + QScriptValue num = qScriptValueFromValue<ulong>(&eng, ulong(456)); + QCOMPARE(num.toInt32(), 456); + } +} + void tst_QScriptEngine::qScriptValueFromValue_noEngine() { QVERIFY(!qScriptValueFromValue(0, 123).isValid()); diff --git a/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp b/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp index 43e640a..9706901 100644 --- a/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp +++ b/tests/auto/qscriptextqobject/tst_qscriptextqobject.cpp @@ -343,6 +343,10 @@ public: { m_qtFunctionInvoked = 67; m_actuals << qVariantFromValue(arg); return arg; } Q_INVOKABLE qulonglong myInvokableWithULonglongArg(qulonglong arg) { m_qtFunctionInvoked = 68; m_actuals << qVariantFromValue(arg); return arg; } + Q_INVOKABLE long myInvokableWithLongArg(long arg) + { m_qtFunctionInvoked = 69; m_actuals << qVariantFromValue(arg); return arg; } + Q_INVOKABLE unsigned long myInvokableWithULongArg(unsigned long arg) + { m_qtFunctionInvoked = 70; m_actuals << qVariantFromValue(arg); return arg; } Q_INVOKABLE QObjectList findObjects() const { return findChildren<QObject *>(); } @@ -1594,6 +1598,32 @@ void tst_QScriptExtQObject::callQtInvokable4() QCOMPARE(v.userType(), int(QMetaType::ULongLong)); QCOMPARE(qvariant_cast<qulonglong>(v), qulonglong(123)); } + + m_myObject->resetQtFunctionInvoked(); + { + QScriptValue ret = m_engine->evaluate("myObject.myInvokableWithLongArg(123)"); + QCOMPARE(m_myObject->qtFunctionInvoked(), 69); + QVERIFY(ret.isNumber()); + QCOMPARE(long(ret.toInteger()), long(123)); + + QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); + QVariant v = m_myObject->qtFunctionActuals().at(0); + QCOMPARE(v.userType(), int(QMetaType::Long)); + QCOMPARE(qvariant_cast<long>(v), long(123)); + } + + m_myObject->resetQtFunctionInvoked(); + { + QScriptValue ret = m_engine->evaluate("myObject.myInvokableWithULongArg(456)"); + QCOMPARE(m_myObject->qtFunctionInvoked(), 70); + QVERIFY(ret.isNumber()); + QCOMPARE(ulong(ret.toInteger()), ulong(456)); + + QCOMPARE(m_myObject->qtFunctionActuals().size(), 1); + QVariant v = m_myObject->qtFunctionActuals().at(0); + QCOMPARE(v.userType(), int(QMetaType::ULong)); + QCOMPARE(qvariant_cast<unsigned long>(v), ulong(456)); + } } void tst_QScriptExtQObject::callQtInvokable5() |