src/declarative/qml/qdeclarativeglobalscriptclass.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "private/qdeclarativeglobalscriptclass_p.h"
       
    43 
       
    44 #include <QtScript/qscriptstring.h>
       
    45 #include <QtScript/qscriptengine.h>
       
    46 #include <QtScript/qscriptvalueiterator.h>
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 /*
       
    51     Used to prevent any writes to the global object.
       
    52 */
       
    53 QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(QScriptEngine *engine)
       
    54 : QScriptClass(engine)
       
    55 {
       
    56     QString eval = QLatin1String("eval");
       
    57 
       
    58     QScriptValue globalObject = engine->globalObject();
       
    59 
       
    60     m_globalObject = engine->newObject();
       
    61     QScriptValue newGlobalObject = engine->newObject();
       
    62 
       
    63     QScriptValueIterator iter(globalObject);
       
    64 
       
    65     while (iter.hasNext()) {
       
    66         iter.next();
       
    67 
       
    68         QString name = iter.name();
       
    69 
       
    70         if (name != eval)
       
    71             m_globalObject.setProperty(iter.scriptName(), iter.value());
       
    72         newGlobalObject.setProperty(iter.scriptName(), iter.value());
       
    73 
       
    74         m_illegalNames.insert(name);
       
    75     }
       
    76 
       
    77     newGlobalObject.setScriptClass(this);
       
    78     engine->setGlobalObject(newGlobalObject);
       
    79 }
       
    80 
       
    81 QScriptClass::QueryFlags 
       
    82 QDeclarativeGlobalScriptClass::queryProperty(const QScriptValue &object,
       
    83                                     const QScriptString &name,
       
    84                                     QueryFlags flags, uint *id)
       
    85 {
       
    86     Q_UNUSED(object);
       
    87     Q_UNUSED(name);
       
    88     Q_UNUSED(flags);
       
    89     Q_UNUSED(id);
       
    90     return HandlesReadAccess | HandlesWriteAccess;
       
    91 }
       
    92 
       
    93 QScriptValue 
       
    94 QDeclarativeGlobalScriptClass::property(const QScriptValue &object,
       
    95                                const QScriptString &name, 
       
    96                                uint id)
       
    97 {
       
    98     Q_UNUSED(object);
       
    99     Q_UNUSED(name);
       
   100     Q_UNUSED(id);
       
   101     return engine()->undefinedValue();
       
   102 }
       
   103 
       
   104 void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object, 
       
   105                                        const QScriptString &name,
       
   106                                        uint id, const QScriptValue &value)
       
   107 {
       
   108     Q_UNUSED(object);
       
   109     Q_UNUSED(id);
       
   110     Q_UNUSED(value);
       
   111     QString error = QLatin1String("Invalid write to global property \"") + 
       
   112                     name.toString() + QLatin1Char('\"');
       
   113     engine()->currentContext()->throwError(error);
       
   114 }
       
   115 
       
   116 /* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */
       
   117 void QDeclarativeGlobalScriptClass::explicitSetProperty(const QString &name, const QScriptValue &value)
       
   118 {
       
   119     QScriptValue globalObject = engine()->globalObject();
       
   120 
       
   121     QScriptValue v = engine()->newObject();
       
   122 
       
   123     QScriptValueIterator iter(v);
       
   124     while (iter.hasNext()) {
       
   125         iter.next();
       
   126         v.setProperty(iter.scriptName(), iter.value());
       
   127     }
       
   128 
       
   129     v.setProperty(name, value);
       
   130     v.setScriptClass(this);
       
   131 
       
   132     engine()->setGlobalObject(v);
       
   133 }
       
   134 
       
   135 QT_END_NAMESPACE
       
   136