| 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 | #ifndef QSCRIPTCONTEXT_P_H | 
|---|
| 43 | #define QSCRIPTCONTEXT_P_H | 
|---|
| 44 |  | 
|---|
| 45 | #include "qscriptcontextfwd_p.h" | 
|---|
| 46 |  | 
|---|
| 47 | #ifndef QT_NO_SCRIPT | 
|---|
| 48 |  | 
|---|
| 49 | #include "qscriptenginefwd_p.h" | 
|---|
| 50 | #include "qscriptnameid_p.h" | 
|---|
| 51 |  | 
|---|
| 52 | #include <QtCore/qnumeric.h> | 
|---|
| 53 |  | 
|---|
| 54 | QT_BEGIN_NAMESPACE | 
|---|
| 55 |  | 
|---|
| 56 | // | 
|---|
| 57 | //  W A R N I N G | 
|---|
| 58 | //  ------------- | 
|---|
| 59 | // | 
|---|
| 60 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 61 | // implementation detail.  This header file may change from version to | 
|---|
| 62 | // version without notice, or even be removed. | 
|---|
| 63 | // | 
|---|
| 64 | // We mean it. | 
|---|
| 65 | // | 
|---|
| 66 |  | 
|---|
| 67 | inline QScriptContextPrivate::QScriptContextPrivate() | 
|---|
| 68 | : previous(0), | 
|---|
| 69 | argc(0), | 
|---|
| 70 | m_state(QScriptContext::NormalState), | 
|---|
| 71 | args(0), | 
|---|
| 72 | tempStack(0), | 
|---|
| 73 | stackPtr(0), | 
|---|
| 74 | m_code(0), | 
|---|
| 75 | iPtr(0), | 
|---|
| 76 | firstInstruction(0), | 
|---|
| 77 | lastInstruction(0), | 
|---|
| 78 | currentLine(0), | 
|---|
| 79 | currentColumn(0), | 
|---|
| 80 | errorLineNumber(0), | 
|---|
| 81 | catching(false), | 
|---|
| 82 | m_calledAsConstructor(false), | 
|---|
| 83 | calleeMetaIndex(0), | 
|---|
| 84 | q_ptr(0) | 
|---|
| 85 | { | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | inline QScriptContextPrivate *QScriptContextPrivate::get(QScriptContext *q) | 
|---|
| 89 | { | 
|---|
| 90 | if (q) | 
|---|
| 91 | return q->d_func(); | 
|---|
| 92 | return 0; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | inline const QScriptContextPrivate *QScriptContextPrivate::get(const QScriptContext *q) | 
|---|
| 96 | { | 
|---|
| 97 | if (q) | 
|---|
| 98 | return q->d_func(); | 
|---|
| 99 | return 0; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | inline QScriptContext *QScriptContextPrivate::create() | 
|---|
| 103 | { | 
|---|
| 104 | return new QScriptContext; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | inline QScriptEnginePrivate *QScriptContextPrivate::engine() const | 
|---|
| 108 | { | 
|---|
| 109 | return m_activation.engine(); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | inline QScriptContextPrivate *QScriptContextPrivate::parentContext() const | 
|---|
| 113 | { | 
|---|
| 114 | return previous; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | inline void QScriptContextPrivate::init(QScriptContextPrivate *parent) | 
|---|
| 118 | { | 
|---|
| 119 | m_state = QScriptContext::NormalState; | 
|---|
| 120 | previous = parent; | 
|---|
| 121 | args = 0; | 
|---|
| 122 | argc = 0; | 
|---|
| 123 | m_code = 0; | 
|---|
| 124 | iPtr = firstInstruction = lastInstruction = 0; | 
|---|
| 125 | stackPtr = tempStack = (parent != 0) ? parent->stackPtr : 0; | 
|---|
| 126 | m_activation.invalidate(); | 
|---|
| 127 | m_thisObject.invalidate(); | 
|---|
| 128 | m_result.invalidate(); | 
|---|
| 129 | m_scopeChain.invalidate(); | 
|---|
| 130 | m_callee.invalidate(); | 
|---|
| 131 | m_arguments.invalidate(); | 
|---|
| 132 | currentLine = -1; | 
|---|
| 133 | currentColumn = -1; | 
|---|
| 134 | errorLineNumber = -1; | 
|---|
| 135 | m_calledAsConstructor = false; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | inline QScriptValueImpl QScriptContextPrivate::argument(int index) const | 
|---|
| 139 | { | 
|---|
| 140 | if (index >= argc) | 
|---|
| 141 | return engine()->undefinedValue(); | 
|---|
| 142 |  | 
|---|
| 143 | Q_ASSERT(args != 0); | 
|---|
| 144 | return args[index]; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | inline int QScriptContextPrivate::argumentCount() const | 
|---|
| 148 | { | 
|---|
| 149 | return argc; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | inline QScriptValueImpl QScriptContextPrivate::argumentsObject() const | 
|---|
| 153 | { | 
|---|
| 154 | if (!m_arguments.isValid() && m_activation.isValid()) { | 
|---|
| 155 | QScriptContextPrivate *dd = const_cast<QScriptContextPrivate*>(this); | 
|---|
| 156 | engine()->newArguments(&dd->m_arguments, m_activation, | 
|---|
| 157 | argc, m_callee); | 
|---|
| 158 | } | 
|---|
| 159 | return m_arguments; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | inline void QScriptContextPrivate::throwException() | 
|---|
| 163 | { | 
|---|
| 164 | m_state = QScriptContext::ExceptionState; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | inline bool QScriptContextPrivate::hasUncaughtException() const | 
|---|
| 168 | { | 
|---|
| 169 | return m_state == QScriptContext::ExceptionState; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | inline void QScriptContextPrivate::recover() | 
|---|
| 173 | { | 
|---|
| 174 | m_state = QScriptContext::NormalState; | 
|---|
| 175 | errorLineNumber = -1; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | inline bool QScriptContextPrivate::isNumerical(const QScriptValueImpl &v) | 
|---|
| 179 | { | 
|---|
| 180 | switch (v.type()) { | 
|---|
| 181 | case QScript::BooleanType: | 
|---|
| 182 | case QScript::IntegerType: | 
|---|
| 183 | case QScript::NumberType: | 
|---|
| 184 | return true; | 
|---|
| 185 |  | 
|---|
| 186 | default: | 
|---|
| 187 | return false; | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | inline bool QScriptContextPrivate::eq_cmp(const QScriptValueImpl &lhs, const QScriptValueImpl &rhs) | 
|---|
| 192 | { | 
|---|
| 193 | if (lhs.type() == rhs.type()) { | 
|---|
| 194 | switch (lhs.type()) { | 
|---|
| 195 | case QScript::InvalidType: | 
|---|
| 196 | case QScript::UndefinedType: | 
|---|
| 197 | case QScript::NullType: | 
|---|
| 198 | return true; | 
|---|
| 199 |  | 
|---|
| 200 | case QScript::NumberType: | 
|---|
| 201 | return lhs.m_number_value == rhs.m_number_value; | 
|---|
| 202 |  | 
|---|
| 203 | case QScript::ReferenceType: | 
|---|
| 204 | case QScript::IntegerType: | 
|---|
| 205 | return lhs.m_int_value == rhs.m_int_value; | 
|---|
| 206 |  | 
|---|
| 207 | case QScript::BooleanType: | 
|---|
| 208 | return lhs.m_bool_value == rhs.m_bool_value; | 
|---|
| 209 |  | 
|---|
| 210 | case QScript::StringType: | 
|---|
| 211 | if (lhs.m_string_value->unique && rhs.m_string_value->unique) | 
|---|
| 212 | return lhs.m_string_value == rhs.m_string_value; | 
|---|
| 213 | return lhs.m_string_value->s == rhs.m_string_value->s; | 
|---|
| 214 |  | 
|---|
| 215 | case QScript::PointerType: | 
|---|
| 216 | return lhs.m_ptr_value == rhs.m_ptr_value; | 
|---|
| 217 |  | 
|---|
| 218 | case QScript::ObjectType: | 
|---|
| 219 | if (lhs.isVariant()) | 
|---|
| 220 | return lhs.m_object_value == rhs.m_object_value || lhs.toVariant() == rhs.toVariant(); | 
|---|
| 221 | #ifndef QT_NO_QOBJECT | 
|---|
| 222 | else if (lhs.isQObject()) | 
|---|
| 223 | return lhs.m_object_value == rhs.m_object_value || lhs.toQObject() == rhs.toQObject(); | 
|---|
| 224 | #endif | 
|---|
| 225 | else | 
|---|
| 226 | return lhs.m_object_value == rhs.m_object_value; | 
|---|
| 227 |  | 
|---|
| 228 | case QScript::LazyStringType: | 
|---|
| 229 | return *lhs.m_lazy_string_value == *rhs.m_lazy_string_value; | 
|---|
| 230 | } | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | return eq_cmp_helper(lhs, rhs); | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | inline bool QScriptContextPrivate::strict_eq_cmp( const QScriptValueImpl &lhs, const QScriptValueImpl &rhs) | 
|---|
| 237 | { | 
|---|
| 238 | if (lhs.type() != rhs.type()) | 
|---|
| 239 | return false; | 
|---|
| 240 |  | 
|---|
| 241 | switch (lhs.type()) { | 
|---|
| 242 | case QScript::InvalidType: | 
|---|
| 243 | case QScript::UndefinedType: | 
|---|
| 244 | case QScript::NullType: | 
|---|
| 245 | return true; | 
|---|
| 246 |  | 
|---|
| 247 | case QScript::NumberType: | 
|---|
| 248 | if (qIsNaN(lhs.m_number_value) || qIsNaN(rhs.m_number_value)) | 
|---|
| 249 | return false; | 
|---|
| 250 | return lhs.m_number_value == rhs.m_number_value; | 
|---|
| 251 |  | 
|---|
| 252 | case QScript::IntegerType: | 
|---|
| 253 | return lhs.m_int_value == rhs.m_int_value; | 
|---|
| 254 |  | 
|---|
| 255 | case QScript::BooleanType: | 
|---|
| 256 | return lhs.m_bool_value == rhs.m_bool_value; | 
|---|
| 257 |  | 
|---|
| 258 | case QScript::StringType: | 
|---|
| 259 | if (lhs.m_string_value->unique && rhs.m_string_value->unique) | 
|---|
| 260 | return lhs.m_string_value == rhs.m_string_value; | 
|---|
| 261 | return lhs.m_string_value->s == rhs.m_string_value->s; | 
|---|
| 262 |  | 
|---|
| 263 | case QScript::ObjectType: | 
|---|
| 264 | return lhs.m_object_value == rhs.m_object_value; | 
|---|
| 265 |  | 
|---|
| 266 | case QScript::ReferenceType: | 
|---|
| 267 | return lhs.m_int_value == rhs.m_int_value; | 
|---|
| 268 |  | 
|---|
| 269 | case QScript::PointerType: | 
|---|
| 270 | return lhs.m_ptr_value == rhs.m_ptr_value; | 
|---|
| 271 |  | 
|---|
| 272 | case QScript::LazyStringType: | 
|---|
| 273 | return *lhs.m_lazy_string_value == *rhs.m_lazy_string_value; | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | return false; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | inline QScriptValueImpl QScriptContextPrivate::throwTypeError(const QString &text) | 
|---|
| 280 | { | 
|---|
| 281 | return throwError(QScriptContext::TypeError, text); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | inline QScriptValueImpl QScriptContextPrivate::throwSyntaxError(const QString &text) | 
|---|
| 285 | { | 
|---|
| 286 | return throwError(QScriptContext::SyntaxError, text); | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | inline QScriptValueImpl QScriptContextPrivate::thisObject() const | 
|---|
| 290 | { | 
|---|
| 291 | return m_thisObject; | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | inline void QScriptContextPrivate::setThisObject(const QScriptValueImpl &object) | 
|---|
| 295 | { | 
|---|
| 296 | m_thisObject = object; | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | inline QScriptValueImpl QScriptContextPrivate::callee() const | 
|---|
| 300 | { | 
|---|
| 301 | return m_callee; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | inline bool QScriptContextPrivate::isCalledAsConstructor() const | 
|---|
| 305 | { | 
|---|
| 306 | return m_calledAsConstructor; | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | inline QScriptValueImpl QScriptContextPrivate::returnValue() const | 
|---|
| 310 | { | 
|---|
| 311 | return m_result; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | inline void QScriptContextPrivate::setReturnValue(const QScriptValueImpl &value) | 
|---|
| 315 | { | 
|---|
| 316 | m_result = value; | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | inline QScriptValueImpl QScriptContextPrivate::activationObject() const | 
|---|
| 320 | { | 
|---|
| 321 | if (previous && !m_activation.property(QLatin1String("arguments")).isValid()) { | 
|---|
| 322 | QScriptContextPrivate *dd = const_cast<QScriptContextPrivate*>(this); | 
|---|
| 323 | dd->m_activation.setProperty(QLatin1String("arguments"), argumentsObject()); | 
|---|
| 324 | } | 
|---|
| 325 | return m_activation; | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | inline void QScriptContextPrivate::setActivationObject(const QScriptValueImpl &activation) | 
|---|
| 329 | { | 
|---|
| 330 | m_activation = activation; | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | inline const QScriptInstruction *QScriptContextPrivate::instructionPointer() | 
|---|
| 334 | { | 
|---|
| 335 | return iPtr; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | inline void QScriptContextPrivate::setInstructionPointer(const QScriptInstruction *instructionPointer) | 
|---|
| 339 | { | 
|---|
| 340 | iPtr = instructionPointer; | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | inline const QScriptValueImpl *QScriptContextPrivate::baseStackPointer() const | 
|---|
| 344 | { | 
|---|
| 345 | return tempStack; | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | inline const QScriptValueImpl *QScriptContextPrivate::currentStackPointer() const | 
|---|
| 349 | { | 
|---|
| 350 | return stackPtr; | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 | inline QScriptContext::ExecutionState QScriptContextPrivate::state() const | 
|---|
| 354 | { | 
|---|
| 355 | return m_state; | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | QT_END_NAMESPACE | 
|---|
| 359 |  | 
|---|
| 360 | #endif // QT_NO_SCRIPT | 
|---|
| 361 | #endif | 
|---|