39 ** |
39 ** |
40 ****************************************************************************/ |
40 ****************************************************************************/ |
41 |
41 |
42 #include "private/qdeclarativeglobalscriptclass_p.h" |
42 #include "private/qdeclarativeglobalscriptclass_p.h" |
43 |
43 |
|
44 #include <QtCore/qstringlist.h> |
|
45 #include <QtCore/qvector.h> |
44 #include <QtScript/qscriptstring.h> |
46 #include <QtScript/qscriptstring.h> |
45 #include <QtScript/qscriptengine.h> |
47 #include <QtScript/qscriptengine.h> |
46 #include <QtScript/qscriptvalueiterator.h> |
48 #include <QtScript/qscriptvalueiterator.h> |
|
49 |
|
50 #include <private/qscriptdeclarativeclass_p.h> |
47 |
51 |
48 QT_BEGIN_NAMESPACE |
52 QT_BEGIN_NAMESPACE |
49 |
53 |
50 /* |
54 /* |
51 Used to prevent any writes to the global object. |
55 Used to prevent any writes to the global object. |
52 */ |
56 */ |
53 QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(QScriptEngine *engine) |
57 QDeclarativeGlobalScriptClass::QDeclarativeGlobalScriptClass(QScriptEngine *engine) |
54 : QScriptClass(engine) |
58 : QScriptClass(engine) |
55 { |
59 { |
56 QString eval = QLatin1String("eval"); |
60 QString eval = QLatin1String("eval"); |
|
61 QString version = QLatin1String("version"); |
57 |
62 |
58 QScriptValue globalObject = engine->globalObject(); |
63 QScriptValue originalGlobalObject = engine->globalObject(); |
59 |
64 |
60 m_globalObject = engine->newObject(); |
|
61 QScriptValue newGlobalObject = engine->newObject(); |
65 QScriptValue newGlobalObject = engine->newObject(); |
62 |
66 |
63 QScriptValueIterator iter(globalObject); |
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(); |
64 |
74 |
65 while (iter.hasNext()) { |
75 QString name = iter.name(); |
66 iter.next(); |
|
67 |
76 |
68 QString name = iter.name(); |
77 if (name == version) |
|
78 continue; |
69 |
79 |
70 if (name != eval) |
80 if (name != eval) { |
71 m_globalObject.setProperty(iter.scriptName(), iter.value()); |
81 names.append(name); |
72 newGlobalObject.setProperty(iter.scriptName(), iter.value()); |
82 values.append(iter.value()); |
|
83 flags.append(iter.flags() | QScriptValue::Undeletable); |
|
84 } |
|
85 newGlobalObject.setProperty(iter.scriptName(), iter.value()); |
73 |
86 |
74 m_illegalNames.insert(name); |
87 m_illegalNames.insert(name); |
|
88 } |
|
89 m_staticGlobalObject = QScriptDeclarativeClass::newStaticScopeObject( |
|
90 engine, names.size(), names.constData(), values.constData(), flags.constData()); |
75 } |
91 } |
76 |
92 |
77 newGlobalObject.setScriptClass(this); |
93 newGlobalObject.setScriptClass(this); |
78 engine->setGlobalObject(newGlobalObject); |
94 engine->setGlobalObject(newGlobalObject); |
79 } |
95 } |
85 { |
101 { |
86 Q_UNUSED(object); |
102 Q_UNUSED(object); |
87 Q_UNUSED(name); |
103 Q_UNUSED(name); |
88 Q_UNUSED(flags); |
104 Q_UNUSED(flags); |
89 Q_UNUSED(id); |
105 Q_UNUSED(id); |
90 return HandlesReadAccess | HandlesWriteAccess; |
106 return 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 } |
107 } |
103 |
108 |
104 void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object, |
109 void QDeclarativeGlobalScriptClass::setProperty(QScriptValue &object, |
105 const QScriptString &name, |
110 const QScriptString &name, |
106 uint id, const QScriptValue &value) |
111 uint id, const QScriptValue &value) |
112 name.toString() + QLatin1Char('\"'); |
117 name.toString() + QLatin1Char('\"'); |
113 engine()->currentContext()->throwError(error); |
118 engine()->currentContext()->throwError(error); |
114 } |
119 } |
115 |
120 |
116 /* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */ |
121 /* This method is for the use of tst_qdeclarativeecmascript::callQtInvokables() only */ |
117 void QDeclarativeGlobalScriptClass::explicitSetProperty(const QString &name, const QScriptValue &value) |
122 void QDeclarativeGlobalScriptClass::explicitSetProperty(const QStringList &names, const QList<QScriptValue> &values) |
118 { |
123 { |
|
124 Q_ASSERT(names.count() == values.count()); |
119 QScriptValue globalObject = engine()->globalObject(); |
125 QScriptValue globalObject = engine()->globalObject(); |
120 |
126 |
121 QScriptValue v = engine()->newObject(); |
127 QScriptValue v = engine()->newObject(); |
122 |
128 |
123 QScriptValueIterator iter(v); |
129 QScriptValueIterator iter(v); |
124 while (iter.hasNext()) { |
130 while (iter.hasNext()) { |
125 iter.next(); |
131 iter.next(); |
126 v.setProperty(iter.scriptName(), iter.value()); |
132 v.setProperty(iter.scriptName(), iter.value()); |
127 } |
133 } |
128 |
134 |
129 v.setProperty(name, 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); |
|
139 } |
|
140 |
130 v.setScriptClass(this); |
141 v.setScriptClass(this); |
131 |
142 |
132 engine()->setGlobalObject(v); |
143 engine()->setGlobalObject(v); |
133 } |
144 } |
134 |
145 |