diff options
author | Simon Hausmann <simon.hausmann@digia.com> | 2014-06-02 14:50:00 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-06-04 08:27:50 +0200 |
commit | fea26bb2941c3f24c4a5f3ad5efc1b85e0123ff3 (patch) | |
tree | c6d27c921a81a0e68b9a6fb1029cb04d2a0aa08b /src/qml/types | |
parent | cca61d66e71c3d6948db1a881267385ff294eb4e (diff) |
Fix worker scripts with cached compilation units
Try to retrieve them from the compilation unit cache instead of from
the file system if possible. The evaluation code can be shared.
In the long run it would be nice to use the type loader here and allow for
worker scripts to have dependencies/imports. But that is a more intrusive
change given the typeloader's dependency on the engine.
Change-Id: I7f9d6be1ff31433d4b14607cf0c25acdf466ac67
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/qml/types')
-rw-r--r-- | src/qml/types/qquickworkerscript.cpp | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/qml/types/qquickworkerscript.cpp b/src/qml/types/qquickworkerscript.cpp index 507e94fb7e..80c4112930 100644 --- a/src/qml/types/qquickworkerscript.cpp +++ b/src/qml/types/qquickworkerscript.cpp @@ -382,36 +382,44 @@ void QQuickWorkerScriptEnginePrivate::processLoad(int id, const QUrl &url) QString fileName = QQmlFile::urlToLocalFileOrQrc(url); - QFile f(fileName); - if (f.open(QIODevice::ReadOnly)) { - QByteArray data = f.readAll(); - QString sourceCode = QString::fromUtf8(data); - QmlIR::Document::removeScriptPragmas(sourceCode); + QV4::ExecutionEngine *v4 = QV8Engine::getV4(workerEngine); + QV4::Scope scope(v4); + QScopedPointer<QV4::Script> program; - WorkerScript *script = workers.value(id); - if (!script) - return; - script->source = url; + WorkerScript *script = workers.value(id); + if (!script) + return; + script->source = url; - QV4::ExecutionEngine *v4 = QV8Engine::getV4(workerEngine); - QV4::Scope scope(v4); + QV4::Scoped<QV4::Object> activation(scope, getWorker(script)); + if (!activation) + return; - QV4::Scoped<QV4::Object> activation(scope, getWorker(script)); - if (!activation) + if (const QQmlPrivate::CachedQmlUnit *cachedUnit = QQmlMetaType::findCachedCompilationUnit(url)) { + QV4::CompiledData::CompilationUnit *jsUnit = cachedUnit->createCompilationUnit(); + program.reset(new QV4::Script(v4, activation, jsUnit)); + } else { + QFile f(fileName); + if (!f.open(QIODevice::ReadOnly)) { + qWarning().nospace() << "WorkerScript: Cannot find source file " << url.toString(); return; + } + + QByteArray data = f.readAll(); + QString sourceCode = QString::fromUtf8(data); + QmlIR::Document::removeScriptPragmas(sourceCode); - QV4::Script program(v4, activation, sourceCode, url.toString()); + program.reset(new QV4::Script(v4, activation, sourceCode, url.toString())); + program->parse(); + } + + if (!v4->hasException) + program->run(); + if (v4->hasException) { QV4::ExecutionContext *ctx = v4->currentContext(); - program.parse(); - if (!v4->hasException) - program.run(); - if (v4->hasException) { - QQmlError error = QV4::ExecutionEngine::catchExceptionAsQmlError(ctx); - reportScriptException(script, error); - } - } else { - qWarning().nospace() << "WorkerScript: Cannot find source file " << url.toString(); + QQmlError error = QV4::ExecutionEngine::catchExceptionAsQmlError(ctx); + reportScriptException(script, error); } } |