| 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 QSCRIPTENGINE_P_H
|
|---|
| 43 | #define QSCRIPTENGINE_P_H
|
|---|
| 44 |
|
|---|
| 45 | #include "qscriptenginefwd_p.h"
|
|---|
| 46 |
|
|---|
| 47 | #ifndef QT_NO_SCRIPT
|
|---|
| 48 |
|
|---|
| 49 | #include <QtCore/QDateTime>
|
|---|
| 50 | #include <QtCore/QMutex>
|
|---|
| 51 | #include <QtCore/QLinkedList>
|
|---|
| 52 | #include <QtCore/QString>
|
|---|
| 53 | #include <QtCore/QVector>
|
|---|
| 54 | #include <QtCore/QHash>
|
|---|
| 55 | #include <QtCore/qnumeric.h>
|
|---|
| 56 |
|
|---|
| 57 | #include "qscriptengine.h"
|
|---|
| 58 | #include "qscriptnameid_p.h"
|
|---|
| 59 | #include "qscriptobjectfwd_p.h"
|
|---|
| 60 | #include "qscriptrepository_p.h"
|
|---|
| 61 | #include "qscriptgc_p.h"
|
|---|
| 62 | #include "qscriptecmaarray_p.h"
|
|---|
| 63 | #include "qscriptecmadate_p.h"
|
|---|
| 64 | #include "qscriptecmaobject_p.h"
|
|---|
| 65 | #include "qscriptecmaboolean_p.h"
|
|---|
| 66 | #include "qscriptecmanumber_p.h"
|
|---|
| 67 | #include "qscriptecmastring_p.h"
|
|---|
| 68 | #include "qscriptecmafunction_p.h"
|
|---|
| 69 | #include "qscriptextvariant_p.h"
|
|---|
| 70 | #include "qscriptextqobject_p.h"
|
|---|
| 71 | #include "qscriptvalue_p.h"
|
|---|
| 72 | #include "qscriptcontextfwd_p.h"
|
|---|
| 73 |
|
|---|
| 74 | #include <math.h>
|
|---|
| 75 |
|
|---|
| 76 | QT_BEGIN_NAMESPACE
|
|---|
| 77 |
|
|---|
| 78 | //
|
|---|
| 79 | // W A R N I N G
|
|---|
| 80 | // -------------
|
|---|
| 81 | //
|
|---|
| 82 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 83 | // implementation detail. This header file may change from version to
|
|---|
| 84 | // version without notice, or even be removed.
|
|---|
| 85 | //
|
|---|
| 86 | // We mean it.
|
|---|
| 87 | //
|
|---|
| 88 |
|
|---|
| 89 | namespace QScript {
|
|---|
| 90 |
|
|---|
| 91 | class ArgumentsObjectData: public QScriptObjectData
|
|---|
| 92 | {
|
|---|
| 93 | public:
|
|---|
| 94 | ArgumentsObjectData() : length(0) {}
|
|---|
| 95 | virtual ~ArgumentsObjectData() {}
|
|---|
| 96 |
|
|---|
| 97 | public: // attributes
|
|---|
| 98 | QScriptValueImpl activation;
|
|---|
| 99 | uint length;
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | } // namespace QScript
|
|---|
| 103 |
|
|---|
| 104 | inline QScriptEnginePrivate *QScriptEnginePrivate::get(QScriptEngine *q)
|
|---|
| 105 | {
|
|---|
| 106 | if (q)
|
|---|
| 107 | return q->d_func();
|
|---|
| 108 | return 0;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | inline const QScriptEnginePrivate *QScriptEnginePrivate::get(const QScriptEngine *q)
|
|---|
| 112 | {
|
|---|
| 113 | if (q)
|
|---|
| 114 | return q->d_func();
|
|---|
| 115 | return 0;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | inline QScriptEngine *QScriptEnginePrivate::get(QScriptEnginePrivate *d)
|
|---|
| 119 | {
|
|---|
| 120 | return d->q_func();
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | inline QString QScriptEnginePrivate::toString(QScriptNameIdImpl *id)
|
|---|
| 124 | {
|
|---|
| 125 | if (! id)
|
|---|
| 126 | return QString();
|
|---|
| 127 |
|
|---|
| 128 | return id->s;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | inline QString QScriptEnginePrivate::memberName(const QScript::Member &member) const
|
|---|
| 132 | {
|
|---|
| 133 | return toString(member.nameId());
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | inline void QScriptEnginePrivate::newReference(QScriptValueImpl *o, int mode)
|
|---|
| 137 | {
|
|---|
| 138 | Q_ASSERT(o);
|
|---|
| 139 | o->m_type = QScript::ReferenceType;
|
|---|
| 140 | o->m_int_value = (mode);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | inline void QScriptEnginePrivate::newActivation(QScriptValueImpl *o)
|
|---|
| 144 | {
|
|---|
| 145 | Q_ASSERT(o);
|
|---|
| 146 | newObject(o, nullValue(), m_class_activation);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | inline void QScriptEnginePrivate::newPointer(QScriptValueImpl *o, void *ptr)
|
|---|
| 150 | {
|
|---|
| 151 | Q_ASSERT(o);
|
|---|
| 152 | o->m_type = QScript::PointerType;
|
|---|
| 153 | o->m_ptr_value = ptr;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | inline void QScriptEnginePrivate::newInteger(QScriptValueImpl *o, int i)
|
|---|
| 157 | {
|
|---|
| 158 | Q_ASSERT(o);
|
|---|
| 159 | o->m_type = QScript::IntegerType;
|
|---|
| 160 | o->m_int_value = (i);
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | inline void QScriptEnginePrivate::newNameId(QScriptValueImpl *o, const QString &s)
|
|---|
| 164 | {
|
|---|
| 165 | Q_ASSERT(o);
|
|---|
| 166 | o->m_type = QScript::StringType;
|
|---|
| 167 | o->m_string_value = (nameId(s, /*persistent=*/false));
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | inline void QScriptEnginePrivate::newString(QScriptValueImpl *o, const QString &s)
|
|---|
| 171 | {
|
|---|
| 172 | Q_ASSERT(o);
|
|---|
| 173 | o->m_type = QScript::StringType;
|
|---|
| 174 | QScriptNameIdImpl *entry = new QScriptNameIdImpl(s);
|
|---|
| 175 | m_tempStringRepository.append(entry);
|
|---|
| 176 | o->m_string_value = (entry);
|
|---|
| 177 | m_newAllocatedTempStringRepositoryChars += s.length();
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | inline void QScriptEnginePrivate::newNameId(QScriptValueImpl *o, QScriptNameIdImpl *id)
|
|---|
| 181 | {
|
|---|
| 182 | Q_ASSERT(o);
|
|---|
| 183 | o->m_type = QScript::StringType;
|
|---|
| 184 | o->m_string_value = (id);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | inline const QScript::IdTable *QScriptEnginePrivate::idTable() const
|
|---|
| 188 | {
|
|---|
| 189 | return &m_id_table;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | inline qsreal QScriptEnginePrivate::convertToNativeDouble(const QScriptValueImpl &value)
|
|---|
| 193 | {
|
|---|
| 194 | Q_ASSERT (value.isValid());
|
|---|
| 195 |
|
|---|
| 196 | if (value.isNumber())
|
|---|
| 197 | return value.m_number_value;
|
|---|
| 198 |
|
|---|
| 199 | return convertToNativeDouble_helper(value);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | inline qint32 QScriptEnginePrivate::convertToNativeInt32(const QScriptValueImpl &value)
|
|---|
| 203 | {
|
|---|
| 204 | Q_ASSERT (value.isValid());
|
|---|
| 205 |
|
|---|
| 206 | return toInt32 (convertToNativeDouble(value));
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 | inline bool QScriptEnginePrivate::convertToNativeBoolean(const QScriptValueImpl &value)
|
|---|
| 211 | {
|
|---|
| 212 | Q_ASSERT (value.isValid());
|
|---|
| 213 |
|
|---|
| 214 | if (value.isBoolean())
|
|---|
| 215 | return value.m_bool_value;
|
|---|
| 216 |
|
|---|
| 217 | return convertToNativeBoolean_helper(value);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | inline QString QScriptEnginePrivate::convertToNativeString(const QScriptValueImpl &value)
|
|---|
| 221 | {
|
|---|
| 222 | Q_ASSERT (value.isValid());
|
|---|
| 223 |
|
|---|
| 224 | if (value.isString())
|
|---|
| 225 | return value.m_string_value->s;
|
|---|
| 226 |
|
|---|
| 227 | return convertToNativeString_helper(value);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | inline qsreal QScriptEnginePrivate::toInteger(qsreal n)
|
|---|
| 231 | {
|
|---|
| 232 | if (qIsNaN(n))
|
|---|
| 233 | return 0;
|
|---|
| 234 |
|
|---|
| 235 | if (n == 0 || qIsInf(n))
|
|---|
| 236 | return n;
|
|---|
| 237 |
|
|---|
| 238 | int sign = n < 0 ? -1 : 1;
|
|---|
| 239 | return sign * ::floor(::fabs(n));
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | inline qint32 QScriptEnginePrivate::toInt32(qsreal n)
|
|---|
| 243 | {
|
|---|
| 244 | if (qIsNaN(n) || qIsInf(n) || (n == 0))
|
|---|
| 245 | return 0;
|
|---|
| 246 |
|
|---|
| 247 | double sign = (n < 0) ? -1.0 : 1.0;
|
|---|
| 248 | qsreal abs_n = fabs(n);
|
|---|
| 249 |
|
|---|
| 250 | n = ::fmod(sign * ::floor(abs_n), D32);
|
|---|
| 251 | const double D31 = D32 / 2.0;
|
|---|
| 252 |
|
|---|
| 253 | if (sign == -1 && n < -D31)
|
|---|
| 254 | n += D32;
|
|---|
| 255 |
|
|---|
| 256 | else if (sign != -1 && n >= D31)
|
|---|
| 257 | n -= D32;
|
|---|
| 258 |
|
|---|
| 259 | return qint32 (n);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | inline quint32 QScriptEnginePrivate::toUint32(qsreal n)
|
|---|
| 263 | {
|
|---|
| 264 | if (qIsNaN(n) || qIsInf(n) || (n == 0))
|
|---|
| 265 | return 0;
|
|---|
| 266 |
|
|---|
| 267 | double sign = (n < 0) ? -1.0 : 1.0;
|
|---|
| 268 | qsreal abs_n = fabs(n);
|
|---|
| 269 |
|
|---|
| 270 | n = ::fmod(sign * ::floor(abs_n), D32);
|
|---|
| 271 |
|
|---|
| 272 | if (n < 0)
|
|---|
| 273 | n += D32;
|
|---|
| 274 |
|
|---|
| 275 | return quint32 (n);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | inline quint16 QScriptEnginePrivate::toUint16(qsreal n)
|
|---|
| 279 | {
|
|---|
| 280 | if (qIsNaN(n) || qIsInf(n) || (n == 0))
|
|---|
| 281 | return 0;
|
|---|
| 282 |
|
|---|
| 283 | double sign = (n < 0) ? -1.0 : 1.0;
|
|---|
| 284 | qsreal abs_n = fabs(n);
|
|---|
| 285 |
|
|---|
| 286 | n = ::fmod(sign * ::floor(abs_n), D16);
|
|---|
| 287 |
|
|---|
| 288 | if (n < 0)
|
|---|
| 289 | n += D16;
|
|---|
| 290 |
|
|---|
| 291 | return quint16 (n);
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | inline QScript::AST::Node *QScriptEnginePrivate::abstractSyntaxTree() const
|
|---|
| 295 | {
|
|---|
| 296 | return m_abstractSyntaxTree;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | inline QScript::MemoryPool *QScriptEnginePrivate::nodePool()
|
|---|
| 300 | {
|
|---|
| 301 | return m_pool;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | inline void QScriptEnginePrivate::setNodePool(QScript::MemoryPool *pool)
|
|---|
| 305 | {
|
|---|
| 306 | m_pool = pool;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | inline QScript::Lexer *QScriptEnginePrivate::lexer()
|
|---|
| 310 | {
|
|---|
| 311 | return m_lexer;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | inline void QScriptEnginePrivate::setLexer(QScript::Lexer *lexer)
|
|---|
| 315 | {
|
|---|
| 316 | m_lexer = lexer;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | inline QScriptObject *QScriptEnginePrivate::allocObject()
|
|---|
| 320 | {
|
|---|
| 321 | return objectAllocator(m_objectGeneration);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | inline QScriptContextPrivate *QScriptEnginePrivate::currentContext() const
|
|---|
| 325 | {
|
|---|
| 326 | return m_context;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | inline QScriptContextPrivate *QScriptEnginePrivate::pushContext()
|
|---|
| 330 | {
|
|---|
| 331 | QScriptContext *context = m_frameRepository.get();
|
|---|
| 332 | QScriptContextPrivate *ctx_p = QScriptContextPrivate::get(context);
|
|---|
| 333 | ctx_p->init(m_context);
|
|---|
| 334 | m_context = ctx_p;
|
|---|
| 335 | #ifndef Q_SCRIPT_NO_EVENT_NOTIFY
|
|---|
| 336 | notifyContextPush();
|
|---|
| 337 | #endif
|
|---|
| 338 | return m_context;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | inline void QScriptEnginePrivate::popContext()
|
|---|
| 342 | {
|
|---|
| 343 | Q_ASSERT(m_context != 0);
|
|---|
| 344 | #ifndef Q_SCRIPT_NO_EVENT_NOTIFY
|
|---|
| 345 | notifyContextPop();
|
|---|
| 346 | #endif
|
|---|
| 347 | QScriptContextPrivate *context = m_context;
|
|---|
| 348 | m_context = context->parentContext();
|
|---|
| 349 | if (m_context) {
|
|---|
| 350 | QScriptContextPrivate *p1 = m_context;
|
|---|
| 351 | QScriptContextPrivate *p2 = context;
|
|---|
| 352 | if ((p1->m_state != QScriptContext::ExceptionState)
|
|---|
| 353 | || (p2->m_state == QScriptContext::ExceptionState)) {
|
|---|
| 354 | // propagate the state
|
|---|
| 355 | p1->m_result = p2->m_result;
|
|---|
| 356 | p1->m_state = p2->m_state;
|
|---|
| 357 | // only update errorLineNumber if there actually was an exception
|
|---|
| 358 | if (p2->m_state == QScriptContext::ExceptionState) {
|
|---|
| 359 | if (p2->errorLineNumber != -1)
|
|---|
| 360 | p1->errorLineNumber = p2->errorLineNumber;
|
|---|
| 361 | else
|
|---|
| 362 | p1->errorLineNumber = p1->currentLine;
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 | }
|
|---|
| 366 | m_frameRepository.release(QScriptContextPrivate::get(context));
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | inline void QScriptEnginePrivate::maybeGC()
|
|---|
| 370 | {
|
|---|
| 371 | if (objectAllocator.blocked())
|
|---|
| 372 | return;
|
|---|
| 373 |
|
|---|
| 374 | bool do_string_gc = ((m_stringRepository.size() - m_oldStringRepositorySize) > 256)
|
|---|
| 375 | || (m_newAllocatedStringRepositoryChars > 0x800000);
|
|---|
| 376 | do_string_gc |= ((m_tempStringRepository.size() - m_oldTempStringRepositorySize) > 1024)
|
|---|
| 377 | || (m_newAllocatedTempStringRepositoryChars > 0x800000);
|
|---|
| 378 |
|
|---|
| 379 | if (! do_string_gc && ! objectAllocator.poll())
|
|---|
| 380 | return;
|
|---|
| 381 |
|
|---|
| 382 | maybeGC_helper(do_string_gc);
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | inline void QScriptEnginePrivate::adjustBytesAllocated(int bytes)
|
|---|
| 386 | {
|
|---|
| 387 | objectAllocator.adjustBytesAllocated(bytes);
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | inline bool QScriptEnginePrivate::blockGC(bool block)
|
|---|
| 391 | {
|
|---|
| 392 | return objectAllocator.blockGC(block);
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | inline void QScriptEnginePrivate::markString(QScriptNameIdImpl *id, int /*generation*/)
|
|---|
| 396 | {
|
|---|
| 397 | id->used = true;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | inline QScriptValueImpl QScriptEnginePrivate::createFunction(QScriptFunction *fun)
|
|---|
| 401 | {
|
|---|
| 402 | QScriptValueImpl v;
|
|---|
| 403 | newFunction(&v, fun);
|
|---|
| 404 | return v;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | inline QScriptValueImpl QScriptEnginePrivate::newArray(const QScript::Array &value)
|
|---|
| 408 | {
|
|---|
| 409 | QScriptValueImpl v;
|
|---|
| 410 | newArray(&v, value);
|
|---|
| 411 | return v;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | inline QScriptValueImpl QScriptEnginePrivate::newArray(uint length)
|
|---|
| 415 | {
|
|---|
| 416 | QScriptValueImpl v;
|
|---|
| 417 | QScript::Array a(this);
|
|---|
| 418 | a.resize(length);
|
|---|
| 419 | newArray(&v, a);
|
|---|
| 420 | return v;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | inline QScriptClassInfo *QScriptEnginePrivate::registerClass(const QString &pname, int type)
|
|---|
| 424 | {
|
|---|
| 425 | if (type == -1)
|
|---|
| 426 | type = ++m_class_prev_id;
|
|---|
| 427 |
|
|---|
| 428 | QScriptClassInfo *oc = new QScriptClassInfo(this, QScriptClassInfo::Type(type), pname);
|
|---|
| 429 | m_allocated_classes.append(oc);
|
|---|
| 430 |
|
|---|
| 431 | return oc;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | inline QScriptClassInfo *QScriptEnginePrivate::registerClass(const QString &name)
|
|---|
| 435 | {
|
|---|
| 436 | return registerClass(name, -1);
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | inline QScriptValueImpl QScriptEnginePrivate::createFunction(QScriptInternalFunctionSignature fun,
|
|---|
| 440 | int length, QScriptClassInfo *classInfo, const QString &name)
|
|---|
| 441 | {
|
|---|
| 442 | return createFunction(new QScript::C2Function(fun, length, classInfo, name));
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | inline void QScriptEnginePrivate::newFunction(QScriptValueImpl *o, QScriptFunction *function)
|
|---|
| 446 | {
|
|---|
| 447 | QScriptValueImpl proto;
|
|---|
| 448 | if (functionConstructor)
|
|---|
| 449 | proto = functionConstructor->publicPrototype;
|
|---|
| 450 | else {
|
|---|
| 451 | // creating the Function prototype object
|
|---|
| 452 | Q_ASSERT(objectConstructor);
|
|---|
| 453 | proto = objectConstructor->publicPrototype;
|
|---|
| 454 | }
|
|---|
| 455 | newObject(o, proto, m_class_function);
|
|---|
| 456 | o->setObjectData(function);
|
|---|
| 457 | }
|
|---|
| 458 |
|
|---|
| 459 | inline void QScriptEnginePrivate::newConstructor(QScriptValueImpl *ctor,
|
|---|
| 460 | QScriptFunction *function,
|
|---|
| 461 | QScriptValueImpl &proto)
|
|---|
| 462 | {
|
|---|
| 463 | newFunction(ctor, function);
|
|---|
| 464 | ctor->setProperty(m_id_table.id_prototype, proto,
|
|---|
| 465 | QScriptValue::Undeletable
|
|---|
| 466 | | QScriptValue::ReadOnly
|
|---|
| 467 | | QScriptValue::SkipInEnumeration);
|
|---|
| 468 | proto.setProperty(m_id_table.id_constructor, *ctor,
|
|---|
| 469 | QScriptValue::Undeletable
|
|---|
| 470 | | QScriptValue::SkipInEnumeration);
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | inline void QScriptEnginePrivate::newArguments(QScriptValueImpl *object,
|
|---|
| 474 | const QScriptValueImpl &activation,
|
|---|
| 475 | uint length,
|
|---|
| 476 | const QScriptValueImpl &callee)
|
|---|
| 477 | {
|
|---|
| 478 | QScript::ArgumentsObjectData *data = new QScript::ArgumentsObjectData();
|
|---|
| 479 | data->activation = activation;
|
|---|
| 480 | data->length = length;
|
|---|
| 481 | newObject(object, m_class_arguments);
|
|---|
| 482 | object->setObjectData(data);
|
|---|
| 483 | object->setProperty(m_id_table.id_callee, callee,
|
|---|
| 484 | QScriptValue::SkipInEnumeration);
|
|---|
| 485 | object->setProperty(m_id_table.id_length, QScriptValueImpl(length),
|
|---|
| 486 | QScriptValue::SkipInEnumeration);
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | inline QScriptFunction *QScriptEnginePrivate::convertToNativeFunction(const QScriptValueImpl &object)
|
|---|
| 490 | {
|
|---|
| 491 | if (object.isFunction())
|
|---|
| 492 | return static_cast<QScriptFunction*> (object.objectData());
|
|---|
| 493 | return 0;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | inline QScriptValue QScriptEnginePrivate::toPublic(const QScriptValueImpl &value)
|
|---|
| 497 | {
|
|---|
| 498 | if (!value.isValid())
|
|---|
| 499 | return QScriptValue();
|
|---|
| 500 |
|
|---|
| 501 | QScriptValuePrivate *p = registerValue(value);
|
|---|
| 502 | QScriptValue v;
|
|---|
| 503 | QScriptValuePrivate::init(v, p);
|
|---|
| 504 | return v;
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | inline QScriptValueImpl QScriptEnginePrivate::toImpl(const QScriptValue &value)
|
|---|
| 508 | {
|
|---|
| 509 | QScriptValuePrivate *p = QScriptValuePrivate::get(value);
|
|---|
| 510 | if (!p)
|
|---|
| 511 | return QScriptValueImpl();
|
|---|
| 512 | if (p->value.type() == QScript::LazyStringType)
|
|---|
| 513 | return toImpl_helper(value);
|
|---|
| 514 | return p->value;
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | inline QScriptValueImplList QScriptEnginePrivate::toImplList(const QScriptValueList &lst)
|
|---|
| 518 | {
|
|---|
| 519 | QScriptValueImplList result;
|
|---|
| 520 | QScriptValueList::const_iterator it;
|
|---|
| 521 | for (it = lst.constBegin(); it != lst.constEnd(); ++it)
|
|---|
| 522 | result.append(toImpl(*it));
|
|---|
| 523 | return result;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | inline QScriptValueImpl QScriptEnginePrivate::toObject(const QScriptValueImpl &value)
|
|---|
| 527 | {
|
|---|
| 528 | if (value.isObject() || !value.isValid())
|
|---|
| 529 | return value;
|
|---|
| 530 | return toObject_helper(value);
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | inline QScriptValueImpl QScriptEnginePrivate::toPrimitive(const QScriptValueImpl &object,
|
|---|
| 534 | QScriptValueImpl::TypeHint hint)
|
|---|
| 535 | {
|
|---|
| 536 | Q_ASSERT(object.isValid());
|
|---|
| 537 |
|
|---|
| 538 | if (! object.isObject())
|
|---|
| 539 | return object;
|
|---|
| 540 |
|
|---|
| 541 | return toPrimitive_helper(object, hint);
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | inline QDateTime QScriptEnginePrivate::toDateTime(const QScriptValueImpl &value) const
|
|---|
| 545 | {
|
|---|
| 546 | return dateConstructor->toDateTime(value);
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | inline void QScriptEnginePrivate::newArray(QScriptValueImpl *object, const QScript::Array &value)
|
|---|
| 550 | {
|
|---|
| 551 | arrayConstructor->newArray(object, value);
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | inline void QScriptEnginePrivate::newObject(QScriptValueImpl *o, const QScriptValueImpl &proto,
|
|---|
| 555 | QScriptClassInfo *oc)
|
|---|
| 556 | {
|
|---|
| 557 | Q_ASSERT(o != 0);
|
|---|
| 558 |
|
|---|
| 559 | QScriptObject *od = allocObject();
|
|---|
| 560 | od->reset();
|
|---|
| 561 | od->m_id = ++m_next_object_id;
|
|---|
| 562 | if (proto.isValid())
|
|---|
| 563 | od->m_prototype = proto;
|
|---|
| 564 | else {
|
|---|
| 565 | Q_ASSERT(objectConstructor);
|
|---|
| 566 | od->m_prototype = objectConstructor->publicPrototype;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | o->m_type = QScript::ObjectType;
|
|---|
| 570 | od->m_class = (oc ? oc : m_class_object);
|
|---|
| 571 | o->m_object_value = od;
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | inline void QScriptEnginePrivate::newObject(QScriptValueImpl *o, QScriptClassInfo *oc)
|
|---|
| 575 | {
|
|---|
| 576 | newObject(o, objectConstructor->publicPrototype, oc);
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | inline QScriptValueImpl QScriptEnginePrivate::newObject()
|
|---|
| 580 | {
|
|---|
| 581 | QScriptValueImpl v;
|
|---|
| 582 | newObject(&v);
|
|---|
| 583 | return v;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | inline void QScriptEnginePrivate::newVariant(QScriptValueImpl *out,
|
|---|
| 587 | const QVariant &value,
|
|---|
| 588 | bool setDefaultPrototype)
|
|---|
| 589 | {
|
|---|
| 590 | Q_ASSERT(variantConstructor != 0);
|
|---|
| 591 | variantConstructor->newVariant(out, value);
|
|---|
| 592 | if (setDefaultPrototype) {
|
|---|
| 593 | QScriptValueImpl proto = defaultPrototype(value.userType());
|
|---|
| 594 | if (proto.isValid())
|
|---|
| 595 | out->setPrototype(proto);
|
|---|
| 596 | }
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | #ifndef QT_NO_QOBJECT
|
|---|
| 600 | # ifndef Q_SCRIPT_NO_QMETAOBJECT_CACHE
|
|---|
| 601 | inline QScriptMetaObject *QScriptEnginePrivate::cachedMetaObject(const QMetaObject *meta)
|
|---|
| 602 | {
|
|---|
| 603 | QScriptMetaObject *value = m_cachedMetaObjects.value(meta);
|
|---|
| 604 | if (!value) {
|
|---|
| 605 | value = new QScriptMetaObject;
|
|---|
| 606 | m_cachedMetaObjects.insert(meta, value);
|
|---|
| 607 | }
|
|---|
| 608 | return value;
|
|---|
| 609 | }
|
|---|
| 610 | # endif
|
|---|
| 611 | #endif // !QT_NO_QOBJECT
|
|---|
| 612 |
|
|---|
| 613 | inline QScriptNameIdImpl *QScriptEnginePrivate::nameId(const QString &str, bool persistent)
|
|---|
| 614 | {
|
|---|
| 615 | QScriptNameIdImpl *entry = toStringEntry(str);
|
|---|
| 616 | if (! entry)
|
|---|
| 617 | entry = insertStringEntry(str);
|
|---|
| 618 |
|
|---|
| 619 | Q_ASSERT(entry->unique);
|
|---|
| 620 |
|
|---|
| 621 | if (persistent)
|
|---|
| 622 | entry->persistent = true;
|
|---|
| 623 |
|
|---|
| 624 | return entry;
|
|---|
| 625 | }
|
|---|
| 626 |
|
|---|
| 627 | inline QScriptNameIdImpl *QScriptEnginePrivate::intern(const QChar *u, int s)
|
|---|
| 628 | {
|
|---|
| 629 | QString tmp(u, s);
|
|---|
| 630 | return nameId(tmp, /*persistent=*/ true);
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | inline QScriptValueImpl QScriptEnginePrivate::valueFromVariant(const QVariant &v)
|
|---|
| 634 | {
|
|---|
| 635 | QScriptValueImpl result = create(v.userType(), v.data());
|
|---|
| 636 | Q_ASSERT(result.isValid());
|
|---|
| 637 | return result;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | inline QScriptValueImpl QScriptEnginePrivate::undefinedValue()
|
|---|
| 641 | {
|
|---|
| 642 | return m_undefinedValue;
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | inline QScriptValueImpl QScriptEnginePrivate::nullValue()
|
|---|
| 646 | {
|
|---|
| 647 | return m_nullValue;
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | inline QScriptValueImpl QScriptEnginePrivate::defaultPrototype(int metaTypeId) const
|
|---|
| 651 | {
|
|---|
| 652 | QScriptCustomTypeInfo info = m_customTypes.value(metaTypeId);
|
|---|
| 653 | return info.prototype;
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | inline void QScriptEnginePrivate::setDefaultPrototype(int metaTypeId, const QScriptValueImpl &prototype)
|
|---|
| 657 | {
|
|---|
| 658 | QScriptCustomTypeInfo info = m_customTypes.value(metaTypeId);
|
|---|
| 659 | info.prototype = prototype;
|
|---|
| 660 | m_customTypes.insert(metaTypeId, info);
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | inline uint _q_scriptHash(const QString &key)
|
|---|
| 664 | {
|
|---|
| 665 | const QChar *p = key.unicode();
|
|---|
| 666 | int n = qMin(key.size(), 128);
|
|---|
| 667 | uint h = key.size();
|
|---|
| 668 | uint g;
|
|---|
| 669 |
|
|---|
| 670 | while (n--) {
|
|---|
| 671 | h = (h << 4) + (*p++).unicode();
|
|---|
| 672 | if ((g = (h & 0xf0000000)) != 0)
|
|---|
| 673 | h ^= g >> 23;
|
|---|
| 674 | h &= ~g;
|
|---|
| 675 | }
|
|---|
| 676 | return h;
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | inline QScriptNameIdImpl *QScriptEnginePrivate::toStringEntry(const QString &s)
|
|---|
| 680 | {
|
|---|
| 681 | uint h = _q_scriptHash(s) % m_string_hash_size;
|
|---|
| 682 |
|
|---|
| 683 | for (QScriptNameIdImpl *entry = m_string_hash_base[h]; entry && entry->h == h; entry = entry->next) {
|
|---|
| 684 | if (entry->s == s)
|
|---|
| 685 | return entry;
|
|---|
| 686 | }
|
|---|
| 687 |
|
|---|
| 688 | return 0;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | inline bool QScriptEnginePrivate::lessThan(const QScriptValueImpl &lhs, const QScriptValueImpl &rhs)
|
|---|
| 692 | {
|
|---|
| 693 | return QScriptContextPrivate::lt_cmp(lhs, rhs);
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | inline bool QScriptEnginePrivate::equals(const QScriptValueImpl &lhs, const QScriptValueImpl &rhs)
|
|---|
| 697 | {
|
|---|
| 698 | return QScriptContextPrivate::eq_cmp(lhs, rhs);
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | inline bool QScriptEnginePrivate::strictlyEquals(const QScriptValueImpl &lhs, const QScriptValueImpl &rhs)
|
|---|
| 702 | {
|
|---|
| 703 | return QScriptContextPrivate::strict_eq_cmp(lhs, rhs);
|
|---|
| 704 | }
|
|---|
| 705 |
|
|---|
| 706 | inline void QScriptEnginePrivate::unregisterValue(QScriptValuePrivate *p)
|
|---|
| 707 | {
|
|---|
| 708 | QScriptValueImpl &v = p->value;
|
|---|
| 709 | Q_ASSERT(v.isValid());
|
|---|
| 710 | if (v.isString()) {
|
|---|
| 711 | QScriptNameIdImpl *id = v.stringValue();
|
|---|
| 712 | m_stringHandles.remove(id);
|
|---|
| 713 | } else if (v.isObject()) {
|
|---|
| 714 | QScriptObject *instance = v.objectValue();
|
|---|
| 715 | m_objectHandles.remove(instance);
|
|---|
| 716 | } else {
|
|---|
| 717 | int i = m_otherHandles.indexOf(p);
|
|---|
| 718 | Q_ASSERT(i != -1);
|
|---|
| 719 | m_otherHandles.remove(i);
|
|---|
| 720 | }
|
|---|
| 721 | m_handleRepository.release(p);
|
|---|
| 722 | }
|
|---|
| 723 |
|
|---|
| 724 | inline QScriptValueImpl QScriptEnginePrivate::globalObject() const
|
|---|
| 725 | {
|
|---|
| 726 | return m_globalObject;
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | inline bool QScriptEnginePrivate::hasUncaughtException() const
|
|---|
| 730 | {
|
|---|
| 731 | return (currentContext()->state() == QScriptContext::ExceptionState);
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | inline QScriptValueImpl QScriptEnginePrivate::uncaughtException() const
|
|---|
| 735 | {
|
|---|
| 736 | if (!hasUncaughtException())
|
|---|
| 737 | return QScriptValueImpl();
|
|---|
| 738 | return currentContext()->returnValue();
|
|---|
| 739 | }
|
|---|
| 740 |
|
|---|
| 741 | inline void QScriptEnginePrivate::maybeProcessEvents()
|
|---|
| 742 | {
|
|---|
| 743 | if (m_processEventsInterval > 0 && ++m_processEventIncr > 512) {
|
|---|
| 744 | m_processEventIncr = 0;
|
|---|
| 745 | processEvents();
|
|---|
| 746 | }
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | inline bool QScriptEnginePrivate::shouldAbort() const
|
|---|
| 750 | {
|
|---|
| 751 | return m_abort;
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | inline void QScriptEnginePrivate::resetAbortFlag()
|
|---|
| 755 | {
|
|---|
| 756 | m_abort = false;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | #ifndef Q_SCRIPT_NO_EVENT_NOTIFY
|
|---|
| 760 |
|
|---|
| 761 | inline bool QScriptEnginePrivate::shouldNotify() const
|
|---|
| 762 | {
|
|---|
| 763 | return m_agent != 0;
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | inline void QScriptEnginePrivate::notifyScriptLoad(
|
|---|
| 767 | qint64 id, const QString &program,
|
|---|
| 768 | const QString &fileName, int lineNumber)
|
|---|
| 769 | {
|
|---|
| 770 | if (shouldNotify())
|
|---|
| 771 | notifyScriptLoad_helper(id, program, fileName, lineNumber);
|
|---|
| 772 | }
|
|---|
| 773 |
|
|---|
| 774 | inline void QScriptEnginePrivate::notifyScriptUnload(qint64 id)
|
|---|
| 775 | {
|
|---|
| 776 | if (shouldNotify())
|
|---|
| 777 | notifyScriptUnload_helper(id);
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | inline void QScriptEnginePrivate::notifyPositionChange(QScriptContextPrivate *ctx)
|
|---|
| 781 | {
|
|---|
| 782 | Q_ASSERT(m_agent != 0);
|
|---|
| 783 | notifyPositionChange_helper(ctx);
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | inline void QScriptEnginePrivate::notifyContextPush()
|
|---|
| 787 | {
|
|---|
| 788 | if (shouldNotify())
|
|---|
| 789 | notifyContextPush_helper();
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | inline void QScriptEnginePrivate::notifyContextPop()
|
|---|
| 793 | {
|
|---|
| 794 | if (shouldNotify())
|
|---|
| 795 | notifyContextPop_helper();
|
|---|
| 796 | }
|
|---|
| 797 |
|
|---|
| 798 | inline void QScriptEnginePrivate::notifyFunctionEntry(QScriptContextPrivate *ctx)
|
|---|
| 799 | {
|
|---|
| 800 | if (shouldNotify())
|
|---|
| 801 | notifyFunctionEntry_helper(ctx);
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | inline void QScriptEnginePrivate::notifyFunctionExit(QScriptContextPrivate *ctx)
|
|---|
| 805 | {
|
|---|
| 806 | if (shouldNotify())
|
|---|
| 807 | notifyFunctionExit_helper(ctx);
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | inline void QScriptEnginePrivate::notifyException(QScriptContextPrivate *ctx)
|
|---|
| 811 | {
|
|---|
| 812 | if (shouldNotify())
|
|---|
| 813 | notifyException_helper(ctx);
|
|---|
| 814 | }
|
|---|
| 815 |
|
|---|
| 816 | inline void QScriptEnginePrivate::notifyExceptionCatch(QScriptContextPrivate *ctx)
|
|---|
| 817 | {
|
|---|
| 818 | if (shouldNotify())
|
|---|
| 819 | notifyExceptionCatch_helper(ctx);
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | #endif // Q_SCRIPT_NO_EVENT_NOTIFY
|
|---|
| 823 |
|
|---|
| 824 | QT_END_NAMESPACE
|
|---|
| 825 |
|
|---|
| 826 | #endif // QT_NO_SCRIPT
|
|---|
| 827 |
|
|---|
| 828 | #endif
|
|---|