aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4script.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@digia.com>2013-09-29 21:20:09 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-30 18:21:26 +0200
commitc48d727e25f0a07d709a81765af6196bc0ddb4c5 (patch)
tree1ad34d61bf20e8756d52f44fd17956844a793d84 /src/qml/jsruntime/qv4script.cpp
parent2b6dfcf23b8e9d672dff0a083ed4e7adc28115eb (diff)
Compile imported scripts in the loader thread
This has the benefit of blocking the GUI thread less and speeding up type creation in the GUI thread (for types that import js libraries). This patch also brings one behavioral change: Due to the parsing at type instantiation type, things like syntax errors for script imports would only generate a run-time warning and the code in the QML file would just see "undefined". Errors in the script now generate real errors at component compilation time, meaning the errors come out earlier and as real errors. This patch implements the separation for the VME only (to keep the size of this patch small). Change-Id: I82f7f3a2d3d4524ea12a7ab62abd8640aba6a47f Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/qml/jsruntime/qv4script.cpp')
-rw-r--r--src/qml/jsruntime/qv4script.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4script.cpp b/src/qml/jsruntime/qv4script.cpp
index c2d35d5926..2350224c32 100644
--- a/src/qml/jsruntime/qv4script.cpp
+++ b/src/qml/jsruntime/qv4script.cpp
@@ -142,6 +142,22 @@ struct CompilationUnitHolder : public QV4::Object
DEFINE_MANAGED_VTABLE(CompilationUnitHolder);
+Script::Script(ExecutionEngine *v4, ObjectRef qml, CompiledData::CompilationUnit *compilationUnit)
+ : line(0), column(0), scope(v4->rootContext), strictMode(false), inheritContext(true), parsed(false)
+ , qml(qml.asReturnedValue()), vmFunction(0), parseAsBinding(true)
+{
+ parsed = true;
+
+ if (compilationUnit) {
+ vmFunction = compilationUnit->linkToEngine(v4);
+ Q_ASSERT(vmFunction);
+ Scope valueScope(v4);
+ ScopedValue holder(valueScope, Value::fromObject(new (v4->memoryManager) CompilationUnitHolder(v4, compilationUnit)));
+ compilationUnitHolder = holder;
+ } else
+ vmFunction = 0;
+}
+
Script::~Script()
{
}
@@ -264,6 +280,85 @@ Function *Script::function()
return vmFunction;
}
+struct PrecompilingCodeGen : public QQmlJS::Codegen
+{
+ struct CompileError {};
+
+ PrecompilingCodeGen(bool strict)
+ : QQmlJS::Codegen(strict)
+ {}
+
+ virtual void throwSyntaxError(const QQmlJS::AST::SourceLocation &loc, const QString &detail)
+ {
+ QQmlJS::Codegen::throwSyntaxError(loc, detail);
+ throw CompileError();
+ }
+
+ virtual void throwReferenceError(const QQmlJS::AST::SourceLocation &loc, const QString &detail)
+ {
+ QQmlJS::Codegen::throwReferenceError(loc, detail);
+ throw CompileError();
+ }
+};
+
+CompiledData::CompilationUnit *Script::precompile(ExecutionEngine *engine, const QUrl &url, const QString &source, bool parseAsBinding, QList<QQmlError> *reportedErrors)
+{
+ using namespace QQmlJS;
+ using namespace QQmlJS::AST;
+
+ QQmlJS::V4IR::Module module;
+
+ QQmlJS::Engine ee;
+ QQmlJS::Lexer lexer(&ee);
+ lexer.setCode(source, /*line*/1, /*qml mode*/true);
+ QQmlJS::Parser parser(&ee);
+
+ parser.parseProgram();
+
+ QList<QQmlError> errors;
+
+ foreach (const QQmlJS::DiagnosticMessage &m, parser.diagnosticMessages()) {
+ if (m.isWarning()) {
+ qWarning("%s:%d : %s", qPrintable(url.toString()), m.loc.startLine, qPrintable(m.message));
+ continue;
+ }
+
+ QQmlError error;
+ error.setUrl(url);
+ error.setDescription(m.message);
+ error.setLine(m.loc.startLine);
+ error.setColumn(m.loc.startColumn);
+ errors << error;
+ }
+
+ if (!errors.isEmpty()) {
+ if (reportedErrors)
+ *reportedErrors << errors;
+ return 0;
+ }
+
+ Program *program = AST::cast<Program *>(parser.rootNode());
+ if (!program) {
+ // if parsing was successful, and we have no program, then
+ // we're done...:
+ return 0;
+ }
+
+ PrecompilingCodeGen cg(/*strict mode*/false);
+ try {
+ cg.generateFromProgram(url.toString(), source, program, &module, parseAsBinding ? QQmlJS::Codegen::QmlBinding : QQmlJS::Codegen::GlobalCode);
+ } catch (const PrecompilingCodeGen::CompileError &) {
+ if (reportedErrors)
+ *reportedErrors << cg.errors();
+ return 0;
+ }
+
+ Compiler::JSUnitGenerator jsGenerator(&module);
+ QScopedPointer<QQmlJS::EvalInstructionSelection> isel(engine->iselFactory->create(engine->executableAllocator, &module, &jsGenerator));
+ isel->setUseFastLookups(false);
+ return isel->compile();
+}
+
ReturnedValue Script::qmlBinding()
{
if (!parsed)