Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4script.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#include "qv4script_p.h"
6
7#include <private/qqmlengine_p.h>
8#include <private/qqmljsast_p.h>
9#include <private/qqmljsengine_p.h>
10#include <private/qqmljslexer_p.h>
11#include <private/qqmljsparser_p.h>
12#include <private/qqmlsourcecoordinate_p.h>
13#include <private/qv4context_p.h>
14#include <private/qv4function_p.h>
15#include <private/qv4mm_p.h>
16#include <private/qv4profiling_p.h>
17#include <private/qv4runtimecodegen_p.h>
18#include <private/qv4scopedvalue_p.h>
19
20#include <QtCore/qdebug.h>
21#include <QtCore/qscopedvaluerollback.h>
22#include <QtCore/qstring.h>
23
25
26using namespace QV4;
27using namespace QQmlJS;
28
29Script::Script(
30 ExecutionEngine *v4, QmlContext *qml,
31 const QQmlRefPointer<ExecutableCompilationUnit> &compilationUnit)
32 : m_compilationUnit(compilationUnit)
33 , m_context(v4->rootContext())
34 , m_parseAsBinding(true)
35 , m_inheritContext(true)
36 , m_parsed(true)
37{
38 if (qml)
39 m_qmlContext.set(v4, *qml);
40
41 m_vmFunction.set(v4, compilationUnit ? compilationUnit->rootFunction() : nullptr);
42}
43
44Script::~Script()
45{
46}
47
48void Script::parse()
49{
50 if (m_parsed)
51 return;
52
53 m_parsed = true;
54
55 ExecutionEngine *v4 = m_context->engine();
56 Scope valueScope(v4);
57
58 QV4::Compiler::Module module(m_sourceFile, m_sourceFile, v4->debugInstructionPass());
59
60 if (m_sourceCode.startsWith(QLatin1String("function("))) {
61 static const int snippetLength = 70;
62 v4->throwSyntaxError(
63 QLatin1String(
64 "Using function expressions as statements in scripts is not compliant "
65 "with the ECMAScript specification:"
66 "\n%1...\n"
67 "If you want a function expression, surround it by parentheses.")
68 .arg(QStringView{m_sourceCode}.left(snippetLength)));
69 return;
70 }
71
72 Engine engine;
73 const Parser::Result parsed = Parser::parseCode(
74 &engine, m_sourceCode, Parser::Goal::Script,
75 Parser::OptionsBuilder().setQmlMode(m_parseAsBinding).setLineNumber(m_line).build());
76
77 for (const DiagnosticMessage &m : parsed.diagnosticMessages) {
78 if (m.isError()) {
79 valueScope.engine->throwSyntaxError(
80 m.message, m_sourceFile, m.loc.startLine, m.loc.startColumn);
81 return;
82 } else {
83 qWarning() << m_sourceFile << ':' << m.loc.startLine << ':' << m.loc.startColumn
84 << ": warning: " << m.message;
85 }
86 }
87
88 if (parsed.success) {
89 using namespace AST;
90 Program *program = AST::cast<Program *>(parsed.rootNode);
91 if (!program) {
92 // if parsing was successful, and we have no program, then
93 // we're done...:
94 return;
95 }
96
97 QV4::Compiler::JSUnitGenerator jsGenerator(&module);
98 RuntimeCodegen cg(v4, &jsGenerator, m_strictMode);
99 if (m_inheritContext)
100 cg.setUseFastLookups(false);
101 cg.generateFromProgram(m_sourceCode, program, &module, m_contextType);
102 if (v4->hasException)
103 return;
104
105 m_compilationUnit = v4->insertCompilationUnit(cg.generateCompilationUnit());
106 m_vmFunction.set(v4, m_compilationUnit->rootFunction());
107 }
108
109 if (!m_vmFunction) {
110 // ### FIX file/line number
111 ScopedObject error(valueScope, v4->newSyntaxErrorObject(QStringLiteral("Syntax error")));
112 v4->throwError(error);
113 }
114}
115
116ReturnedValue Script::run(const QV4::Value *thisObject)
117{
118 if (!m_parsed)
119 parse();
120 if (!m_vmFunction)
121 return Encode::undefined();
122
123 QV4::ExecutionEngine *engine = m_context->engine();
124 QV4::Scope valueScope(engine);
125
126 if (m_qmlContext.isUndefined()) {
127 QScopedValueRollback<Function*> savedGlobalCode(engine->globalCode, m_vmFunction);
128
129 return m_vmFunction->call(thisObject ? thisObject : engine->globalObject, nullptr, 0,
130 m_context);
131 } else {
132 Scoped<QmlContext> qml(valueScope, m_qmlContext.value());
133 return m_vmFunction->call(thisObject, nullptr, 0, qml);
134 }
135}
136
137Function *Script::function()
138{
139 if (!m_parsed)
140 parse();
141 return m_vmFunction;
142}
143
144QQmlRefPointer<QV4::CompiledData::CompilationUnit> Script::precompile(
145 QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine,
146 Compiler::JSUnitGenerator *unitGenerator, const QString &fileName,
147 const QString &source, QList<QQmlError> *reportedErrors,
148 QV4::Compiler::ContextType contextType, InheritContext inheritContext)
149{
150 using namespace QV4::Compiler;
151 using namespace QQmlJS::AST;
152
153 const Parser::Result parsed = Parser::parseCode(jsEngine, source, Parser::Goal::Script);
154
155 QList<QQmlError> errors =
156 QQmlEnginePrivate::qmlErrorFromDiagnostics(fileName, parsed.diagnosticMessages);
157 if (!errors.isEmpty()) {
158 if (reportedErrors)
159 *reportedErrors << errors;
160 return nullptr;
161 }
162
163 Program *program = AST::cast<Program *>(parsed.rootNode);
164 if (!program) {
165 // if parsing was successful, and we have no program, then
166 // we're done...:
167 return nullptr;
168 }
169
170 Codegen cg(unitGenerator, /*strict mode*/false);
171 if (inheritContext == InheritContext::Yes)
172 cg.setUseFastLookups(false);
173 cg.generateFromProgram(source, program, module, contextType);
174 if (cg.hasError()) {
175 if (reportedErrors) {
176 const auto v4Error = cg.error();
177 QQmlError error;
178 error.setUrl(cg.url());
179 error.setLine(qmlConvertSourceCoordinate<quint32, int>(v4Error.loc.startLine));
180 error.setColumn(qmlConvertSourceCoordinate<quint32, int>(v4Error.loc.startColumn));
181 error.setDescription(v4Error.message);
182 reportedErrors->append(error);
183 }
184 return nullptr;
185 }
186
187 return cg.generateCompilationUnit(/*generate unit data*/false);
188}
189
190QT_END_NAMESPACE
Combined button and popup list for selecting options.