Qt 4.8
qdeclarativeglobalscriptclass.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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 
53 
54 /*
55  Used to prevent any writes to the global object.
56 */
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;
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  }
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
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 
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 */
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 
147 
void explicitSetProperty(const QStringList &, const QList< QScriptValue > &)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QScriptClass class provides an interface for defining custom behavior of (a class of) Qt Script o...
Definition: qscriptclass.h:43
#define error(msg)
QScriptValue throwError(Error error, const QString &text)
Throws an error with the given text.
QScriptValue globalObject() const
Returns this engine&#39;s Global Object.
QString name() const
Returns the name of the last property that was jumped over using next() or previous().
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.
Definition: qscriptstring.h:38
The QScriptEngine class provides an environment for evaluating Qt Script code.
QScriptEngine * engine() const
Returns the engine that this QScriptClass is associated with.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
const_iterator insert(const T &value)
Definition: qset.h:179
unsigned int uint
Definition: qglobal.h:996
QScriptValue newObject()
Creates a QtScript object of class Object.
void setScriptClass(QScriptClass *scriptClass)
Sets the custom script class of this script object to scriptClass.
void setGlobalObject(const QScriptValue &object)
Sets this engine&#39;s Global Object to be the given object.
quint16 values[128]
QScriptValue::PropertyFlags flags() const
Returns the flags of the last property that was jumped over using next() or previous().
static QScriptValue newStaticScopeObject(QScriptEngine *, int propertyCount, const QString *names, const QScriptValue *values, const QScriptValue::PropertyFlags *flags)
Creates a scope object with a fixed set of undeletable properties.
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
virtual QString name() const
Returns the name of the script class.
virtual void setProperty(QScriptValue &object, const QScriptString &name, uint id, const QScriptValue &value)
Sets the property with the given name of the given object to the given value.
QScriptValue value() const
Returns the value of the last property that was jumped over using next() or previous().
QScriptContext * currentContext() const
Returns the current context.
const T * constData() const
Returns a const pointer to the data stored in the vector.
Definition: qvector.h:154
QScriptString scriptName() const
Returns the name of the last property that was jumped over using next() or previous().
QString toString() const
Returns the string that this QScriptString represents, or a null string if this QScriptString is not ...
virtual QueryFlags queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id)
Queries this script class for how access to the property with the given name of the given object shou...
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void next()
Advances the iterator by one position.
bool hasNext() const
Returns true if there is at least one item ahead of the iterator (i.e.