aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime
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
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')
-rw-r--r--src/qml/jsruntime/qv4script.cpp95
-rw-r--r--src/qml/jsruntime/qv4script_p.h6
2 files changed, 101 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)
diff --git a/src/qml/jsruntime/qv4script_p.h b/src/qml/jsruntime/qv4script_p.h
index 5442e265fd..52ad4dd78c 100644
--- a/src/qml/jsruntime/qv4script_p.h
+++ b/src/qml/jsruntime/qv4script_p.h
@@ -45,6 +45,8 @@
#include "qv4engine_p.h"
#include "qv4functionobject_p.h"
+#include <QQmlError>
+
QT_BEGIN_NAMESPACE
namespace QV4 {
@@ -77,6 +79,7 @@ struct Q_QML_EXPORT Script {
: sourceFile(source), line(line), column(column), sourceCode(sourceCode)
, scope(engine->rootContext), strictMode(false), inheritContext(true), parsed(false)
, qml(qml.asReturnedValue()), vmFunction(0), parseAsBinding(true) {}
+ Script(ExecutionEngine *engine, ObjectRef qml, CompiledData::CompilationUnit *compilationUnit);
~Script();
QString sourceFile;
int line;
@@ -97,6 +100,9 @@ struct Q_QML_EXPORT Script {
Function *function();
+ static CompiledData::CompilationUnit *precompile(ExecutionEngine *engine, const QUrl &url, const QString &source,
+ bool parseAsBinding,
+ QList<QQmlError> *reportedErrors = 0);
static ReturnedValue evaluate(ExecutionEngine *engine, const QString &script, ObjectRef scopeObject);
};