Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/jsruntime/qv4script.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 "qv4script_p.h" | - | ||||||
41 | #include <private/qv4mm_p.h> | - | ||||||
42 | #include "qv4functionobject_p.h" | - | ||||||
43 | #include "qv4function_p.h" | - | ||||||
44 | #include "qv4context_p.h" | - | ||||||
45 | #include "qv4debugging_p.h" | - | ||||||
46 | #include "qv4profiling_p.h" | - | ||||||
47 | #include "qv4scopedvalue_p.h" | - | ||||||
48 | #include "qv4jscall_p.h" | - | ||||||
49 | - | |||||||
50 | #include <private/qqmljsengine_p.h> | - | ||||||
51 | #include <private/qqmljslexer_p.h> | - | ||||||
52 | #include <private/qqmljsparser_p.h> | - | ||||||
53 | #include <private/qqmljsast_p.h> | - | ||||||
54 | #include <private/qqmlengine_p.h> | - | ||||||
55 | #include <private/qv4profiling_p.h> | - | ||||||
56 | #include <qv4runtimecodegen_p.h> | - | ||||||
57 | - | |||||||
58 | #include <QtCore/QDebug> | - | ||||||
59 | #include <QtCore/QString> | - | ||||||
60 | - | |||||||
61 | using namespace QV4; | - | ||||||
62 | - | |||||||
63 | Script::Script(ExecutionEngine *v4, QmlContext *qml, const QQmlRefPointer<CompiledData::CompilationUnit> &compilationUnit) | - | ||||||
64 | : line(1), column(0), context(v4->rootContext()), strictMode(false), inheritContext(true), parsed(false) | - | ||||||
65 | , compilationUnit(compilationUnit), vmFunction(nullptr), parseAsBinding(true) | - | ||||||
66 | { | - | ||||||
67 | if (qml)
| 2-250 | ||||||
68 | qmlContext.set(v4, *qml); executed 2 times by 1 test: qmlContext.set(v4, *qml); Executed by:
| 2 | ||||||
69 | - | |||||||
70 | parsed = true; | - | ||||||
71 | - | |||||||
72 | vmFunction = compilationUnit ? compilationUnit->linkToEngine(v4) : nullptr;
| 0-252 | ||||||
73 | } executed 252 times by 13 tests: end of block Executed by:
| 252 | ||||||
74 | - | |||||||
75 | Script::~Script() | - | ||||||
76 | { | - | ||||||
77 | } | - | ||||||
78 | - | |||||||
79 | void Script::parse() | - | ||||||
80 | { | - | ||||||
81 | if (parsed)
| 22-2818282 | ||||||
82 | return; executed 22 times by 3 tests: return; Executed by:
| 22 | ||||||
83 | - | |||||||
84 | using namespace QV4::Compiler; | - | ||||||
85 | - | |||||||
86 | parsed = true; | - | ||||||
87 | - | |||||||
88 | ExecutionEngine *v4 = context->engine(); | - | ||||||
89 | Scope valueScope(v4); | - | ||||||
90 | - | |||||||
91 | Module module(v4->debugger() != nullptr); | - | ||||||
92 | - | |||||||
93 | if (sourceCode.startsWith(QLatin1String("function("))) {
| 0-2817831 | ||||||
94 | static const int snippetLength = 70; | - | ||||||
95 | qWarning() << "Warning: Using function expressions as statements in scripts is not compliant with the ECMAScript specification:\n" | - | ||||||
96 | << (sourceCode.leftRef(snippetLength) + QLatin1String("...")) | - | ||||||
97 | << "\nThis will throw a syntax error in Qt 5.12. If you want a function expression, surround it by parentheses."; | - | ||||||
98 | } never executed: end of block | 0 | ||||||
99 | - | |||||||
100 | Engine ee, *engine = ⅇ | - | ||||||
101 | Lexer lexer(engine); | - | ||||||
102 | lexer.setCode(sourceCode, line, parseAsBinding); | - | ||||||
103 | Parser parser(engine); | - | ||||||
104 | - | |||||||
105 | const bool parsed = parser.parseProgram(); | - | ||||||
106 | - | |||||||
107 | const auto diagnosticMessages = parser.diagnosticMessages(); | - | ||||||
108 | for (const DiagnosticMessage &m : diagnosticMessages) { | - | ||||||
109 | if (m.isError()) {
| 0-4804 | ||||||
110 | valueScope.engine->throwSyntaxError(m.message, sourceFile, m.loc.startLine, m.loc.startColumn); | - | ||||||
111 | return; executed 4782 times by 7 tests: return; Executed by:
| 4782 | ||||||
112 | } else { | - | ||||||
113 | qWarning() << sourceFile << ':' << m.loc.startLine << ':' << m.loc.startColumn | - | ||||||
114 | << ": warning: " << m.message; | - | ||||||
115 | } never executed: end of block | 0 | ||||||
116 | } | - | ||||||
117 | - | |||||||
118 | if (parsed) {
| 316-2813723 | ||||||
119 | using namespace AST; | - | ||||||
120 | Program *program = AST::cast<Program *>(parser.rootNode()); | - | ||||||
121 | if (!program) {
| 523002-2288635 | ||||||
122 | // if parsing was successful, and we have no program, then | - | ||||||
123 | // we're done...: | - | ||||||
124 | return; executed 522737 times by 3 tests: return; Executed by:
| 522737 | ||||||
125 | } | - | ||||||
126 | - | |||||||
127 | QV4::Compiler::JSUnitGenerator jsGenerator(&module); | - | ||||||
128 | RuntimeCodegen cg(v4, &jsGenerator, strictMode); | - | ||||||
129 | if (inheritContext)
| 611236-1677898 | ||||||
130 | cg.setUseFastLookups(false); executed 1677898 times by 29 tests: cg.setUseFastLookups(false); Executed by:
| 1677898 | ||||||
131 | cg.generateFromProgram(sourceFile, sourceFile, sourceCode, program, &module, contextType); | - | ||||||
132 | if (v4->hasException)
| 1212-2286334 | ||||||
133 | return; executed 1205 times by 3 tests: return; Executed by:
| 1205 | ||||||
134 | - | |||||||
135 | compilationUnit = cg.generateCompilationUnit(); | - | ||||||
136 | vmFunction = compilationUnit->linkToEngine(v4); | - | ||||||
137 | } executed 2288281 times by 31 tests: end of block Executed by:
| 2288281 | ||||||
138 | - | |||||||
139 | if (!vmFunction) {
| 316-2288254 | ||||||
140 | // ### FIX file/line number | - | ||||||
141 | ScopedObject error(valueScope, v4->newSyntaxErrorObject(QStringLiteral("Syntax error"))); executed 316 times by 1 test: return qstring_literal_temp; Executed by:
| 316 | ||||||
142 | v4->throwError(error); | - | ||||||
143 | } executed 317 times by 1 test: end of block Executed by:
| 317 | ||||||
144 | } executed 2288656 times by 31 tests: end of block Executed by:
| 2288656 | ||||||
145 | - | |||||||
146 | ReturnedValue Script::run(const QV4::Value *thisObject) | - | ||||||
147 | { | - | ||||||
148 | if (!parsed)
| 2374-118459 | ||||||
149 | parse(); executed 2374 times by 5 tests: parse(); Executed by:
| 2374 | ||||||
150 | if (!vmFunction)
| 12-121381 | ||||||
151 | return Encode::undefined(); executed 12 times by 2 tests: return Encode::undefined(); Executed by:
| 12 | ||||||
152 | - | |||||||
153 | QV4::ExecutionEngine *engine = context->engine(); | - | ||||||
154 | QV4::Scope valueScope(engine); | - | ||||||
155 | - | |||||||
156 | if (qmlContext.isUndefined()) {
| 1498-119455 | ||||||
157 | TemporaryAssignment<Function*> savedGlobalCode(engine->globalCode, vmFunction); | - | ||||||
158 | - | |||||||
159 | return vmFunction->call(thisObject ? thisObject : engine->globalObject, nullptr, 0, executed 119728 times by 14 tests: return vmFunction->call(thisObject ? thisObject : engine->globalObject, nullptr, 0, context); Executed by:
| 119728 | ||||||
160 | context); executed 119728 times by 14 tests: return vmFunction->call(thisObject ? thisObject : engine->globalObject, nullptr, 0, context); Executed by:
| 119728 | ||||||
161 | } else { | - | ||||||
162 | Scoped<QmlContext> qml(valueScope, qmlContext.value()); | - | ||||||
163 | return vmFunction->call(thisObject, nullptr, 0, qml); executed 1498 times by 17 tests: return vmFunction->call(thisObject, nullptr, 0, qml); Executed by:
| 1498 | ||||||
164 | } | - | ||||||
165 | } | - | ||||||
166 | - | |||||||
167 | Function *Script::function() | - | ||||||
168 | { | - | ||||||
169 | if (!parsed)
| 0-1577457 | ||||||
170 | parse(); never executed: parse(); | 0 | ||||||
171 | return vmFunction; executed 1577049 times by 7 tests: return vmFunction; Executed by:
| 1577049 | ||||||
172 | } | - | ||||||
173 | - | |||||||
174 | QQmlRefPointer<QV4::CompiledData::CompilationUnit> Script::precompile(QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine, Compiler::JSUnitGenerator *unitGenerator, | - | ||||||
175 | const QString &fileName, const QString &finalUrl, const QString &source, | - | ||||||
176 | QList<QQmlError> *reportedErrors) | - | ||||||
177 | { | - | ||||||
178 | using namespace QV4::Compiler; | - | ||||||
179 | using namespace QQmlJS::AST; | - | ||||||
180 | - | |||||||
181 | Lexer lexer(jsEngine); | - | ||||||
182 | lexer.setCode(source, /*line*/1, /*qml mode*/false); | - | ||||||
183 | Parser parser(jsEngine); | - | ||||||
184 | - | |||||||
185 | parser.parseProgram(); | - | ||||||
186 | - | |||||||
187 | QList<QQmlError> errors; | - | ||||||
188 | - | |||||||
189 | const auto diagnosticMessages = parser.diagnosticMessages(); | - | ||||||
190 | for (const DiagnosticMessage &m : diagnosticMessages) { | - | ||||||
191 | if (m.isWarning()) {
| 0-26 | ||||||
192 | qWarning("%s:%d : %s", qPrintable(fileName), m.loc.startLine, qPrintable(m.message)); | - | ||||||
193 | continue; never executed: continue; | 0 | ||||||
194 | } | - | ||||||
195 | - | |||||||
196 | QQmlError error; | - | ||||||
197 | error.setUrl(QUrl(fileName)); | - | ||||||
198 | error.setDescription(m.message); | - | ||||||
199 | error.setLine(m.loc.startLine); | - | ||||||
200 | error.setColumn(m.loc.startColumn); | - | ||||||
201 | errors << error; | - | ||||||
202 | } executed 26 times by 1 test: end of block Executed by:
| 26 | ||||||
203 | - | |||||||
204 | if (!errors.isEmpty()) {
| 26-90 | ||||||
205 | if (reportedErrors)
| 0-26 | ||||||
206 | *reportedErrors << errors; executed 26 times by 1 test: *reportedErrors << errors; Executed by:
| 26 | ||||||
207 | return nullptr; executed 26 times by 1 test: return nullptr; Executed by:
| 26 | ||||||
208 | } | - | ||||||
209 | - | |||||||
210 | Program *program = AST::cast<Program *>(parser.rootNode()); | - | ||||||
211 | if (!program) {
| 0-90 | ||||||
212 | // if parsing was successful, and we have no program, then | - | ||||||
213 | // we're done...: | - | ||||||
214 | return nullptr; never executed: return nullptr; | 0 | ||||||
215 | } | - | ||||||
216 | - | |||||||
217 | Codegen cg(unitGenerator, /*strict mode*/false); | - | ||||||
218 | cg.setUseFastLookups(false); | - | ||||||
219 | cg.generateFromProgram(fileName, finalUrl, source, program, module, ContextType::Global); | - | ||||||
220 | errors = cg.qmlErrors(); | - | ||||||
221 | if (!errors.isEmpty()) {
| 0-90 | ||||||
222 | if (reportedErrors)
| 0 | ||||||
223 | *reportedErrors << errors; never executed: *reportedErrors << errors; | 0 | ||||||
224 | return nullptr; never executed: return nullptr; | 0 | ||||||
225 | } | - | ||||||
226 | - | |||||||
227 | return cg.generateCompilationUnit(/*generate unit data*/false); executed 90 times by 12 tests: return cg.generateCompilationUnit( false); Executed by:
| 90 | ||||||
228 | } | - | ||||||
229 | - | |||||||
230 | Script *Script::createFromFileOrCache(ExecutionEngine *engine, QmlContext *qmlContext, const QString &fileName, const QUrl &originalUrl, QString *error) | - | ||||||
231 | { | - | ||||||
232 | if (error)
| 0-1178 | ||||||
233 | error->clear(); executed 1178 times by 5 tests: error->clear(); Executed by:
| 1178 | ||||||
234 | - | |||||||
235 | QQmlMetaType::CachedUnitLookupError cacheError = QQmlMetaType::CachedUnitLookupError::NoError; | - | ||||||
236 | if (const QV4::CompiledData::Unit *cachedUnit = QQmlMetaType::findCachedCompilationUnit(originalUrl, &cacheError)) {
| 2-1176 | ||||||
237 | QQmlRefPointer<QV4::CompiledData::CompilationUnit> jsUnit; | - | ||||||
238 | jsUnit.adopt(new QV4::CompiledData::CompilationUnit(cachedUnit)); | - | ||||||
239 | return new QV4::Script(engine, qmlContext, jsUnit); executed 2 times by 1 test: return new QV4::Script(engine, qmlContext, jsUnit); Executed by:
| 2 | ||||||
240 | } | - | ||||||
241 | - | |||||||
242 | QFile f(fileName); | - | ||||||
243 | if (!f.open(QIODevice::ReadOnly)) {
| 2-1174 | ||||||
244 | if (error) {
| 0-2 | ||||||
245 | if (cacheError == QQmlMetaType::CachedUnitLookupError::VersionMismatch)
| 0-2 | ||||||
246 | *error = originalUrl.toString() + QString::fromUtf8(" was compiled ahead of time with an incompatible version of Qt and the original source code cannot be found. Please recompile"); never executed: *error = originalUrl.toString() + QString::fromUtf8(" was compiled ahead of time with an incompatible version of Qt and the original source code cannot be found. Please recompile"); | 0 | ||||||
247 | else | - | ||||||
248 | *error = QString::fromUtf8("Error opening source file %1: %2").arg(originalUrl.toString()).arg(f.errorString()); executed 2 times by 1 test: *error = QString::fromUtf8("Error opening source file %1: %2").arg(originalUrl.toString()).arg(f.errorString()); Executed by:
| 2 | ||||||
249 | } | - | ||||||
250 | return nullptr; executed 2 times by 1 test: return nullptr; Executed by:
| 2 | ||||||
251 | } | - | ||||||
252 | - | |||||||
253 | QByteArray data = f.readAll(); | - | ||||||
254 | QString sourceCode = QString::fromUtf8(data); | - | ||||||
255 | QmlIR::Document::removeScriptPragmas(sourceCode); | - | ||||||
256 | - | |||||||
257 | auto result = new QV4::Script(engine, qmlContext, sourceCode, originalUrl.toString()); | - | ||||||
258 | result->parse(); | - | ||||||
259 | return result; executed 1174 times by 4 tests: return result; Executed by:
| 1174 | ||||||
260 | } | - | ||||||
261 | - | |||||||
262 | QV4::ReturnedValue Script::evaluate(ExecutionEngine *engine, const QString &script, QmlContext *qmlContext) | - | ||||||
263 | { | - | ||||||
264 | QV4::Scope scope(engine); | - | ||||||
265 | QV4::Script qmlScript(engine, qmlContext, script, QString()); | - | ||||||
266 | - | |||||||
267 | qmlScript.parse(); | - | ||||||
268 | QV4::ScopedValue result(scope); | - | ||||||
269 | if (!scope.engine->hasException)
| 0 | ||||||
270 | result = qmlScript.run(); never executed: result = qmlScript.run(); | 0 | ||||||
271 | if (scope.engine->hasException) {
| 0 | ||||||
272 | scope.engine->catchException(); | - | ||||||
273 | return Encode::undefined(); never executed: return Encode::undefined(); | 0 | ||||||
274 | } | - | ||||||
275 | return result->asReturnedValue(); never executed: return result->asReturnedValue(); | 0 | ||||||
276 | } | - | ||||||
Source code | Switch to Preprocessed file |