| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qscriptextvariant_p.h" | 
|---|
| 43 |  | 
|---|
| 44 | #ifndef QT_NO_SCRIPT | 
|---|
| 45 |  | 
|---|
| 46 | #include "qscriptengine_p.h" | 
|---|
| 47 | #include "qscriptvalueimpl_p.h" | 
|---|
| 48 | #include "qscriptcontext_p.h" | 
|---|
| 49 | #include "qscriptmember_p.h" | 
|---|
| 50 | #include "qscriptobject_p.h" | 
|---|
| 51 |  | 
|---|
| 52 | #include <QtCore/QtDebug> | 
|---|
| 53 |  | 
|---|
| 54 | #include <QtCore/QStringList> | 
|---|
| 55 |  | 
|---|
| 56 | #include <limits.h> | 
|---|
| 57 |  | 
|---|
| 58 | QT_BEGIN_NAMESPACE | 
|---|
| 59 |  | 
|---|
| 60 | namespace QScript { namespace Ext { | 
|---|
| 61 |  | 
|---|
| 62 | Variant::Variant(QScriptEnginePrivate *eng): | 
|---|
| 63 | Ecma::Core(eng, QLatin1String("QVariant"), QScriptClassInfo::VariantType) | 
|---|
| 64 | { | 
|---|
| 65 | newVariant(&publicPrototype, QVariant()); | 
|---|
| 66 |  | 
|---|
| 67 | eng->newConstructor(&ctor, this, publicPrototype); | 
|---|
| 68 |  | 
|---|
| 69 | addPrototypeFunction(QLatin1String("toString"), method_toString, 0); | 
|---|
| 70 | addPrototypeFunction(QLatin1String("valueOf"), method_valueOf, 0); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | Variant::~Variant() | 
|---|
| 74 | { | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | Variant::Instance *Variant::Instance::get(const QScriptValueImpl &object, QScriptClassInfo *klass) | 
|---|
| 78 | { | 
|---|
| 79 | if (! klass || klass == object.classInfo()) | 
|---|
| 80 | return static_cast<Instance*> (object.objectData()); | 
|---|
| 81 |  | 
|---|
| 82 | return 0; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void Variant::execute(QScriptContextPrivate *context) | 
|---|
| 86 | { | 
|---|
| 87 | QScriptValueImpl tmp; | 
|---|
| 88 | newVariant(&tmp, QVariant()); | 
|---|
| 89 | context->setReturnValue(tmp); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void Variant::newVariant(QScriptValueImpl *result, const QVariant &value) | 
|---|
| 93 | { | 
|---|
| 94 | Instance *instance; | 
|---|
| 95 | if (!result->isValid()) { | 
|---|
| 96 | engine()->newObject(result, publicPrototype, classInfo()); | 
|---|
| 97 | instance = new Instance(); | 
|---|
| 98 | result->setObjectData(instance); | 
|---|
| 99 | } else { | 
|---|
| 100 | Q_ASSERT(result->isObject()); | 
|---|
| 101 | if (result->classInfo() != classInfo()) { | 
|---|
| 102 | result->destroyObjectData(); | 
|---|
| 103 | result->setClassInfo(classInfo()); | 
|---|
| 104 | instance = new Instance(); | 
|---|
| 105 | result->setObjectData(instance); | 
|---|
| 106 | } else { | 
|---|
| 107 | instance = Instance::get(*result, classInfo()); | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | instance->value = value; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | QScriptValueImpl Variant::method_toString(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo) | 
|---|
| 114 | { | 
|---|
| 115 | if (Instance *instance = Instance::get(context->thisObject(), classInfo)) { | 
|---|
| 116 | QString result; | 
|---|
| 117 | QScriptValueImpl value = method_valueOf(context, eng, classInfo); | 
|---|
| 118 | if (value.isObject()) { | 
|---|
| 119 | result = instance->value.toString(); | 
|---|
| 120 | if (result.isEmpty()) { | 
|---|
| 121 | result = QString::fromLatin1("QVariant(%0)") | 
|---|
| 122 | .arg(QLatin1String(instance->value.typeName())); | 
|---|
| 123 | } | 
|---|
| 124 | } else { | 
|---|
| 125 | result = value.toString(); | 
|---|
| 126 | } | 
|---|
| 127 | return QScriptValueImpl(eng, result); | 
|---|
| 128 | } | 
|---|
| 129 | return context->throwError(QScriptContext::TypeError, | 
|---|
| 130 | QLatin1String("QVariant.prototype.toString")); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | QScriptValueImpl Variant::method_valueOf(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo) | 
|---|
| 134 | { | 
|---|
| 135 | if (Instance *instance = Instance::get(context->thisObject(), classInfo)) { | 
|---|
| 136 | QVariant v = instance->value; | 
|---|
| 137 | switch (v.type ()) { | 
|---|
| 138 | case QVariant::Invalid: | 
|---|
| 139 | return eng->undefinedValue(); | 
|---|
| 140 | case QVariant::String: | 
|---|
| 141 | return (QScriptValueImpl(eng, v.toString())); | 
|---|
| 142 |  | 
|---|
| 143 | case QVariant::Int: | 
|---|
| 144 | return (QScriptValueImpl(v.toInt())); | 
|---|
| 145 |  | 
|---|
| 146 | case QVariant::Bool: | 
|---|
| 147 | return (QScriptValueImpl(v.toBool())); | 
|---|
| 148 |  | 
|---|
| 149 | case QVariant::Double: | 
|---|
| 150 | return (QScriptValueImpl(v.toDouble())); // ### hmmm | 
|---|
| 151 |  | 
|---|
| 152 | case QVariant::Char: | 
|---|
| 153 | return (QScriptValueImpl(v.toChar().unicode())); | 
|---|
| 154 |  | 
|---|
| 155 | case QVariant::UInt: | 
|---|
| 156 | return (QScriptValueImpl(v.toUInt())); | 
|---|
| 157 |  | 
|---|
| 158 | default: | 
|---|
| 159 | return context->thisObject(); | 
|---|
| 160 | } // switch | 
|---|
| 161 | } | 
|---|
| 162 | return context->thisObject(); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | } } // namespace QScript::Ecma | 
|---|
| 166 |  | 
|---|
| 167 | QT_END_NAMESPACE | 
|---|
| 168 |  | 
|---|
| 169 | #endif // QT_NO_SCRIPT | 
|---|