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)
39 m_qmlContext.set(v4, *qml);
41 m_vmFunction.set(v4, compilationUnit ? compilationUnit->rootFunction() :
nullptr);
55 ExecutionEngine *v4 = m_context->engine();
58 QV4::Compiler::Module module(m_sourceFile, m_sourceFile, v4->debugInstructionPass());
60 if (m_sourceCode.startsWith(QLatin1String(
"function("))) {
61 static const int snippetLength = 70;
64 "Using function expressions as statements in scripts is not compliant "
65 "with the ECMAScript specification:"
67 "If you want a function expression, surround it by parentheses.")
68 .arg(QStringView{m_sourceCode}.left(snippetLength)));
73 const Parser::Result parsed = Parser::parseCode(
74 &engine, m_sourceCode, Parser::Goal::Script,
75 Parser::OptionsBuilder().setQmlMode(m_parseAsBinding).setLineNumber(m_line).build());
77 for (
const DiagnosticMessage &m : parsed.diagnosticMessages) {
79 valueScope.engine->throwSyntaxError(
80 m.message, m_sourceFile, m.loc.startLine, m.loc.startColumn);
83 qWarning() << m_sourceFile <<
':' << m.loc.startLine <<
':' << m.loc.startColumn
84 <<
": warning: " << m.message;
90 Program *program = AST::cast<Program *>(parsed.rootNode);
97 QV4::Compiler::JSUnitGenerator jsGenerator(&module);
98 RuntimeCodegen cg(v4, &jsGenerator, m_strictMode);
100 cg.setUseFastLookups(
false);
101 cg.generateFromProgram(m_sourceCode, program, &module, m_contextType);
102 if (v4->hasException)
105 m_compilationUnit = v4->insertCompilationUnit(cg.generateCompilationUnit());
106 m_vmFunction.set(v4, m_compilationUnit->rootFunction());
111 ScopedObject error(valueScope, v4->newSyntaxErrorObject(QStringLiteral(
"Syntax error")));
112 v4->throwError(error);
116ReturnedValue Script::run(
const QV4::Value *thisObject)
121 return Encode::undefined();
123 QV4::ExecutionEngine *engine = m_context->engine();
124 QV4::Scope valueScope(engine);
126 if (m_qmlContext.isUndefined()) {
127 QScopedValueRollback<Function*> savedGlobalCode(engine->globalCode, m_vmFunction);
129 return m_vmFunction->call(thisObject ? thisObject : engine->globalObject,
nullptr, 0,
132 Scoped<QmlContext> qml(valueScope, m_qmlContext.value());
133 return m_vmFunction->call(thisObject,
nullptr, 0, qml);
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)
150 using namespace QV4::Compiler;
151 using namespace QQmlJS::AST;
153 const Parser::Result parsed = Parser::parseCode(jsEngine, source, Parser::Goal::Script);
155 QList<QQmlError> errors =
156 QQmlEnginePrivate::qmlErrorFromDiagnostics(fileName, parsed.diagnosticMessages);
157 if (!errors.isEmpty()) {
159 *reportedErrors << errors;
163 Program *program = AST::cast<Program *>(parsed.rootNode);
170 Codegen cg(unitGenerator,
false);
171 if (inheritContext == InheritContext::Yes)
172 cg.setUseFastLookups(
false);
173 cg.generateFromProgram(source, program, module, contextType);
175 if (reportedErrors) {
176 const auto v4Error = cg.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);
187 return cg.generateCompilationUnit(
false);