diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2010-02-18 12:54:18 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2010-02-18 16:36:04 (GMT) |
commit | af4ccb478650b7efd5de3d81439173cd31931bd2 (patch) | |
tree | 4b7eca9084396caf4434d04c9b62d726e338736c /src/script/api | |
parent | bc5c2c7362e0ad60990871e84426027a812937fc (diff) | |
download | Qt-af4ccb478650b7efd5de3d81439173cd31931bd2.zip Qt-af4ccb478650b7efd5de3d81439173cd31931bd2.tar.gz Qt-af4ccb478650b7efd5de3d81439173cd31931bd2.tar.bz2 |
Move more script value conversion code to helper functions
In preparation of operating purely on JSC::JSValue internally.
Reviewed-by: Jedrzej Nowacki
Diffstat (limited to 'src/script/api')
-rw-r--r-- | src/script/api/qscriptengine_p.h | 131 | ||||
-rw-r--r-- | src/script/api/qscriptvalue.cpp | 86 |
2 files changed, 152 insertions, 65 deletions
diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h index daf670f..5f079f4 100644 --- a/src/script/api/qscriptengine_p.h +++ b/src/script/api/qscriptengine_p.h @@ -37,8 +37,10 @@ #include "private/qobject_p.h" +#include <QtCore/qdatetime.h> #include <QtCore/qhash.h> #include <QtCore/qnumeric.h> +#include <QtCore/qregexp.h> #include <QtCore/qset.h> #include "qscriptvalue_p.h" #include "qscriptstring_p.h" @@ -46,11 +48,14 @@ #include "utils/qscriptdate_p.h" #include "DateConstructor.h" +#include "DateInstance.h" #include "Debugger.h" +#include "ErrorInstance.h" #include "JSArray.h" #include "Lexer.h" #include "RefPtr.h" #include "RegExpConstructor.h" +#include "RegExpObject.h" #include "SourceProvider.h" #include "Structure.h" #include "JSGlobalObject.h" @@ -134,6 +139,23 @@ public: static QScriptEnginePrivate *get(QScriptEngine *q) { return q ? q->d_func() : 0; } static QScriptEngine *get(QScriptEnginePrivate *d) { return d ? d->q_func() : 0; } + static inline bool isArray(JSC::JSValue); + static inline bool isDate(JSC::JSValue); + static inline bool isError(JSC::JSValue); + static inline bool isObject(JSC::JSValue); + static inline bool isRegExp(JSC::JSValue); + static inline bool isVariant(JSC::JSValue); + + static inline bool toBool(JSC::ExecState *, JSC::JSValue); + static inline qsreal toInteger(JSC::ExecState *, JSC::JSValue); + static inline qsreal toNumber(JSC::ExecState *, JSC::JSValue); + static inline qint32 toInt32(JSC::ExecState *, JSC::JSValue); + static inline quint32 toUInt32(JSC::ExecState *, JSC::JSValue); + static inline quint16 toUInt16(JSC::ExecState *, JSC::JSValue); + static inline QString toString(JSC::ExecState *, JSC::JSValue); + + static inline QDateTime toDateTime(JSC::ExecState *, JSC::JSValue); + static bool convert(const QScriptValue &value, int type, void *ptr, QScriptEnginePrivate *eng); @@ -676,6 +698,115 @@ inline JSC::JSValue QScriptEnginePrivate::newObject() return new (currentFrame)QScriptObject(scriptObjectStructure); } +inline bool QScriptEnginePrivate::isObject(JSC::JSValue value) +{ + return value && value.isObject(); +} + +inline bool QScriptEnginePrivate::isArray(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::JSArray::info); +} + +inline bool QScriptEnginePrivate::isDate(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::DateInstance::info); +} + +inline bool QScriptEnginePrivate::isError(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::ErrorInstance::info); +} + +inline bool QScriptEnginePrivate::isRegExp(JSC::JSValue value) +{ + return isObject(value) && value.inherits(&JSC::RegExpObject::info); +} + +inline bool QScriptEnginePrivate::isVariant(JSC::JSValue value) +{ + if (!isObject(value) || !value.inherits(&QScriptObject::info)) + return false; + QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(value)); + QScriptObjectDelegate *delegate = object->delegate(); + return (delegate && (delegate->type() == QScriptObjectDelegate::Variant)); +} + +inline bool QScriptEnginePrivate::toBool(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + bool result = value.toBoolean(exec); + restoreException(exec, savedException); + return result; +} + +inline qsreal QScriptEnginePrivate::toInteger(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + qsreal result = value.toInteger(exec); + restoreException(exec, savedException); + return result; +} + +inline qsreal QScriptEnginePrivate::toNumber(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + qsreal result = value.toNumber(exec); + restoreException(exec, savedException); + return result; +} + +inline qint32 QScriptEnginePrivate::toInt32(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + qint32 result = value.toInt32(exec); + restoreException(exec, savedException); + return result; +} + +inline quint32 QScriptEnginePrivate::toUInt32(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + quint32 result = value.toUInt32(exec); + restoreException(exec, savedException); + return result; +} + +inline quint16 QScriptEnginePrivate::toUInt16(JSC::ExecState *exec, JSC::JSValue value) +{ + // ### no equivalent function in JSC + return QScript::ToUInt16(toNumber(exec, value)); +} + +inline QString QScriptEnginePrivate::toString(JSC::ExecState *exec, JSC::JSValue value) +{ + JSC::JSValue savedException; + saveException(exec, &savedException); + JSC::UString str = value.toString(exec); + if (exec && exec->hadException() && !str.size()) { + JSC::JSValue savedException2; + saveException(exec, &savedException2); + str = savedException2.toString(exec); + restoreException(exec, savedException2); + } + if (savedException) + restoreException(exec, savedException); + return str; +} + +inline QDateTime QScriptEnginePrivate::toDateTime(JSC::ExecState *, JSC::JSValue value) +{ + if (!isDate(value)) + return QDateTime(); + qsreal t = static_cast<JSC::DateInstance*>(JSC::asObject(value))->internalNumber(); + return QScript::ToDateTime(t, Qt::LocalTime); +} + QT_END_NAMESPACE #endif diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp index 724bbe3..929f606 100644 --- a/src/script/api/qscriptvalue.cpp +++ b/src/script/api/qscriptvalue.cpp @@ -29,21 +29,15 @@ #include "qscriptengine_p.h" #include "qscriptstring_p.h" -#include "JSArray.h" #include "JSGlobalObject.h" #include "JSImmediate.h" #include "JSObject.h" #include "JSValue.h" #include "JSFunction.h" -#include "DateInstance.h" -#include "ErrorInstance.h" -#include "RegExpObject.h" #include "Identifier.h" #include "Operations.h" #include "Arguments.h" -#include <QtCore/qdatetime.h> -#include <QtCore/qregexp.h> #include <QtCore/qvariant.h> #include <QtCore/qvarlengtharray.h> #include <QtCore/qnumeric.h> @@ -629,9 +623,9 @@ QScriptValue &QScriptValue::operator=(const QScriptValue &other) bool QScriptValue::isError() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::ErrorInstance::info); + return QScriptEnginePrivate::isError(d->jscValue); } /*! @@ -643,9 +637,9 @@ bool QScriptValue::isError() const bool QScriptValue::isArray() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::JSArray::info); + return QScriptEnginePrivate::isArray(d->jscValue); } /*! @@ -657,9 +651,9 @@ bool QScriptValue::isArray() const bool QScriptValue::isDate() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::DateInstance::info); + return QScriptEnginePrivate::isDate(d->jscValue); } /*! @@ -671,9 +665,9 @@ bool QScriptValue::isDate() const bool QScriptValue::isRegExp() const { Q_D(const QScriptValue); - if (!d || !d->isObject()) + if (!d || !d->isJSC()) return false; - return d->jscValue.inherits(&JSC::RegExpObject::info); + return QScriptEnginePrivate::isRegExp(d->jscValue); } /*! @@ -1113,18 +1107,7 @@ QString QScriptValue::toString() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - JSC::UString str = d->jscValue.toString(exec); - if (exec && exec->hadException() && !str.size()) { - JSC::JSValue savedException2; - QScriptEnginePrivate::saveException(exec, &savedException2); - str = savedException2.toString(exec); - QScriptEnginePrivate::restoreException(exec, savedException2); - } - if (savedException) - QScriptEnginePrivate::restoreException(exec, savedException); - return str; + return QScriptEnginePrivate::toString(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToString(d->numberValue); @@ -1154,11 +1137,7 @@ qsreal QScriptValue::toNumber() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - qsreal result = d->jscValue.toNumber(exec); - QScriptEnginePrivate::restoreException(exec, savedException); - return result; + return QScriptEnginePrivate::toNumber(exec, d->jscValue); } case QScriptValuePrivate::Number: return d->numberValue; @@ -1181,11 +1160,7 @@ bool QScriptValue::toBoolean() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - bool result = d->jscValue.toBoolean(exec); - QScriptEnginePrivate::restoreException(exec, savedException); - return result; + return QScriptEnginePrivate::toBool(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToBool(d->numberValue); @@ -1217,11 +1192,7 @@ bool QScriptValue::toBool() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - bool result = d->jscValue.toBoolean(exec); - QScriptEnginePrivate::restoreException(exec, savedException); - return result; + return QScriptEnginePrivate::toBool(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToBool(d->numberValue); @@ -1251,11 +1222,7 @@ qint32 QScriptValue::toInt32() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - qint32 result = d->jscValue.toInt32(exec); - QScriptEnginePrivate::restoreException(exec, savedException); - return result; + return QScriptEnginePrivate::toInt32(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToInt32(d->numberValue); @@ -1285,11 +1252,7 @@ quint32 QScriptValue::toUInt32() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - quint32 result = d->jscValue.toUInt32(exec); - QScriptEnginePrivate::restoreException(exec, savedException); - return result; + return QScriptEnginePrivate::toUInt32(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToUInt32(d->numberValue); @@ -1318,8 +1281,8 @@ quint16 QScriptValue::toUInt16() const return 0; switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { - // ### no equivalent function in JSC - return QScript::ToUInt16(toNumber()); + JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; + return QScriptEnginePrivate::toUInt16(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToUInt16(d->numberValue); @@ -1349,11 +1312,7 @@ qsreal QScriptValue::toInteger() const switch (d->type) { case QScriptValuePrivate::JavaScriptCore: { JSC::ExecState *exec = d->engine ? d->engine->currentFrame : 0; - JSC::JSValue savedException; - QScriptEnginePrivate::saveException(exec, &savedException); - qsreal result = d->jscValue.toInteger(exec); - QScriptEnginePrivate::restoreException(exec, savedException); - return result; + return QScriptEnginePrivate::toInteger(exec, d->jscValue); } case QScriptValuePrivate::Number: return QScript::ToInteger(d->numberValue); @@ -1456,10 +1415,9 @@ QScriptValue QScriptValue::toObject() const QDateTime QScriptValue::toDateTime() const { Q_D(const QScriptValue); - if (!isDate()) + if (!d || !d->engine) return QDateTime(); - qsreal t = static_cast<JSC::DateInstance*>(JSC::asObject(d->jscValue))->internalNumber(); - return QScript::ToDateTime(t, Qt::LocalTime); + return QScriptEnginePrivate::toDateTime(d->engine->currentFrame, d->jscValue); } #ifndef QT_NO_REGEXP @@ -2151,11 +2109,9 @@ bool QScriptValue::isObject() const bool QScriptValue::isVariant() const { Q_D(const QScriptValue); - if (!d || !d->isJSC() || !d->jscValue.inherits(&QScriptObject::info)) + if (!d || !d->isJSC()) return false; - QScriptObject *object = static_cast<QScriptObject*>(JSC::asObject(d->jscValue)); - QScriptObjectDelegate *delegate = object->delegate(); - return (delegate && (delegate->type() == QScriptObjectDelegate::Variant)); + return QScriptEnginePrivate::isVariant(d->jscValue); } /*! |