1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "private/qdeclarativeglobalscriptclass_p.h"
|
---|
43 |
|
---|
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>
|
---|
49 |
|
---|
50 | #include <private/qscriptdeclarativeclass_p.h>
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | /*
|
---|
55 | Used to prevent any writes to the global object.
|
---|
56 | */
|
---|
57 | QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(QScriptEngine *engine)
|
---|
58 | : QScriptClass(engine)
|
---|
59 | {
|
---|
60 | QString eval = QLatin1String("eval");
|
---|
61 | QString version = QLatin1String("version");
|
---|
62 |
|
---|
63 | QScriptValue originalGlobalObject = engine->globalObject();
|
---|
64 |
|
---|
65 | QScriptValue newGlobalObject = engine->newObject();
|
---|
66 |
|
---|
67 | {
|
---|
68 | QScriptValueIterator iter(originalGlobalObject);
|
---|
69 | QVector<QString> names;
|
---|
70 | QVector<QScriptValue> values;
|
---|
71 | QVector<QScriptValue::PropertyFlags> flags;
|
---|
72 | while (iter.hasNext()) {
|
---|
73 | iter.next();
|
---|
74 |
|
---|
75 | QString name = iter.name();
|
---|
76 |
|
---|
77 | if (name == version)
|
---|
78 | continue;
|
---|
79 |
|
---|
80 | if (name != eval) {
|
---|
81 | names.append(name);
|
---|
82 | values.append(iter.value());
|
---|
83 | flags.append(iter.flags() | QScriptValue::Undeletable);
|
---|
84 | }
|
---|
85 | newGlobalObject.setProperty(iter.scriptName(), iter.value());
|
---|
86 |
|
---|
87 | m_illegalNames.insert(name);
|
---|
88 | }
|
---|
89 | m_staticGlobalObject = QScriptDeclarativeClass::newStaticScopeObject(
|
---|
90 | engine, names.size(), names.constData(), values.constData(), flags.constData());
|
---|
91 | }
|
---|
92 |
|
---|
93 | newGlobalObject.setScriptClass(this);
|
---|
94 | engine->setGlobalObject(newGlobalObject);
|
---|
95 | }
|
---|
96 |
|
---|
97 | QScriptClass::QueryFlags
|
---|
98 | QDeclarativeGlobalScriptClass::queryProperty(const QScriptValue &object,
|
---|
99 | const QScriptString &name,
|
---|
100 | QueryFlags flags, uint *id)
|
---|
101 | {
|
---|
102 | Q_UNUSED(object);
|
---|
103 | Q_UNUSED(name);
|
---|
104 | Q_UNUSED(flags);
|
---|
105 | Q_UNUSED(id);
|
---|
106 | return HandlesWriteAccess;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object,
|
---|
110 | const QScriptString &name,
|
---|
111 | uint id, const QScriptValue &value)
|
---|
112 | {
|
---|
113 | Q_UNUSED(object);
|
---|
114 | Q_UNUSED(id);
|
---|
115 | Q_UNUSED(value);
|
---|
116 | QString error = QLatin1String("Invalid write to global property \"") +
|
---|
117 | name.toString() + QLatin1Char('\"');
|
---|
118 | engine()->currentContext()->throwError(error);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */
|
---|
122 | void QDeclarativeGlobalScriptClass::explicitSetProperty(const QStringList &names, const QList<QScriptValue> &values)
|
---|
123 | {
|
---|
124 | Q_ASSERT(names.count() == values.count());
|
---|
125 | QScriptValue globalObject = engine()->globalObject();
|
---|
126 |
|
---|
127 | QScriptValue v = engine()->newObject();
|
---|
128 |
|
---|
129 | QScriptValueIterator iter(v);
|
---|
130 | while (iter.hasNext()) {
|
---|
131 | iter.next();
|
---|
132 | v.setProperty(iter.scriptName(), iter.value());
|
---|
133 | }
|
---|
134 |
|
---|
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);
|
---|
139 | }
|
---|
140 |
|
---|
141 | v.setScriptClass(this);
|
---|
142 |
|
---|
143 | engine()->setGlobalObject(v);
|
---|
144 | }
|
---|
145 |
|
---|
146 | QT_END_NAMESPACE
|
---|
147 |
|
---|