diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-05-14 07:15:08 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2010-05-14 07:15:08 (GMT) |
commit | d9ab23c190de774ea093652060981e14ac5aeaf8 (patch) | |
tree | 80a4926c2ecfd4b9284fe64e23e6d0a6ab646678 | |
parent | 2f6eb60bf4012f9a185d7cd5d4c50762c3abc7f7 (diff) | |
download | Qt-d9ab23c190de774ea093652060981e14ac5aeaf8.zip Qt-d9ab23c190de774ea093652060981e14ac5aeaf8.tar.gz Qt-d9ab23c190de774ea093652060981e14ac5aeaf8.tar.bz2 |
Qt.include() support in worker scripts
-rw-r--r-- | src/declarative/qml/qdeclarativeengine.cpp | 3 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeinclude.cpp | 59 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeinclude_p.h | 2 | ||||
-rw-r--r-- | src/declarative/qml/qdeclarativeworkerscript.cpp | 6 |
4 files changed, 69 insertions, 1 deletions
diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index 8d3ca59..2c89abd 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -207,6 +207,9 @@ QDeclarativeScriptEngine::QDeclarativeScriptEngine(QDeclarativeEnginePrivate *pr //types if (mainthread) qtObject.setProperty(QLatin1String("include"), newFunction(QDeclarativeInclude::include, 2)); + else + qtObject.setProperty(QLatin1String("include"), newFunction(QDeclarativeInclude::worker_include, 2)); + qtObject.setProperty(QLatin1String("isQtObject"), newFunction(QDeclarativeEnginePrivate::isQtObject, 1)); qtObject.setProperty(QLatin1String("rgba"), newFunction(QDeclarativeEnginePrivate::rgba, 4)); qtObject.setProperty(QLatin1String("hsla"), newFunction(QDeclarativeEnginePrivate::hsla, 4)); diff --git a/src/declarative/qml/qdeclarativeinclude.cpp b/src/declarative/qml/qdeclarativeinclude.cpp index b886935..97220f1 100644 --- a/src/declarative/qml/qdeclarativeinclude.cpp +++ b/src/declarative/qml/qdeclarativeinclude.cpp @@ -252,6 +252,65 @@ QScriptValue QDeclarativeInclude::include(QScriptContext *ctxt, QScriptEngine *e return result; } +QScriptValue QDeclarativeInclude::worker_include(QScriptContext *ctxt, QScriptEngine *engine) +{ + if (ctxt->argumentCount() == 0) + return engine->undefinedValue(); + + QDeclarativeEnginePrivate *ep = QDeclarativeEnginePrivate::get(engine); + + QString urlString = ctxt->argument(0).toString(); + QUrl url(ctxt->argument(0).toString()); + if (url.isRelative()) { + QString contextUrl = QScriptDeclarativeClass::scopeChainValue(ctxt, -3).data().toString(); + Q_ASSERT(!contextUrl.isEmpty()); + + url = QUrl(contextUrl).resolved(url); + urlString = url.toString(); + } + + QString localFile = toLocalFileOrQrc(url); + + QScriptValue func = ctxt->argument(1); + if (!func.isFunction()) + func = QScriptValue(); + + QScriptValue result; + if (!localFile.isEmpty()) { + + QFile f(localFile); + if (f.open(QIODevice::ReadOnly)) { + QByteArray data = f.readAll(); + QString code = QString::fromUtf8(data); + QScriptContext *scriptContext = QScriptDeclarativeClass::pushCleanContext(engine); + QScriptValue urlContext = engine->newObject(); + urlContext.setData(QScriptValue(engine, urlString)); + scriptContext->pushScope(urlContext); + + QScriptValue scope = QScriptDeclarativeClass::scopeChainValue(ctxt, -4); + scriptContext->pushScope(scope); + scriptContext->setActivationObject(scope); + + engine->evaluate(code, urlString, 1); + + engine->popContext(); + + if (engine->hasUncaughtException()) { + result = resultValue(engine, Exception); + result.setProperty(QLatin1String("exception"), engine->uncaughtException()); + engine->clearExceptions(); + } else { + result = resultValue(engine, Ok); + } + callback(engine, func, result); + } else { + result = resultValue(engine, NetworkError); + callback(engine, func, result); + } + } + + return result; +} QT_END_NAMESPACE diff --git a/src/declarative/qml/qdeclarativeinclude_p.h b/src/declarative/qml/qdeclarativeinclude_p.h index 9678e42..3124374 100644 --- a/src/declarative/qml/qdeclarativeinclude_p.h +++ b/src/declarative/qml/qdeclarativeinclude_p.h @@ -88,7 +88,9 @@ public: static QScriptValue resultValue(QScriptEngine *, Status status = Loading); static void callback(QScriptEngine *, QScriptValue &callback, QScriptValue &status); + static QScriptValue include(QScriptContext *ctxt, QScriptEngine *engine); + static QScriptValue worker_include(QScriptContext *ctxt, QScriptEngine *engine); public slots: void finished(); diff --git a/src/declarative/qml/qdeclarativeworkerscript.cpp b/src/declarative/qml/qdeclarativeworkerscript.cpp index c55998f..4b687a9 100644 --- a/src/declarative/qml/qdeclarativeworkerscript.cpp +++ b/src/declarative/qml/qdeclarativeworkerscript.cpp @@ -289,7 +289,11 @@ void QDeclarativeWorkerScriptEnginePrivate::processLoad(int id, const QUrl &url) QScriptValue activation = getWorker(id); - QScriptContext *ctxt = workerEngine->pushContext(); + QScriptContext *ctxt = QScriptDeclarativeClass::pushCleanContext(workerEngine); + QScriptValue urlContext = workerEngine->newObject(); + urlContext.setData(QScriptValue(workerEngine, fileName)); + ctxt->pushScope(urlContext); + ctxt->pushScope(activation); ctxt->setActivationObject(activation); workerEngine->baseUrl = url; |