From 6f9a090aa5c21916d19e20ebea16ff46690b7346 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 20 Aug 2015 22:17:56 +0200 Subject: Centralize access to qml context Change-Id: If601262c7fd2d304491d60f63a7b9bcd4118cee8 Reviewed-by: Simon Hausmann --- src/qml/types/qquickworkerscript.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'src/qml/types/qquickworkerscript.cpp') diff --git a/src/qml/types/qquickworkerscript.cpp b/src/qml/types/qquickworkerscript.cpp index 09e2709b2d..ab69ed410f 100644 --- a/src/qml/types/qquickworkerscript.cpp +++ b/src/qml/types/qquickworkerscript.cpp @@ -165,7 +165,7 @@ public: QUrl source; bool initialized; QQuickWorkerScript *owner; - QV4::PersistentValue object; + QV4::PersistentValue qmlContext; }; QHash workers; @@ -291,7 +291,6 @@ QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::method_sendMessage(QV4::Call return QV4::Encode::undefined(); } -// Requires handle scope and context scope QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::getWorker(WorkerScript *script) { if (!script->initialized) { @@ -300,9 +299,7 @@ QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::getWorker(WorkerScript *scri QV4::ExecutionEngine *v4 = QV8Engine::getV4(workerEngine); QV4::Scope scope(v4); - script->object.set(v4, QV4::QmlContextWrapper::urlScope(v4, script->source)); - - QV4::Scoped w(scope, script->object.value()); + QV4::Scoped w(scope, QV4::QmlContextWrapper::urlScope(v4, script->source)); Q_ASSERT(!!w); w->setReadOnly(false); @@ -312,9 +309,11 @@ QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::getWorker(WorkerScript *scri w->QV4::Object::put(QV4::ScopedString(scope, v4->newString(QStringLiteral("WorkerScript"))), api); w->setReadOnly(true); + + script->qmlContext.set(v4, v4->rootContext()->newQmlContext(w)); } - return script->object.value(); + return script->qmlContext.value(); } bool QQuickWorkerScriptEnginePrivate::event(QEvent *event) @@ -354,10 +353,12 @@ void QQuickWorkerScriptEnginePrivate::processMessage(int id, const QByteArray &d QV4::ScopedFunctionObject f(scope, workerEngine->onmessage.value()); QV4::ScopedValue value(scope, QV4::Serialize::deserialize(data, v4)); + QV4::Scoped qmlContext(scope, script->qmlContext.value()); + Q_ASSERT(!!qmlContext); QV4::ScopedCallData callData(scope, 2); callData->thisObject = workerEngine->global(); - callData->args[0] = script->object.value(); + callData->args[0] = qmlContext->d()->qml; // ### callData->args[1] = value; f->call(callData); if (scope.hasException()) { @@ -382,10 +383,8 @@ void QQuickWorkerScriptEnginePrivate::processLoad(int id, const QUrl &url) return; script->source = url; - QV4::Scoped activation(scope, getWorker(script)); - if (!activation) - return; - QV4::Scoped qmlContext(scope, v4->rootContext()->newQmlContext(activation)); + QV4::Scoped qmlContext(scope, getWorker(script)); + Q_ASSERT(!!qmlContext); if (const QQmlPrivate::CachedQmlUnit *cachedUnit = QQmlMetaType::findCachedCompilationUnit(url)) { QV4::CompiledData::CompilationUnit *jsUnit = cachedUnit->createCompilationUnit(); -- cgit v1.2.3