Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/jsruntime/qv4include.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtQml 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 The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qv4include_p.h" | - | ||||||||||||
41 | #include "qv4scopedvalue_p.h" | - | ||||||||||||
42 | #include "qv4jscall_p.h" | - | ||||||||||||
43 | - | |||||||||||||
44 | #include <QtQml/qjsengine.h> | - | ||||||||||||
45 | #if QT_CONFIG(qml_network) | - | ||||||||||||
46 | #include <QtNetwork/qnetworkrequest.h> | - | ||||||||||||
47 | #include <QtNetwork/qnetworkreply.h> | - | ||||||||||||
48 | #endif | - | ||||||||||||
49 | #include <QtCore/qfile.h> | - | ||||||||||||
50 | #include <QtQml/qqmlfile.h> | - | ||||||||||||
51 | - | |||||||||||||
52 | #include <private/qqmlengine_p.h> | - | ||||||||||||
53 | #include <private/qv4engine_p.h> | - | ||||||||||||
54 | #include <private/qv4functionobject_p.h> | - | ||||||||||||
55 | #include <private/qv4script_p.h> | - | ||||||||||||
56 | #include <private/qv4context_p.h> | - | ||||||||||||
57 | - | |||||||||||||
58 | QT_BEGIN_NAMESPACE | - | ||||||||||||
59 | - | |||||||||||||
60 | QV4Include::QV4Include(const QUrl &url, QV4::ExecutionEngine *engine, | - | ||||||||||||
61 | QV4::QmlContext *qmlContext, const QV4::Value &callback) | - | ||||||||||||
62 | : v4(engine), m_url(url) | - | ||||||||||||
63 | #if QT_CONFIG(qml_network) | - | ||||||||||||
64 | , m_redirectCount(0), m_network(nullptr) , m_reply(nullptr) | - | ||||||||||||
65 | #endif | - | ||||||||||||
66 | { | - | ||||||||||||
67 | if (qmlContext)
| 0-6 | ||||||||||||
68 | m_qmlContext.set(engine, *qmlContext); executed 6 times by 1 test: m_qmlContext.set(engine, *qmlContext); Executed by:
| 6 | ||||||||||||
69 | if (callback.as<QV4::FunctionObject>())
| 0-6 | ||||||||||||
70 | m_callbackFunction.set(engine, callback); executed 6 times by 1 test: m_callbackFunction.set(engine, callback); Executed by:
| 6 | ||||||||||||
71 | - | |||||||||||||
72 | m_resultObject.set(v4, resultValue(v4)); | - | ||||||||||||
73 | - | |||||||||||||
74 | #if QT_CONFIG(qml_network) | - | ||||||||||||
75 | m_network = engine->v8Engine->networkAccessManager(); | - | ||||||||||||
76 | - | |||||||||||||
77 | QNetworkRequest request; | - | ||||||||||||
78 | request.setUrl(url); | - | ||||||||||||
79 | - | |||||||||||||
80 | m_reply = m_network->get(request); | - | ||||||||||||
81 | QObject::connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); | - | ||||||||||||
82 | #else | - | ||||||||||||
83 | finished(); | - | ||||||||||||
84 | #endif | - | ||||||||||||
85 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
86 | - | |||||||||||||
87 | QV4Include::~QV4Include() | - | ||||||||||||
88 | { | - | ||||||||||||
89 | #if QT_CONFIG(qml_network) | - | ||||||||||||
90 | delete m_reply; | - | ||||||||||||
91 | m_reply = nullptr; | - | ||||||||||||
92 | #endif | - | ||||||||||||
93 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
94 | - | |||||||||||||
95 | QV4::ReturnedValue QV4Include::resultValue(QV4::ExecutionEngine *v4, Status status, | - | ||||||||||||
96 | const QString &statusText) | - | ||||||||||||
97 | { | - | ||||||||||||
98 | QV4::Scope scope(v4); | - | ||||||||||||
99 | - | |||||||||||||
100 | // XXX It seems inefficient to create this object from scratch each time. | - | ||||||||||||
101 | QV4::ScopedObject o(scope, v4->newObject()); | - | ||||||||||||
102 | QV4::ScopedString s(scope); | - | ||||||||||||
103 | QV4::ScopedValue v(scope); | - | ||||||||||||
104 | o->put((s = v4->newString(QStringLiteral("OK"))), (v = QV4::Primitive::fromInt32(Ok))); executed 30 times by 3 tests: return qstring_literal_temp; Executed by:
| 30 | ||||||||||||
105 | o->put((s = v4->newString(QStringLiteral("LOADING"))), (v = QV4::Primitive::fromInt32(Loading))); executed 30 times by 3 tests: return qstring_literal_temp; Executed by:
| 30 | ||||||||||||
106 | o->put((s = v4->newString(QStringLiteral("NETWORK_ERROR"))), (v = QV4::Primitive::fromInt32(NetworkError))); executed 30 times by 3 tests: return qstring_literal_temp; Executed by:
| 30 | ||||||||||||
107 | o->put((s = v4->newString(QStringLiteral("EXCEPTION"))), (v = QV4::Primitive::fromInt32(Exception))); executed 30 times by 3 tests: return qstring_literal_temp; Executed by:
| 30 | ||||||||||||
108 | o->put((s = v4->newString(QStringLiteral("status"))), (v = QV4::Primitive::fromInt32(status))); executed 30 times by 3 tests: return qstring_literal_temp; Executed by:
| 30 | ||||||||||||
109 | if (!statusText.isEmpty())
| 2-28 | ||||||||||||
110 | o->put((s = v4->newString(QStringLiteral("statusText"))), (v = v4->newString(statusText))); executed 2 times by 1 test: o->put((s = v4->newString(([]() noexcept -> QString { enum { Size = sizeof(u"" "statusText")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "statusText" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()))), (v = v4->newString(statusText))); Executed by:
executed 2 times by 1 test: return qstring_literal_temp; Executed by:
| 2 | ||||||||||||
111 | - | |||||||||||||
112 | return o.asReturnedValue(); executed 30 times by 3 tests: return o.asReturnedValue(); Executed by:
| 30 | ||||||||||||
113 | } | - | ||||||||||||
114 | - | |||||||||||||
115 | void QV4Include::callback(const QV4::Value &callback, const QV4::Value &status) | - | ||||||||||||
116 | { | - | ||||||||||||
117 | if (!callback.isObject())
| 12-18 | ||||||||||||
118 | return; executed 18 times by 3 tests: return; Executed by:
| 18 | ||||||||||||
119 | QV4::ExecutionEngine *v4 = callback.as<QV4::Object>()->engine(); | - | ||||||||||||
120 | QV4::Scope scope(v4); | - | ||||||||||||
121 | QV4::ScopedFunctionObject f(scope, callback); | - | ||||||||||||
122 | if (!f)
| 0-12 | ||||||||||||
123 | return; never executed: return; | 0 | ||||||||||||
124 | - | |||||||||||||
125 | QV4::JSCallData jsCallData(scope, 1); | - | ||||||||||||
126 | *jsCallData->thisObject = v4->globalObject->asReturnedValue(); | - | ||||||||||||
127 | jsCallData->args[0] = status; | - | ||||||||||||
128 | f->call(jsCallData); | - | ||||||||||||
129 | if (scope.hasException())
| 0-12 | ||||||||||||
130 | scope.engine->catchException(); never executed: scope.engine->catchException(); | 0 | ||||||||||||
131 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||
132 | - | |||||||||||||
133 | QV4::ReturnedValue QV4Include::result() | - | ||||||||||||
134 | { | - | ||||||||||||
135 | return m_resultObject.value(); executed 6 times by 1 test: return m_resultObject.value(); Executed by:
| 6 | ||||||||||||
136 | } | - | ||||||||||||
137 | - | |||||||||||||
138 | #define INCLUDE_MAXIMUM_REDIRECT_RECURSION 15 | - | ||||||||||||
139 | void QV4Include::finished() | - | ||||||||||||
140 | { | - | ||||||||||||
141 | #if QT_CONFIG(qml_network) | - | ||||||||||||
142 | m_redirectCount++; | - | ||||||||||||
143 | - | |||||||||||||
144 | if (m_redirectCount < INCLUDE_MAXIMUM_REDIRECT_RECURSION) {
| 0-6 | ||||||||||||
145 | QVariant redirect = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute); | - | ||||||||||||
146 | if (redirect.isValid()) {
| 0-6 | ||||||||||||
147 | m_url = m_url.resolved(redirect.toUrl()); | - | ||||||||||||
148 | delete m_reply; | - | ||||||||||||
149 | - | |||||||||||||
150 | QNetworkRequest request; | - | ||||||||||||
151 | request.setUrl(m_url); | - | ||||||||||||
152 | - | |||||||||||||
153 | m_reply = m_network->get(request); | - | ||||||||||||
154 | QObject::connect(m_reply, SIGNAL(finished()), this, SLOT(finished())); | - | ||||||||||||
155 | return; never executed: return; | 0 | ||||||||||||
156 | } | - | ||||||||||||
157 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
158 | - | |||||||||||||
159 | QV4::Scope scope(v4); | - | ||||||||||||
160 | QV4::ScopedObject resultObj(scope, m_resultObject.value()); | - | ||||||||||||
161 | QV4::ScopedString status(scope, v4->newString(QStringLiteral("status"))); executed 6 times by 1 test: return qstring_literal_temp; Executed by:
| 6 | ||||||||||||
162 | if (m_reply->error() == QNetworkReply::NoError) {
| 2-4 | ||||||||||||
163 | QByteArray data = m_reply->readAll(); | - | ||||||||||||
164 | - | |||||||||||||
165 | QString code = QString::fromUtf8(data); | - | ||||||||||||
166 | QmlIR::Document::removeScriptPragmas(code); | - | ||||||||||||
167 | - | |||||||||||||
168 | QV4::Scoped<QV4::QmlContext> qml(scope, m_qmlContext.value()); | - | ||||||||||||
169 | QV4::Script script(v4, qml, code, m_url.toString()); | - | ||||||||||||
170 | - | |||||||||||||
171 | script.parse(); | - | ||||||||||||
172 | if (!scope.engine->hasException)
| 0-4 | ||||||||||||
173 | script.run(); executed 4 times by 1 test: script.run(); Executed by:
| 4 | ||||||||||||
174 | if (scope.engine->hasException) {
| 2 | ||||||||||||
175 | QV4::ScopedValue ex(scope, scope.engine->catchException()); | - | ||||||||||||
176 | resultObj->put(status, QV4::ScopedValue(scope, QV4::Primitive::fromInt32(Exception))); | - | ||||||||||||
177 | QV4::ScopedString exception(scope, v4->newString(QStringLiteral("exception"))); executed 2 times by 1 test: return qstring_literal_temp; Executed by:
| 2 | ||||||||||||
178 | resultObj->put(exception, ex); | - | ||||||||||||
179 | } else { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
180 | resultObj->put(status, QV4::ScopedValue(scope, QV4::Primitive::fromInt32(Ok))); | - | ||||||||||||
181 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
182 | } else { | - | ||||||||||||
183 | resultObj->put(status, QV4::ScopedValue(scope, QV4::Primitive::fromInt32(NetworkError))); | - | ||||||||||||
184 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
185 | #else | - | ||||||||||||
186 | QV4::Scope scope(v4); | - | ||||||||||||
187 | QV4::ScopedObject resultObj(scope, m_resultObject.value()); | - | ||||||||||||
188 | QV4::ScopedString status(scope, v4->newString(QStringLiteral("status"))); | - | ||||||||||||
189 | resultObj->put(status, QV4::ScopedValue(scope, QV4::Primitive::fromInt32(NetworkError))); | - | ||||||||||||
190 | #endif // qml_network | - | ||||||||||||
191 | - | |||||||||||||
192 | QV4::ScopedValue cb(scope, m_callbackFunction.value()); | - | ||||||||||||
193 | callback(cb, resultObj); | - | ||||||||||||
194 | - | |||||||||||||
195 | disconnect(); | - | ||||||||||||
196 | deleteLater(); | - | ||||||||||||
197 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
198 | - | |||||||||||||
199 | /* | - | ||||||||||||
200 | Documented in qv8engine.cpp | - | ||||||||||||
201 | */ | - | ||||||||||||
202 | QV4::ReturnedValue QV4Include::method_include(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *argv, int argc) | - | ||||||||||||
203 | { | - | ||||||||||||
204 | QV4::Scope scope(b); | - | ||||||||||||
205 | if (!argc)
| 0-30 | ||||||||||||
206 | RETURN_UNDEFINED(); never executed: return QV4::Encode::undefined(); | 0 | ||||||||||||
207 | - | |||||||||||||
208 | QQmlContextData *context = scope.engine->callingQmlContext(); | - | ||||||||||||
209 | - | |||||||||||||
210 | if (!context || !context->isJSContext)
| 0-30 | ||||||||||||
211 | RETURN_RESULT(scope.engine->throwError(QString::fromUtf8("Qt.include(): Can only be called from JavaScript files"))); never executed: return QV4::Encode(scope.engine->throwError(QString::fromUtf8("Qt.include(): Can only be called from JavaScript files"))); | 0 | ||||||||||||
212 | - | |||||||||||||
213 | QV4::ScopedValue callbackFunction(scope, QV4::Primitive::undefinedValue()); | - | ||||||||||||
214 | if (argc >= 2 && argv[1].as<QV4::FunctionObject>())
| 0-18 | ||||||||||||
215 | callbackFunction = argv[1]; executed 12 times by 1 test: callbackFunction = argv[1]; Executed by:
| 12 | ||||||||||||
216 | - | |||||||||||||
217 | #if QT_CONFIG(qml_network) | - | ||||||||||||
218 | QUrl url(scope.engine->resolvedUrl(argv[0].toQStringNoThrow())); | - | ||||||||||||
219 | if (scope.engine->qmlEngine() && scope.engine->qmlEngine()->urlInterceptor())
| 0-28 | ||||||||||||
220 | url = scope.engine->qmlEngine()->urlInterceptor()->intercept(url, QQmlAbstractUrlInterceptor::JavaScriptFile); never executed: url = scope.engine->qmlEngine()->urlInterceptor()->intercept(url, QQmlAbstractUrlInterceptor::JavaScriptFile); | 0 | ||||||||||||
221 | - | |||||||||||||
222 | QString localFile = QQmlFile::urlToLocalFileOrQrc(url); | - | ||||||||||||
223 | - | |||||||||||||
224 | QV4::ScopedValue result(scope); | - | ||||||||||||
225 | QV4::Scoped<QV4::QmlContext> qmlcontext(scope, scope.engine->qmlContext()); | - | ||||||||||||
226 | - | |||||||||||||
227 | if (localFile.isEmpty()) {
| 6-24 | ||||||||||||
228 | QV4Include *i = new QV4Include(url, scope.engine, qmlcontext, callbackFunction); | - | ||||||||||||
229 | result = i->result(); | - | ||||||||||||
230 | - | |||||||||||||
231 | } else { executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
232 | QScopedPointer<QV4::Script> script; | - | ||||||||||||
233 | QString error; | - | ||||||||||||
234 | script.reset(QV4::Script::createFromFileOrCache(scope.engine, qmlcontext, localFile, url, &error)); | - | ||||||||||||
235 | - | |||||||||||||
236 | if (!script.isNull()) {
| 2-22 | ||||||||||||
237 | script->parse(); | - | ||||||||||||
238 | if (!scope.engine->hasException)
| 0-22 | ||||||||||||
239 | script->run(); executed 22 times by 3 tests: script->run(); Executed by:
| 22 | ||||||||||||
240 | if (scope.engine->hasException) {
| 2-20 | ||||||||||||
241 | QV4::ScopedValue ex(scope, scope.engine->catchException()); | - | ||||||||||||
242 | result = resultValue(scope.engine, Exception); | - | ||||||||||||
243 | QV4::ScopedString exception(scope, scope.engine->newString(QStringLiteral("exception"))); executed 2 times by 1 test: return qstring_literal_temp; Executed by:
| 2 | ||||||||||||
244 | result->as<QV4::Object>()->put(exception, ex); | - | ||||||||||||
245 | } else { executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
246 | result = resultValue(scope.engine, Ok); | - | ||||||||||||
247 | } executed 20 times by 3 tests: end of block Executed by:
| 20 | ||||||||||||
248 | } else { | - | ||||||||||||
249 | result = resultValue(scope.engine, NetworkError, error); | - | ||||||||||||
250 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
251 | - | |||||||||||||
252 | callback(callbackFunction, result); | - | ||||||||||||
253 | } executed 24 times by 3 tests: end of block Executed by:
| 24 | ||||||||||||
254 | - | |||||||||||||
255 | #else | - | ||||||||||||
256 | QV4::ScopedValue result(scope); | - | ||||||||||||
257 | result = resultValue(scope.engine, NetworkError); | - | ||||||||||||
258 | callback(callbackFunction, result); | - | ||||||||||||
259 | #endif | - | ||||||||||||
260 | - | |||||||||||||
261 | return result->asReturnedValue(); executed 30 times by 3 tests: return result->asReturnedValue(); Executed by:
| 30 | ||||||||||||
262 | } | - | ||||||||||||
263 | - | |||||||||||||
264 | QT_END_NAMESPACE | - | ||||||||||||
265 | - | |||||||||||||
266 | #include "moc_qv4include_p.cpp" | - | ||||||||||||
Source code | Switch to Preprocessed file |