1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "private/qdeclarativeglobalscriptclass_p.h"
44 #include <QtCore/qstringlist.h>
45 #include <QtCore/qvector.h>
46 #include <QtScript/qscriptstring.h>
47 #include <QtScript/qscriptengine.h>
48 #include <QtScript/qscriptvalueiterator.h>
50 #include <private/qscriptdeclarativeclass_p.h>
55 Used to prevent any writes to the global object.
57 QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(QScriptEngine *engine)
58 : QScriptClass(engine)
60 QString eval = QLatin1String("eval");
61 QString version = QLatin1String("version");
63 QScriptValue originalGlobalObject = engine->globalObject();
65 QScriptValue newGlobalObject = engine->newObject();
68 QScriptValueIterator iter(originalGlobalObject);
69 QVector<QString> names;
70 QVector<QScriptValue> values;
71 QVector<QScriptValue::PropertyFlags> flags;
72 while (iter.hasNext()) {
75 QString name = iter.name();
82 values.append(iter.value());
83 flags.append(iter.flags() | QScriptValue::Undeletable);
85 newGlobalObject.setProperty(iter.scriptName(), iter.value());
87 m_illegalNames.insert(name);
89 m_staticGlobalObject = QScriptDeclarativeClass::newStaticScopeObject(
90 engine, names.size(), names.constData(), values.constData(), flags.constData());
93 newGlobalObject.setScriptClass(this);
94 engine->setGlobalObject(newGlobalObject);
97 QScriptClass::QueryFlags
98 QDeclarativeGlobalScriptClass::queryProperty(const QScriptValue &object,
99 const QScriptString &name,
100 QueryFlags flags, uint *id)
106 return HandlesWriteAccess;
109 void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object,
110 const QScriptString &name,
111 uint id, const QScriptValue &value)
116 QString error = QLatin1String("Invalid write to global property \"") +
117 name.toString() + QLatin1Char('\"');
118 engine()->currentContext()->throwError(error);
121 /* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */
122 void QDeclarativeGlobalScriptClass::explicitSetProperty(const QStringList &names, const QList<QScriptValue> &values)
124 Q_ASSERT(names.count() == values.count());
125 QScriptValue globalObject = engine()->globalObject();
127 QScriptValue v = engine()->newObject();
129 QScriptValueIterator iter(v);
130 while (iter.hasNext()) {
132 v.setProperty(iter.scriptName(), iter.value());
135 for (int ii = 0; ii < names.count(); ++ii) {
136 const QString &name = names.at(ii);
137 const QScriptValue &value = values.at(ii);
138 v.setProperty(name, value);
141 v.setScriptClass(this);
143 engine()->setGlobalObject(v);