From bc5c2c7362e0ad60990871e84426027a812937fc Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Thu, 18 Feb 2010 13:10:12 +0100 Subject: Cleanup: Move value conversion code to helper functions In preparation of doing this conversion in more places. Reviewed-by: Jedrzej Nowacki --- src/script/api/qscriptengine.cpp | 1 - src/script/api/qscriptengine_p.h | 50 ++++++++++++++++++++++++++++++++++++++++ src/script/api/qscriptvalue.cpp | 20 ++++++++-------- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 988e362..06233d9 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -40,7 +40,6 @@ #include #include -#include #include #include "Error.h" diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h index 46df429..daf670f 100644 --- a/src/script/api/qscriptengine_p.h +++ b/src/script/api/qscriptengine_p.h @@ -38,6 +38,7 @@ #include "private/qobject_p.h" #include +#include #include #include "qscriptvalue_p.h" #include "qscriptstring_p.h" @@ -93,6 +94,15 @@ namespace QScript quint16 ToUInt16(qsreal); qsreal ToInteger(qsreal); + inline bool ToBool(qsreal); + inline bool ToBool(const QString &); + inline qsreal ToNumber(const QString &); + inline qint32 ToInt32(const QString &); + inline quint32 ToUInt32(const QString &); + inline quint16 ToUInt16(const QString &); + inline qsreal ToInteger(const QString &); + inline QString ToString(qsreal); + //some conversion helper functions inline QScriptEnginePrivate *scriptEngineFromExec(const JSC::ExecState *exec); bool isFunction(JSC::JSValue value); @@ -396,6 +406,46 @@ inline QScriptEnginePrivate *scriptEngineFromExec(const JSC::ExecState *exec) return static_cast(exec->globalData().clientData)->engine; } +inline QString ToString(qsreal value) +{ + return JSC::UString::from(value); +} + +inline qsreal ToNumber(const QString &value) +{ + return ((JSC::UString)value).toDouble(); +} + +inline qint32 ToInt32(const QString &value) +{ + return ToInt32(ToNumber(value)); +} + +inline quint32 ToUInt32(const QString &value) +{ + return ToUInt32(ToNumber(value)); +} + +inline quint16 ToUInt16(const QString &value) +{ + return ToUInt16(ToNumber(value)); +} + +inline qsreal ToInteger(const QString &value) +{ + return ToInteger(ToNumber(value)); +} + +inline bool ToBool(qsreal value) +{ + return (value != 0) && !qIsNaN(value); +} + +inline bool ToBool(const QString &value) +{ + return !value.isEmpty(); +} + } // namespace QScript inline QScriptValuePrivate *QScriptEnginePrivate::allocateScriptValuePrivate(size_t size) diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp index e8c2050..724bbe3 100644 --- a/src/script/api/qscriptvalue.cpp +++ b/src/script/api/qscriptvalue.cpp @@ -1127,7 +1127,7 @@ QString QScriptValue::toString() const return str; } case QScriptValuePrivate::Number: - return JSC::UString::from(d->numberValue); + return QScript::ToString(d->numberValue); case QScriptValuePrivate::String: return d->stringValue; } @@ -1163,7 +1163,7 @@ qsreal QScriptValue::toNumber() const case QScriptValuePrivate::Number: return d->numberValue; case QScriptValuePrivate::String: - return ((JSC::UString)d->stringValue).toDouble(); + return QScript::ToNumber(d->stringValue); } return 0; } @@ -1188,9 +1188,9 @@ bool QScriptValue::toBoolean() const return result; } case QScriptValuePrivate::Number: - return (d->numberValue != 0) && !qIsNaN(d->numberValue); + return QScript::ToBool(d->numberValue); case QScriptValuePrivate::String: - return (!d->stringValue.isEmpty()); + return QScript::ToBool(d->stringValue); } return false; } @@ -1224,9 +1224,9 @@ bool QScriptValue::toBool() const return result; } case QScriptValuePrivate::Number: - return (d->numberValue != 0) && !qIsNaN(d->numberValue); + return QScript::ToBool(d->numberValue); case QScriptValuePrivate::String: - return (!d->stringValue.isEmpty()); + return QScript::ToBool(d->stringValue); } return false; } @@ -1260,7 +1260,7 @@ qint32 QScriptValue::toInt32() const case QScriptValuePrivate::Number: return QScript::ToInt32(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToInt32(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToInt32(d->stringValue); } return 0; } @@ -1294,7 +1294,7 @@ quint32 QScriptValue::toUInt32() const case QScriptValuePrivate::Number: return QScript::ToUInt32(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToUInt32(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToUInt32(d->stringValue); } return 0; } @@ -1324,7 +1324,7 @@ quint16 QScriptValue::toUInt16() const case QScriptValuePrivate::Number: return QScript::ToUInt16(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToUInt16(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToUInt16(d->stringValue); } return 0; } @@ -1358,7 +1358,7 @@ qsreal QScriptValue::toInteger() const case QScriptValuePrivate::Number: return QScript::ToInteger(d->numberValue); case QScriptValuePrivate::String: - return QScript::ToInteger(((JSC::UString)d->stringValue).toDouble()); + return QScript::ToInteger(d->stringValue); } return 0; } -- cgit v0.12