| 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 | |
| 4 | #include "qv4script_p.h" |
| 5 | #include <private/qv4mm_p.h> |
| 6 | #include "qv4function_p.h" |
| 7 | #include "qv4context_p.h" |
| 8 | #include "qv4debugging_p.h" |
| 9 | #include "qv4scopedvalue_p.h" |
| 10 | |
| 11 | #include <private/qqmljsengine_p.h> |
| 12 | #include <private/qqmljslexer_p.h> |
| 13 | #include <private/qqmljsparser_p.h> |
| 14 | #include <private/qqmljsast_p.h> |
| 15 | #include <private/qqmlengine_p.h> |
| 16 | #include <private/qqmlsourcecoordinate_p.h> |
| 17 | #include <private/qv4profiling_p.h> |
| 18 | #include <qv4runtimecodegen_p.h> |
| 19 | |
| 20 | #include <QtCore/QDebug> |
| 21 | #include <QtCore/QString> |
| 22 | #include <QScopedValueRollback> |
| 23 | |
| 24 | using namespace QV4; |
| 25 | using namespace QQmlJS; |
| 26 | |
| 27 | Script::Script(ExecutionEngine *v4, QmlContext *qml, const QQmlRefPointer<ExecutableCompilationUnit> &compilationUnit) |
| 28 | : line(1), column(0), context(v4->rootContext()), strictMode(false), inheritContext(true), parsed(false) |
| 29 | , compilationUnit(compilationUnit), parseAsBinding(true) |
| 30 | { |
| 31 | if (qml) |
| 32 | qmlContext.set(engine: v4, value: *qml); |
| 33 | |
| 34 | parsed = true; |
| 35 | |
| 36 | vmFunction.set(e: v4, |
| 37 | newVal: compilationUnit ? compilationUnit->rootFunction() : nullptr); |
| 38 | } |
| 39 | |
| 40 | Script::~Script() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void Script::parse() |
| 45 | { |
| 46 | if (parsed) |
| 47 | return; |
| 48 | |
| 49 | parsed = true; |
| 50 | |
| 51 | ExecutionEngine *v4 = context->engine(); |
| 52 | Scope valueScope(v4); |
| 53 | |
| 54 | QV4::Compiler::Module module(sourceFile, sourceFile, v4->debugger() != nullptr); |
| 55 | |
| 56 | if (sourceCode.startsWith(s: QLatin1String("function(" ))) { |
| 57 | static const int snippetLength = 70; |
| 58 | qWarning() << "Warning: Using function expressions as statements in scripts is not compliant with the ECMAScript specification:\n" |
| 59 | << (QStringView{sourceCode}.left(n: snippetLength) + QLatin1String("..." )) |
| 60 | << "\nThis will throw a syntax error in Qt 5.12. If you want a function expression, surround it by parentheses." ; |
| 61 | } |
| 62 | |
| 63 | Engine ee, *engine = ⅇ |
| 64 | Lexer lexer(engine); |
| 65 | lexer.setCode(code: sourceCode, lineno: line, qmlMode: parseAsBinding); |
| 66 | Parser parser(engine); |
| 67 | |
| 68 | const bool parsed = parser.parseProgram(); |
| 69 | |
| 70 | const auto diagnosticMessages = parser.diagnosticMessages(); |
| 71 | for (const DiagnosticMessage &m : diagnosticMessages) { |
| 72 | if (m.isError()) { |
| 73 | valueScope.engine->throwSyntaxError(m.message, sourceFile, m.loc.startLine, m.loc.startColumn); |
| 74 | return; |
| 75 | } else { |
| 76 | qWarning() << sourceFile << ':' << m.loc.startLine << ':' << m.loc.startColumn |
| 77 | << ": warning: " << m.message; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (parsed) { |
| 82 | using namespace AST; |
| 83 | Program *program = AST::cast<Program *>(parser.rootNode()); |
| 84 | if (!program) { |
| 85 | // if parsing was successful, and we have no program, then |
| 86 | // we're done...: |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | QV4::Compiler::JSUnitGenerator jsGenerator(&module); |
| 91 | RuntimeCodegen cg(v4, &jsGenerator, strictMode); |
| 92 | if (inheritContext) |
| 93 | cg.setUseFastLookups(false); |
| 94 | cg.generateFromProgram(sourceCode, ast: program, module: &module, contextType); |
| 95 | if (v4->hasException) |
| 96 | return; |
| 97 | |
| 98 | compilationUnit = v4->insertCompilationUnit(unit: cg.generateCompilationUnit()); |
| 99 | vmFunction.set(e: v4, newVal: compilationUnit->rootFunction()); |
| 100 | } |
| 101 | |
| 102 | if (!vmFunction) { |
| 103 | // ### FIX file/line number |
| 104 | ScopedObject error(valueScope, v4->newSyntaxErrorObject(QStringLiteral("Syntax error" ))); |
| 105 | v4->throwError(value: error); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ReturnedValue Script::run(const QV4::Value *thisObject) |
| 110 | { |
| 111 | if (!parsed) |
| 112 | parse(); |
| 113 | if (!vmFunction) |
| 114 | return Encode::undefined(); |
| 115 | |
| 116 | QV4::ExecutionEngine *engine = context->engine(); |
| 117 | QV4::Scope valueScope(engine); |
| 118 | |
| 119 | if (qmlContext.isUndefined()) { |
| 120 | QScopedValueRollback<Function*> savedGlobalCode(engine->globalCode, vmFunction); |
| 121 | |
| 122 | return vmFunction->call(thisObject: thisObject ? thisObject : engine->globalObject, argv: nullptr, argc: 0, |
| 123 | context); |
| 124 | } else { |
| 125 | Scoped<QmlContext> qml(valueScope, qmlContext.value()); |
| 126 | return vmFunction->call(thisObject, argv: nullptr, argc: 0, context: qml); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | Function *Script::function() |
| 131 | { |
| 132 | if (!parsed) |
| 133 | parse(); |
| 134 | return vmFunction; |
| 135 | } |
| 136 | |
| 137 | QQmlRefPointer<QV4::CompiledData::CompilationUnit> Script::precompile( |
| 138 | QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine, |
| 139 | Compiler::JSUnitGenerator *unitGenerator, const QString &fileName, |
| 140 | const QString &source, QList<QQmlError> *reportedErrors, |
| 141 | QV4::Compiler::ContextType contextType) |
| 142 | { |
| 143 | using namespace QV4::Compiler; |
| 144 | using namespace QQmlJS::AST; |
| 145 | |
| 146 | Lexer lexer(jsEngine); |
| 147 | lexer.setCode(code: source, /*line*/lineno: 1, /*qml mode*/qmlMode: false); |
| 148 | Parser parser(jsEngine); |
| 149 | |
| 150 | parser.parseProgram(); |
| 151 | |
| 152 | QList<QQmlError> errors = QQmlEnginePrivate::qmlErrorFromDiagnostics(fileName, diagnosticMessages: parser.diagnosticMessages()); |
| 153 | if (!errors.isEmpty()) { |
| 154 | if (reportedErrors) |
| 155 | *reportedErrors << errors; |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
| 159 | Program *program = AST::cast<Program *>(parser.rootNode()); |
| 160 | if (!program) { |
| 161 | // if parsing was successful, and we have no program, then |
| 162 | // we're done...: |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
| 166 | Codegen cg(unitGenerator, /*strict mode*/false); |
| 167 | cg.generateFromProgram(sourceCode: source, ast: program, module, contextType); |
| 168 | if (cg.hasError()) { |
| 169 | if (reportedErrors) { |
| 170 | const auto v4Error = cg.error(); |
| 171 | QQmlError error; |
| 172 | error.setUrl(cg.url()); |
| 173 | error.setLine(qmlConvertSourceCoordinate<quint32, int>(n: v4Error.loc.startLine)); |
| 174 | error.setColumn(qmlConvertSourceCoordinate<quint32, int>(n: v4Error.loc.startColumn)); |
| 175 | error.setDescription(v4Error.message); |
| 176 | reportedErrors->append(t: error); |
| 177 | } |
| 178 | return nullptr; |
| 179 | } |
| 180 | |
| 181 | return cg.generateCompilationUnit(/*generate unit data*/generateUnitData: false); |
| 182 | } |
| 183 | |
| 184 | Script *Script::createFromFileOrCache(ExecutionEngine *engine, QmlContext *qmlContext, const QString &fileName, const QUrl &originalUrl, QString *error) |
| 185 | { |
| 186 | if (error) |
| 187 | error->clear(); |
| 188 | |
| 189 | QQmlMetaType::CachedUnitLookupError cacheError = QQmlMetaType::CachedUnitLookupError::NoError; |
| 190 | const ExecutionEngine::DiskCacheOptions options = engine->diskCacheOptions(); |
| 191 | if (const QQmlPrivate::CachedQmlUnit *cachedUnit |
| 192 | = (options & ExecutionEngine::DiskCache::Aot) |
| 193 | ? QQmlMetaType::findCachedCompilationUnit( |
| 194 | uri: originalUrl, |
| 195 | mode: (options & ExecutionEngine::DiskCache::AotByteCode) |
| 196 | ? QQmlMetaType::AcceptUntyped |
| 197 | : QQmlMetaType::RequireFullyTyped, |
| 198 | status: &cacheError) |
| 199 | : nullptr) { |
| 200 | QQmlRefPointer<QV4::ExecutableCompilationUnit> jsUnit |
| 201 | = engine->insertCompilationUnit( |
| 202 | unit: QQml::makeRefPointer<QV4::CompiledData::CompilationUnit>( |
| 203 | args: cachedUnit->qmlData, args: cachedUnit->aotCompiledFunctions)); |
| 204 | return new QV4::Script(engine, qmlContext, jsUnit); |
| 205 | } |
| 206 | |
| 207 | QFile f(fileName); |
| 208 | if (!f.open(flags: QIODevice::ReadOnly)) { |
| 209 | if (error) { |
| 210 | if (cacheError == QQmlMetaType::CachedUnitLookupError::VersionMismatch) |
| 211 | *error = originalUrl.toString() + QString::fromUtf8(utf8: " was compiled ahead of time with an incompatible version of Qt and the original source code cannot be found. Please recompile" ); |
| 212 | else |
| 213 | *error = QString::fromUtf8(utf8: "Error opening source file %1: %2" ).arg(a: originalUrl.toString()).arg(a: f.errorString()); |
| 214 | } |
| 215 | return nullptr; |
| 216 | } |
| 217 | |
| 218 | QByteArray data = f.readAll(); |
| 219 | QString sourceCode = QString::fromUtf8(ba: data); |
| 220 | |
| 221 | auto result = new QV4::Script(engine, qmlContext, /*parseAsBinding*/false, sourceCode, originalUrl.toString()); |
| 222 | result->contextType = QV4::Compiler::ContextType::ScriptImportedByQML; |
| 223 | result->parse(); |
| 224 | return result; |
| 225 | } |
| 226 | |