| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/types/qquickworkerscript.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||
| 2 | - | |||||||||||||
| 3 | - | |||||||||||||
| 4 | - | |||||||||||||
| 5 | - | |||||||||||||
| 6 | - | |||||||||||||
| 7 | - | |||||||||||||
| 8 | - | |||||||||||||
| 9 | - | |||||||||||||
| 10 | - | |||||||||||||
| 11 | class WorkerDataEvent : public QEvent | - | ||||||||||||
| 12 | { | - | ||||||||||||
| 13 | public: | - | ||||||||||||
| 14 | enum Type { WorkerData = QEvent::User }; | - | ||||||||||||
| 15 | - | |||||||||||||
| 16 | WorkerDataEvent(int workerId, const QByteArray &data); | - | ||||||||||||
| 17 | virtual ~WorkerDataEvent(); | - | ||||||||||||
| 18 | - | |||||||||||||
| 19 | int workerId() const; | - | ||||||||||||
| 20 | QByteArray data() const; | - | ||||||||||||
| 21 | - | |||||||||||||
| 22 | private: | - | ||||||||||||
| 23 | int m_id; | - | ||||||||||||
| 24 | QByteArray m_data; | - | ||||||||||||
| 25 | }; | - | ||||||||||||
| 26 | - | |||||||||||||
| 27 | class WorkerLoadEvent : public QEvent | - | ||||||||||||
| 28 | { | - | ||||||||||||
| 29 | public: | - | ||||||||||||
| 30 | enum Type { WorkerLoad = WorkerDataEvent::WorkerData + 1 }; | - | ||||||||||||
| 31 | - | |||||||||||||
| 32 | WorkerLoadEvent(int workerId, const QUrl &url); | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | int workerId() const; | - | ||||||||||||
| 35 | QUrl url() const; | - | ||||||||||||
| 36 | - | |||||||||||||
| 37 | private: | - | ||||||||||||
| 38 | int m_id; | - | ||||||||||||
| 39 | QUrl m_url; | - | ||||||||||||
| 40 | }; | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | class WorkerRemoveEvent : public QEvent | - | ||||||||||||
| 43 | { | - | ||||||||||||
| 44 | public: | - | ||||||||||||
| 45 | enum Type { WorkerRemove = WorkerLoadEvent::WorkerLoad + 1 }; | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | WorkerRemoveEvent(int workerId); | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | int workerId() const; | - | ||||||||||||
| 50 | - | |||||||||||||
| 51 | private: | - | ||||||||||||
| 52 | int m_id; | - | ||||||||||||
| 53 | }; | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | class WorkerErrorEvent : public QEvent | - | ||||||||||||
| 56 | { | - | ||||||||||||
| 57 | public: | - | ||||||||||||
| 58 | enum Type { WorkerError = WorkerRemoveEvent::WorkerRemove + 1 }; | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | WorkerErrorEvent(const QQmlError &error); | - | ||||||||||||
| 61 | - | |||||||||||||
| 62 | QQmlError error() const; | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | private: | - | ||||||||||||
| 65 | QQmlError m_error; | - | ||||||||||||
| 66 | }; | - | ||||||||||||
| 67 | - | |||||||||||||
| 68 | class QQuickWorkerScriptEnginePrivate : public QObject | - | ||||||||||||
| 69 | { | - | ||||||||||||
| 70 | public: | - | ||||||||||||
| 71 | #pragma GCC diagnostic push | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | #pragma GCC diagnostic ignored "-Wsuggest-override" | - | ||||||||||||
| 74 | static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); static inline QString tr(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } __attribute__ ((__deprecated__)) static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } private: | - | ||||||||||||
| 75 | #pragma GCC diagnostic ignored "-Wattributes" | - | ||||||||||||
| 76 | __attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); | - | ||||||||||||
| 77 | #pragma GCC diagnostic pop | - | ||||||||||||
| 78 | struct QPrivateSignal {}; | - | ||||||||||||
| 79 | public: | - | ||||||||||||
| 80 | enum WorkerEventTypes { | - | ||||||||||||
| 81 | WorkerDestroyEvent = QEvent::User + 100 | - | ||||||||||||
| 82 | }; | - | ||||||||||||
| 83 | - | |||||||||||||
| 84 | QQuickWorkerScriptEnginePrivate(QQmlEngine *eng); | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | class WorkerEngine : public QV8Engine | - | ||||||||||||
| 87 | { | - | ||||||||||||
| 88 | public: | - | ||||||||||||
| 89 | WorkerEngine(QQuickWorkerScriptEnginePrivate *parent); | - | ||||||||||||
| 90 | ~WorkerEngine(); | - | ||||||||||||
| 91 | - | |||||||||||||
| 92 | void init(); | - | ||||||||||||
| 93 | - | |||||||||||||
| 94 | - | |||||||||||||
| 95 | QNetworkAccessManager *networkAccessManager() override; | - | ||||||||||||
| 96 | - | |||||||||||||
| 97 | - | |||||||||||||
| 98 | QQuickWorkerScriptEnginePrivate *p; | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | QV4::ReturnedValue sendFunction(int id); | - | ||||||||||||
| 101 | - | |||||||||||||
| 102 | QV4::PersistentValue onmessage; | - | ||||||||||||
| 103 | private: | - | ||||||||||||
| 104 | QV4::PersistentValue createsend; | - | ||||||||||||
| 105 | - | |||||||||||||
| 106 | QNetworkAccessManager *accessManager; | - | ||||||||||||
| 107 | - | |||||||||||||
| 108 | }; | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | WorkerEngine *workerEngine; | - | ||||||||||||
| 111 | static QQuickWorkerScriptEnginePrivate *get(QV8Engine *e) { | - | ||||||||||||
| 112 | return executed 8 times by 1 test: static_cast<WorkerEngine *>(e)->p;return static_cast<WorkerEngine *>(e)->p;Executed by:
executed 8 times by 1 test: return static_cast<WorkerEngine *>(e)->p;Executed by:
| 8 | ||||||||||||
| 113 | } | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | QQmlEngine *qmlengine; | - | ||||||||||||
| 116 | - | |||||||||||||
| 117 | QMutex m_lock; | - | ||||||||||||
| 118 | QWaitCondition m_wait; | - | ||||||||||||
| 119 | - | |||||||||||||
| 120 | struct WorkerScript { | - | ||||||||||||
| 121 | WorkerScript(); | - | ||||||||||||
| 122 | ~WorkerScript(); | - | ||||||||||||
| 123 | - | |||||||||||||
| 124 | int id; | - | ||||||||||||
| 125 | QUrl source; | - | ||||||||||||
| 126 | bool initialized; | - | ||||||||||||
| 127 | QQuickWorkerScript *owner; | - | ||||||||||||
| 128 | QV4::PersistentValue qmlContext; | - | ||||||||||||
| 129 | }; | - | ||||||||||||
| 130 | - | |||||||||||||
| 131 | QHash<int, WorkerScript *> workers; | - | ||||||||||||
| 132 | QV4::ReturnedValue getWorker(WorkerScript *); | - | ||||||||||||
| 133 | - | |||||||||||||
| 134 | int m_nextId; | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | static QV4::ReturnedValue method_sendMessage(const QV4::FunctionObject *, const QV4::Value *thisObject, const QV4::Value *argv, int argc); | - | ||||||||||||
| 137 | - | |||||||||||||
| 138 | public : | - | ||||||||||||
| 139 | void stopThread(); | - | ||||||||||||
| 140 | - | |||||||||||||
| 141 | protected: | - | ||||||||||||
| 142 | bool event(QEvent *) override; | - | ||||||||||||
| 143 | - | |||||||||||||
| 144 | private: | - | ||||||||||||
| 145 | void processMessage(int, const QByteArray &); | - | ||||||||||||
| 146 | void processLoad(int, const QUrl &); | - | ||||||||||||
| 147 | void reportScriptException(WorkerScript *, const QQmlError &error); | - | ||||||||||||
| 148 | }; | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | QQuickWorkerScriptEnginePrivate::WorkerEngine::WorkerEngine(QQuickWorkerScriptEnginePrivate *parent) | - | ||||||||||||
| 151 | : QV8Engine(new QV4::ExecutionEngine), p(parent) | - | ||||||||||||
| 152 | - | |||||||||||||
| 153 | , accessManager(nullptr) | - | ||||||||||||
| 154 | - | |||||||||||||
| 155 | { | - | ||||||||||||
| 156 | m_v4Engine->v8Engine = this; | - | ||||||||||||
| 157 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 158 | - | |||||||||||||
| 159 | QQuickWorkerScriptEnginePrivate::WorkerEngine::~WorkerEngine() | - | ||||||||||||
| 160 | { | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | delete accessManager; | - | ||||||||||||
| 163 | - | |||||||||||||
| 164 | delete m_v4Engine; | - | ||||||||||||
| 165 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 166 | - | |||||||||||||
| 167 | void QQuickWorkerScriptEnginePrivate::WorkerEngine::init() | - | ||||||||||||
| 168 | { | - | ||||||||||||
| 169 | initQmlGlobalObject(); | - | ||||||||||||
| 170 | QV4::Scope scope(m_v4Engine); | - | ||||||||||||
| 171 | QV4::ExecutionContext *globalContext = scope.engine->rootContext(); | - | ||||||||||||
| 172 | onmessage.set(scope.engine, QV4::Script(globalContext, QV4::Compiler::ContextType::Global, QString::fromUtf8("(function(object, message) { " "var isfunction = false; " "try { " "isfunction = object.WorkerScript.onMessage instanceof Function; " "} catch (e) {}" "if (isfunction) " "object.WorkerScript.onMessage(message); " "})")).run()); | - | ||||||||||||
| 173 | ((!scope.engine->hasException) ? static_cast<void>(0) : qt_assert("!scope.engine->hasException", __FILE__, 248)); | - | ||||||||||||
| 174 | QV4::Script createsendscript(globalContext, QV4::Compiler::ContextType::Global, QString::fromUtf8("(function(method, engine) { " "return (function(id) { " "return (function(message) { " "if (arguments.length) method(engine, id, message); " "}); " "}); " "})")); | - | ||||||||||||
| 175 | QV4::ScopedFunctionObject createsendconstructor(scope, createsendscript.run()); | - | ||||||||||||
| 176 | ((!scope.engine->hasException) ? static_cast<void>(0) : qt_assert("!scope.engine->hasException", __FILE__, 251)); | - | ||||||||||||
| 177 | QV4::ScopedString name(scope, m_v4Engine->newString(([]() noexcept -> QString { enum { Size = sizeof(u"" "sendMessage")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "sendMessage" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return executed 1110 times by 5 tests: qstring_literal_temp;return qstring_literal_temp;Executed by:
executed 1110 times by 5 tests: }())));return qstring_literal_temp;Executed by:
| 1110 | ||||||||||||
| 178 | QV4::ScopedValue function(scope, QV4::FunctionObject::createBuiltinFunction(m_v4Engine, name, method_sendMessage, 1)); | - | ||||||||||||
| 179 | QV4::JSCallData jsCallData(scope, 1); | - | ||||||||||||
| 180 | jsCallData->args[0] = function; | - | ||||||||||||
| 181 | *jsCallData->thisObject = m_v4Engine->global(); | - | ||||||||||||
| 182 | createsend.set(scope.engine, createsendconstructor->call(jsCallData)); | - | ||||||||||||
| 183 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 184 | - | |||||||||||||
| 185 | - | |||||||||||||
| 186 | QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::WorkerEngine::sendFunction(int id) | - | ||||||||||||
| 187 | { | - | ||||||||||||
| 188 | QV4::ExecutionEngine *v4 = createsend.engine(); | - | ||||||||||||
| 189 | if (!v4
| 0-1152 | ||||||||||||
| 190 | return never executed: QV4::Encode::undefined();return QV4::Encode::undefined();never executed: return QV4::Encode::undefined(); | 0 | ||||||||||||
| 191 | - | |||||||||||||
| 192 | QV4::Scope scope(v4); | - | ||||||||||||
| 193 | QV4::ScopedFunctionObject f(scope, createsend.value()); | - | ||||||||||||
| 194 | - | |||||||||||||
| 195 | QV4::ScopedValue v(scope); | - | ||||||||||||
| 196 | QV4::JSCallData jsCallData(scope, 1); | - | ||||||||||||
| 197 | jsCallData->args[0] = QV4::Primitive::fromInt32(id); | - | ||||||||||||
| 198 | *jsCallData->thisObject = m_v4Engine->global(); | - | ||||||||||||
| 199 | v = f->call(jsCallData); | - | ||||||||||||
| 200 | if (scope.hasException()
| 0-1152 | ||||||||||||
| 201 | v = scope.engine->catchException(); never executed: v = scope.engine->catchException(); | 0 | ||||||||||||
| 202 | return executed 1152 times by 5 tests: v->asReturnedValue();return v->asReturnedValue();Executed by:
executed 1152 times by 5 tests: return v->asReturnedValue();Executed by:
| 1152 | ||||||||||||
| 203 | } | - | ||||||||||||
| 204 | - | |||||||||||||
| 205 | - | |||||||||||||
| 206 | QNetworkAccessManager *QQuickWorkerScriptEnginePrivate::WorkerEngine::networkAccessManager() | - | ||||||||||||
| 207 | { | - | ||||||||||||
| 208 | if (!accessManager
| 0 | ||||||||||||
| 209 | if (p->qmlengine
| 0 | ||||||||||||
| 210 | accessManager = p->qmlengine->networkAccessManagerFactory()->create(p); | - | ||||||||||||
| 211 | } never executed: else {end of block | 0 | ||||||||||||
| 212 | accessManager = new QNetworkAccessManager(p); | - | ||||||||||||
| 213 | } never executed: end of block | 0 | ||||||||||||
| 214 | } | - | ||||||||||||
| 215 | return never executed: accessManager;return accessManager;never executed: return accessManager; | 0 | ||||||||||||
| 216 | } | - | ||||||||||||
| 217 | - | |||||||||||||
| 218 | - | |||||||||||||
| 219 | QQuickWorkerScriptEnginePrivate::QQuickWorkerScriptEnginePrivate(QQmlEngine *engine) | - | ||||||||||||
| 220 | : workerEngine(nullptr), qmlengine(engine), m_nextId(0) | - | ||||||||||||
| 221 | { | - | ||||||||||||
| 222 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 223 | - | |||||||||||||
| 224 | QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::method_sendMessage(const QV4::FunctionObject *b, | - | ||||||||||||
| 225 | const QV4::Value *, const QV4::Value *argv, int argc) | - | ||||||||||||
| 226 | { | - | ||||||||||||
| 227 | QV4::Scope scope(b); | - | ||||||||||||
| 228 | WorkerEngine *engine = static_cast<WorkerEngine *>(scope.engine->v8Engine); | - | ||||||||||||
| 229 | - | |||||||||||||
| 230 | int id = argc > 1
| 0-976 | ||||||||||||
| 231 | - | |||||||||||||
| 232 | QV4::ScopedValue v(scope, argc > 2 ? argv[2] : QV4::Primitive::undefinedValue()); | - | ||||||||||||
| 233 | QByteArray data = QV4::Serialize::serialize(v, scope.engine); | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | QMutexLocker locker(&engine->p->m_lock); | - | ||||||||||||
| 236 | WorkerScript *script = engine->p->workers.value(id); | - | ||||||||||||
| 237 | if (script
| 0-976 | ||||||||||||
| 238 | QCoreApplication::postEvent(script->owner, new WorkerDataEvent(0, data)); executed 972 times by 4 tests: QCoreApplication::postEvent(script->owner, new WorkerDataEvent(0, data));Executed by:
| 972 | ||||||||||||
| 239 | - | |||||||||||||
| 240 | return executed 976 times by 4 tests: QV4::Encode::undefined();return QV4::Encode::undefined();Executed by:
executed 976 times by 4 tests: return QV4::Encode::undefined();Executed by:
| 976 | ||||||||||||
| 241 | } | - | ||||||||||||
| 242 | - | |||||||||||||
| 243 | QV4::ReturnedValue QQuickWorkerScriptEnginePrivate::getWorker(WorkerScript *script) | - | ||||||||||||
| 244 | { | - | ||||||||||||
| 245 | if (!script->initialized
| 2-1152 | ||||||||||||
| 246 | script->initialized = true; | - | ||||||||||||
| 247 | - | |||||||||||||
| 248 | QV4::ExecutionEngine *v4 = QV8Engine::getV4(workerEngine); | - | ||||||||||||
| 249 | QV4::Scope scope(v4); | - | ||||||||||||
| 250 | QV4::ScopedValue v(scope, workerEngine->sendFunction(script->id)); | - | ||||||||||||
| 251 | script->qmlContext.set(v4, QV4::QmlContext::createWorkerContext(v4->rootContext(), script->source, v)); | - | ||||||||||||
| 252 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 253 | - | |||||||||||||
| 254 | return executed 1154 times by 5 tests: script->qmlContext.value();return script->qmlContext.value();Executed by:
executed 1154 times by 5 tests: return script->qmlContext.value();Executed by:
| 1154 | ||||||||||||
| 255 | } | - | ||||||||||||
| 256 | - | |||||||||||||
| 257 | bool QQuickWorkerScriptEnginePrivate::event(QEvent *event) | - | ||||||||||||
| 258 | { | - | ||||||||||||
| 259 | if (event->type() == (QEvent::Type)WorkerDataEvent::WorkerData
| 1182-4526 | ||||||||||||
| 260 | WorkerDataEvent *workerEvent = static_cast<WorkerDataEvent *>(event); | - | ||||||||||||
| 261 | processMessage(workerEvent->workerId(), workerEvent->data()); | - | ||||||||||||
| 262 | return executed 1182 times by 5 tests: true;return true;Executed by:
executed 1182 times by 5 tests: return true;Executed by:
| 1182 | ||||||||||||
| 263 | } else if (event->type() == (QEvent::Type)WorkerLoadEvent::WorkerLoad
| 1154-3372 | ||||||||||||
| 264 | WorkerLoadEvent *workerEvent = static_cast<WorkerLoadEvent *>(event); | - | ||||||||||||
| 265 | processLoad(workerEvent->workerId(), workerEvent->url()); | - | ||||||||||||
| 266 | return executed 1154 times by 5 tests: true;return true;Executed by:
executed 1154 times by 5 tests: return true;Executed by:
| 1154 | ||||||||||||
| 267 | } else if (event->type() == (QEvent::Type)WorkerDestroyEvent
| 1110-2262 | ||||||||||||
| 268 | stopThread(); | - | ||||||||||||
| 269 | return executed 1110 times by 5 tests: true;return true;Executed by:
executed 1110 times by 5 tests: return true;Executed by:
| 1110 | ||||||||||||
| 270 | } else if (event->type() == (QEvent::Type)WorkerRemoveEvent::WorkerRemove
| 1110-1152 | ||||||||||||
| 271 | WorkerRemoveEvent *workerEvent = static_cast<WorkerRemoveEvent *>(event); | - | ||||||||||||
| 272 | QHash<int, WorkerScript *>::iterator itr = workers.find(workerEvent->workerId()); | - | ||||||||||||
| 273 | if (itr != workers.end()
| 0-1152 | ||||||||||||
| 274 | delete itr.value(); | - | ||||||||||||
| 275 | workers.erase(itr); | - | ||||||||||||
| 276 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 277 | return executed 1152 times by 5 tests: true;return true;Executed by:
executed 1152 times by 5 tests: return true;Executed by:
| 1152 | ||||||||||||
| 278 | } else { | - | ||||||||||||
| 279 | return executed 1110 times by 5 tests: QObject::event(event);return QObject::event(event);Executed by:
executed 1110 times by 5 tests: return QObject::event(event);Executed by:
| 1110 | ||||||||||||
| 280 | } | - | ||||||||||||
| 281 | } | - | ||||||||||||
| 282 | - | |||||||||||||
| 283 | void QQuickWorkerScriptEnginePrivate::processMessage(int id, const QByteArray &data) | - | ||||||||||||
| 284 | { | - | ||||||||||||
| 285 | WorkerScript *script = workers.value(id); | - | ||||||||||||
| 286 | if (!script
| 0-1182 | ||||||||||||
| 287 | return; never executed: return; | 0 | ||||||||||||
| 288 | - | |||||||||||||
| 289 | QV4::ExecutionEngine *v4 = QV8Engine::getV4(workerEngine); | - | ||||||||||||
| 290 | QV4::Scope scope(v4); | - | ||||||||||||
| 291 | QV4::ScopedFunctionObject f(scope, workerEngine->onmessage.value()); | - | ||||||||||||
| 292 | - | |||||||||||||
| 293 | QV4::ScopedValue value(scope, QV4::Serialize::deserialize(data, v4)); | - | ||||||||||||
| 294 | QV4::Scoped<QV4::QmlContext> qmlContext(scope, script->qmlContext.value()); | - | ||||||||||||
| 295 | ((!!qmlContext) ? static_cast<void>(0) : qt_assert("!!qmlContext", __FILE__, 370)); | - | ||||||||||||
| 296 | - | |||||||||||||
| 297 | QV4::JSCallData jsCallData(scope, 2); | - | ||||||||||||
| 298 | *jsCallData->thisObject = v4->global(); | - | ||||||||||||
| 299 | jsCallData->args[0] = qmlContext->d()->qml(); | - | ||||||||||||
| 300 | jsCallData->args[1] = value; | - | ||||||||||||
| 301 | f->call(jsCallData); | - | ||||||||||||
| 302 | if (scope.hasException()
| 4-1178 | ||||||||||||
| 303 | QQmlError error = scope.engine->catchExceptionAsQmlError(); | - | ||||||||||||
| 304 | reportScriptException(script, error); | - | ||||||||||||
| 305 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 306 | } executed 1182 times by 5 tests: end of blockExecuted by:
| 1182 | ||||||||||||
| 307 | - | |||||||||||||
| 308 | void QQuickWorkerScriptEnginePrivate::processLoad(int id, const QUrl &url) | - | ||||||||||||
| 309 | { | - | ||||||||||||
| 310 | if (url.isRelative()
| 0-1154 | ||||||||||||
| 311 | return; never executed: return; | 0 | ||||||||||||
| 312 | - | |||||||||||||
| 313 | QString fileName = QQmlFile::urlToLocalFileOrQrc(url); | - | ||||||||||||
| 314 | - | |||||||||||||
| 315 | QV4::ExecutionEngine *v4 = QV8Engine::getV4(workerEngine); | - | ||||||||||||
| 316 | QV4::Scope scope(v4); | - | ||||||||||||
| 317 | QScopedPointer<QV4::Script> program; | - | ||||||||||||
| 318 | - | |||||||||||||
| 319 | WorkerScript *script = workers.value(id); | - | ||||||||||||
| 320 | if (!script
| 0-1154 | ||||||||||||
| 321 | return; never executed: return; | 0 | ||||||||||||
| 322 | script->source = url; | - | ||||||||||||
| 323 | - | |||||||||||||
| 324 | QV4::Scoped<QV4::QmlContext> qmlContext(scope, getWorker(script)); | - | ||||||||||||
| 325 | ((!!qmlContext) ? static_cast<void>(0) : qt_assert("!!qmlContext", __FILE__, 400)); | - | ||||||||||||
| 326 | - | |||||||||||||
| 327 | QString error; | - | ||||||||||||
| 328 | program.reset(QV4::Script::createFromFileOrCache(v4, qmlContext, fileName, url, &error)); | - | ||||||||||||
| 329 | if (program.isNull()
| 0-1154 | ||||||||||||
| 330 | if (!error.isEmpty()
| 0 | ||||||||||||
| 331 | QMessageLogger(__FILE__, 406, __PRETTY_FUNCTION__).warning().nospace() << error; never executed: QMessageLogger(__FILE__, 406, __PRETTY_FUNCTION__).warning().nospace() << error; | 0 | ||||||||||||
| 332 | return; never executed: return; | 0 | ||||||||||||
| 333 | } | - | ||||||||||||
| 334 | - | |||||||||||||
| 335 | if (!v4->hasException
| 2-1152 | ||||||||||||
| 336 | program->run(); executed 1152 times by 5 tests: program->run();Executed by:
| 1152 | ||||||||||||
| 337 | - | |||||||||||||
| 338 | if (v4->hasException
| 4-1150 | ||||||||||||
| 339 | QQmlError error = v4->catchExceptionAsQmlError(); | - | ||||||||||||
| 340 | reportScriptException(script, error); | - | ||||||||||||
| 341 | } executed 4 times by 1 test: end of blockExecuted by:
| 4 | ||||||||||||
| 342 | } executed 1154 times by 5 tests: end of blockExecuted by:
| 1154 | ||||||||||||
| 343 | - | |||||||||||||
| 344 | void QQuickWorkerScriptEnginePrivate::reportScriptException(WorkerScript *script, | - | ||||||||||||
| 345 | const QQmlError &error) | - | ||||||||||||
| 346 | { | - | ||||||||||||
| 347 | QQuickWorkerScriptEnginePrivate *p = QQuickWorkerScriptEnginePrivate::get(workerEngine); | - | ||||||||||||
| 348 | - | |||||||||||||
| 349 | QMutexLocker locker(&p->m_lock); | - | ||||||||||||
| 350 | if (script->owner
| 0-8 | ||||||||||||
| 351 | QCoreApplication::postEvent(script->owner, new WorkerErrorEvent(error)); executed 8 times by 1 test: QCoreApplication::postEvent(script->owner, new WorkerErrorEvent(error));Executed by:
| 8 | ||||||||||||
| 352 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||
| 353 | - | |||||||||||||
| 354 | WorkerDataEvent::WorkerDataEvent(int workerId, const QByteArray &data) | - | ||||||||||||
| 355 | : QEvent((QEvent::Type)WorkerData), m_id(workerId), m_data(data) | - | ||||||||||||
| 356 | { | - | ||||||||||||
| 357 | } executed 2154 times by 5 tests: end of blockExecuted by:
| 2154 | ||||||||||||
| 358 | - | |||||||||||||
| 359 | WorkerDataEvent::~WorkerDataEvent() | - | ||||||||||||
| 360 | { | - | ||||||||||||
| 361 | } | - | ||||||||||||
| 362 | - | |||||||||||||
| 363 | int WorkerDataEvent::workerId() const | - | ||||||||||||
| 364 | { | - | ||||||||||||
| 365 | return executed 1182 times by 5 tests: m_id;return m_id;Executed by:
executed 1182 times by 5 tests: return m_id;Executed by:
| 1182 | ||||||||||||
| 366 | } | - | ||||||||||||
| 367 | - | |||||||||||||
| 368 | QByteArray WorkerDataEvent::data() const | - | ||||||||||||
| 369 | { | - | ||||||||||||
| 370 | return executed 2154 times by 5 tests: m_data;return m_data;Executed by:
executed 2154 times by 5 tests: return m_data;Executed by:
| 2154 | ||||||||||||
| 371 | } | - | ||||||||||||
| 372 | - | |||||||||||||
| 373 | WorkerLoadEvent::WorkerLoadEvent(int workerId, const QUrl &url) | - | ||||||||||||
| 374 | : QEvent((QEvent::Type)WorkerLoad), m_id(workerId), m_url(url) | - | ||||||||||||
| 375 | { | - | ||||||||||||
| 376 | } executed 1154 times by 5 tests: end of blockExecuted by:
| 1154 | ||||||||||||
| 377 | - | |||||||||||||
| 378 | int WorkerLoadEvent::workerId() const | - | ||||||||||||
| 379 | { | - | ||||||||||||
| 380 | return executed 1154 times by 5 tests: m_id;return m_id;Executed by:
executed 1154 times by 5 tests: return m_id;Executed by:
| 1154 | ||||||||||||
| 381 | } | - | ||||||||||||
| 382 | - | |||||||||||||
| 383 | QUrl WorkerLoadEvent::url() const | - | ||||||||||||
| 384 | { | - | ||||||||||||
| 385 | return executed 1154 times by 5 tests: m_url;return m_url;Executed by:
executed 1154 times by 5 tests: return m_url;Executed by:
| 1154 | ||||||||||||
| 386 | } | - | ||||||||||||
| 387 | - | |||||||||||||
| 388 | WorkerRemoveEvent::WorkerRemoveEvent(int workerId) | - | ||||||||||||
| 389 | : QEvent((QEvent::Type)WorkerRemove), m_id(workerId) | - | ||||||||||||
| 390 | { | - | ||||||||||||
| 391 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 392 | - | |||||||||||||
| 393 | int WorkerRemoveEvent::workerId() const | - | ||||||||||||
| 394 | { | - | ||||||||||||
| 395 | return executed 1152 times by 5 tests: m_id;return m_id;Executed by:
executed 1152 times by 5 tests: return m_id;Executed by:
| 1152 | ||||||||||||
| 396 | } | - | ||||||||||||
| 397 | - | |||||||||||||
| 398 | WorkerErrorEvent::WorkerErrorEvent(const QQmlError &error) | - | ||||||||||||
| 399 | : QEvent((QEvent::Type)WorkerError), m_error(error) | - | ||||||||||||
| 400 | { | - | ||||||||||||
| 401 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||
| 402 | - | |||||||||||||
| 403 | QQmlError WorkerErrorEvent::error() const | - | ||||||||||||
| 404 | { | - | ||||||||||||
| 405 | return executed 8 times by 1 test: m_error;return m_error;Executed by:
executed 8 times by 1 test: return m_error;Executed by:
| 8 | ||||||||||||
| 406 | } | - | ||||||||||||
| 407 | - | |||||||||||||
| 408 | QQuickWorkerScriptEngine::QQuickWorkerScriptEngine(QQmlEngine *parent) | - | ||||||||||||
| 409 | : QThread(parent), d(new QQuickWorkerScriptEnginePrivate(parent)) | - | ||||||||||||
| 410 | { | - | ||||||||||||
| 411 | d->m_lock.lock(); | - | ||||||||||||
| 412 | connect(d, qFlagLocation("2""stopThread()" "\0" __FILE__ ":" "487"), this, qFlagLocation("1""quit()" "\0" __FILE__ ":" "487"), Qt::DirectConnection); | - | ||||||||||||
| 413 | start(QThread::LowestPriority); | - | ||||||||||||
| 414 | d->m_wait.wait(&d->m_lock); | - | ||||||||||||
| 415 | d->moveToThread(this); | - | ||||||||||||
| 416 | d->m_lock.unlock(); | - | ||||||||||||
| 417 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 418 | - | |||||||||||||
| 419 | QQuickWorkerScriptEngine::~QQuickWorkerScriptEngine() | - | ||||||||||||
| 420 | { | - | ||||||||||||
| 421 | d->m_lock.lock(); | - | ||||||||||||
| 422 | QCoreApplication::postEvent(d, new QEvent((QEvent::Type)QQuickWorkerScriptEnginePrivate::WorkerDestroyEvent)); | - | ||||||||||||
| 423 | d->m_lock.unlock(); | - | ||||||||||||
| 424 | - | |||||||||||||
| 425 | - | |||||||||||||
| 426 | - | |||||||||||||
| 427 | - | |||||||||||||
| 428 | while (!isFinished()
| 1110-136696 | ||||||||||||
| 429 | - | |||||||||||||
| 430 | - | |||||||||||||
| 431 | QCoreApplication::processEvents(); | - | ||||||||||||
| 432 | yieldCurrentThread(); | - | ||||||||||||
| 433 | } executed 136696 times by 5 tests: end of blockExecuted by:
| 136696 | ||||||||||||
| 434 | - | |||||||||||||
| 435 | d->deleteLater(); | - | ||||||||||||
| 436 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 437 | - | |||||||||||||
| 438 | QQuickWorkerScriptEnginePrivate::WorkerScript::WorkerScript() | - | ||||||||||||
| 439 | : id(-1), initialized(false), owner(nullptr) | - | ||||||||||||
| 440 | { | - | ||||||||||||
| 441 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 442 | - | |||||||||||||
| 443 | QQuickWorkerScriptEnginePrivate::WorkerScript::~WorkerScript() | - | ||||||||||||
| 444 | { | - | ||||||||||||
| 445 | } | - | ||||||||||||
| 446 | - | |||||||||||||
| 447 | int QQuickWorkerScriptEngine::registerWorkerScript(QQuickWorkerScript *owner) | - | ||||||||||||
| 448 | { | - | ||||||||||||
| 449 | typedef QQuickWorkerScriptEnginePrivate::WorkerScript WorkerScript; | - | ||||||||||||
| 450 | WorkerScript *script = new WorkerScript; | - | ||||||||||||
| 451 | - | |||||||||||||
| 452 | script->id = d->m_nextId++; | - | ||||||||||||
| 453 | script->owner = owner; | - | ||||||||||||
| 454 | - | |||||||||||||
| 455 | d->m_lock.lock(); | - | ||||||||||||
| 456 | d->workers.insert(script->id, script); | - | ||||||||||||
| 457 | d->m_lock.unlock(); | - | ||||||||||||
| 458 | - | |||||||||||||
| 459 | return executed 1152 times by 5 tests: script->id;return script->id;Executed by:
executed 1152 times by 5 tests: return script->id;Executed by:
| 1152 | ||||||||||||
| 460 | } | - | ||||||||||||
| 461 | - | |||||||||||||
| 462 | void QQuickWorkerScriptEngine::removeWorkerScript(int id) | - | ||||||||||||
| 463 | { | - | ||||||||||||
| 464 | QQuickWorkerScriptEnginePrivate::WorkerScript* script = d->workers.value(id); | - | ||||||||||||
| 465 | if (script
| 0-1152 | ||||||||||||
| 466 | script->owner = nullptr; | - | ||||||||||||
| 467 | QCoreApplication::postEvent(d, new WorkerRemoveEvent(id)); | - | ||||||||||||
| 468 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 469 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 470 | - | |||||||||||||
| 471 | void QQuickWorkerScriptEngine::executeUrl(int id, const QUrl &url) | - | ||||||||||||
| 472 | { | - | ||||||||||||
| 473 | QCoreApplication::postEvent(d, new WorkerLoadEvent(id, url)); | - | ||||||||||||
| 474 | } executed 1154 times by 5 tests: end of blockExecuted by:
| 1154 | ||||||||||||
| 475 | - | |||||||||||||
| 476 | void QQuickWorkerScriptEngine::sendMessage(int id, const QByteArray &data) | - | ||||||||||||
| 477 | { | - | ||||||||||||
| 478 | QCoreApplication::postEvent(d, new WorkerDataEvent(id, data)); | - | ||||||||||||
| 479 | } executed 1182 times by 5 tests: end of blockExecuted by:
| 1182 | ||||||||||||
| 480 | - | |||||||||||||
| 481 | void QQuickWorkerScriptEngine::run() | - | ||||||||||||
| 482 | { | - | ||||||||||||
| 483 | d->m_lock.lock(); | - | ||||||||||||
| 484 | - | |||||||||||||
| 485 | d->workerEngine = new QQuickWorkerScriptEnginePrivate::WorkerEngine(d); | - | ||||||||||||
| 486 | d->workerEngine->init(); | - | ||||||||||||
| 487 | - | |||||||||||||
| 488 | d->m_wait.wakeAll(); | - | ||||||||||||
| 489 | - | |||||||||||||
| 490 | d->m_lock.unlock(); | - | ||||||||||||
| 491 | - | |||||||||||||
| 492 | exec(); | - | ||||||||||||
| 493 | - | |||||||||||||
| 494 | qDeleteAll(d->workers); | - | ||||||||||||
| 495 | d->workers.clear(); | - | ||||||||||||
| 496 | - | |||||||||||||
| 497 | delete d->workerEngine; d->workerEngine = nullptr; | - | ||||||||||||
| 498 | } executed 1110 times by 5 tests: end of blockExecuted by:
| 1110 | ||||||||||||
| 499 | QQuickWorkerScript::QQuickWorkerScript(QObject *parent) | - | ||||||||||||
| 500 | : QObject(parent), m_engine(nullptr), m_scriptId(-1), m_componentComplete(true) | - | ||||||||||||
| 501 | { | - | ||||||||||||
| 502 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 503 | - | |||||||||||||
| 504 | QQuickWorkerScript::~QQuickWorkerScript() | - | ||||||||||||
| 505 | { | - | ||||||||||||
| 506 | if (m_scriptId != -1
executed 1152 times by 5 tests: m_engine->removeWorkerScript(m_scriptId);Executed by:
| 0-1152 | ||||||||||||
| 507 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 508 | - | |||||||||||||
| 509 | - | |||||||||||||
| 510 | - | |||||||||||||
| 511 | - | |||||||||||||
| 512 | - | |||||||||||||
| 513 | - | |||||||||||||
| 514 | - | |||||||||||||
| 515 | QUrl QQuickWorkerScript::source() const | - | ||||||||||||
| 516 | { | - | ||||||||||||
| 517 | return executed 2 times by 1 test: m_source;return m_source;Executed by:
executed 2 times by 1 test: return m_source;Executed by:
| 2 | ||||||||||||
| 518 | } | - | ||||||||||||
| 519 | - | |||||||||||||
| 520 | void QQuickWorkerScript::setSource(const QUrl &source) | - | ||||||||||||
| 521 | { | - | ||||||||||||
| 522 | if (m_source == source
| 0-1154 | ||||||||||||
| 523 | return; never executed: return; | 0 | ||||||||||||
| 524 | - | |||||||||||||
| 525 | m_source = source; | - | ||||||||||||
| 526 | - | |||||||||||||
| 527 | if (engine()
| 2-1152 | ||||||||||||
| 528 | m_engine->executeUrl(m_scriptId, m_source); executed 2 times by 1 test: m_engine->executeUrl(m_scriptId, m_source);Executed by:
| 2 | ||||||||||||
| 529 | - | |||||||||||||
| 530 | sourceChanged(); | - | ||||||||||||
| 531 | } executed 1154 times by 5 tests: end of blockExecuted by:
| 1154 | ||||||||||||
| 532 | void QQuickWorkerScript::sendMessage(QQmlV4Function *args) | - | ||||||||||||
| 533 | { | - | ||||||||||||
| 534 | if (!engine()
| 4-1182 | ||||||||||||
| 535 | QMessageLogger(__FILE__, 679, __PRETTY_FUNCTION__).warning("QQuickWorkerScript: Attempt to send message before WorkerScript establishment"); | - | ||||||||||||
| 536 | return; executed 4 times by 1 test: return;Executed by:
| 4 | ||||||||||||
| 537 | } | - | ||||||||||||
| 538 | - | |||||||||||||
| 539 | QV4::Scope scope(args->v4engine()); | - | ||||||||||||
| 540 | QV4::ScopedValue argument(scope, QV4::Primitive::undefinedValue()); | - | ||||||||||||
| 541 | if (args->length() != 0
| 0-1182 | ||||||||||||
| 542 | argument = (*args)[0]; executed 1182 times by 5 tests: argument = (*args)[0];Executed by:
| 1182 | ||||||||||||
| 543 | - | |||||||||||||
| 544 | m_engine->sendMessage(m_scriptId, QV4::Serialize::serialize(argument, scope.engine)); | - | ||||||||||||
| 545 | } executed 1182 times by 5 tests: end of blockExecuted by:
| 1182 | ||||||||||||
| 546 | - | |||||||||||||
| 547 | void QQuickWorkerScript::classBegin() | - | ||||||||||||
| 548 | { | - | ||||||||||||
| 549 | m_componentComplete = false; | - | ||||||||||||
| 550 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 551 | - | |||||||||||||
| 552 | QQuickWorkerScriptEngine *QQuickWorkerScript::engine() | - | ||||||||||||
| 553 | { | - | ||||||||||||
| 554 | if (m_engine
executed 1184 times by 5 tests: m_engine;return m_engine;Executed by:
executed 1184 times by 5 tests: return m_engine;Executed by:
| 1184-2308 | ||||||||||||
| 555 | if (m_componentComplete
| 1152-1156 | ||||||||||||
| 556 | QQmlEngine *engine = qmlEngine(this); | - | ||||||||||||
| 557 | if (!engine
| 0-1152 | ||||||||||||
| 558 | QMessageLogger(__FILE__, 702, __PRETTY_FUNCTION__).warning("QQuickWorkerScript: engine() called without qmlEngine() set"); | - | ||||||||||||
| 559 | return never executed: nullptr;return nullptr;never executed: return nullptr; | 0 | ||||||||||||
| 560 | } | - | ||||||||||||
| 561 | - | |||||||||||||
| 562 | m_engine = QQmlEnginePrivate::get(engine)->getWorkerScriptEngine(); | - | ||||||||||||
| 563 | m_scriptId = m_engine->registerWorkerScript(this); | - | ||||||||||||
| 564 | - | |||||||||||||
| 565 | if (m_source.isValid()
| 0-1152 | ||||||||||||
| 566 | m_engine->executeUrl(m_scriptId, m_source); executed 1152 times by 5 tests: m_engine->executeUrl(m_scriptId, m_source);Executed by:
| 1152 | ||||||||||||
| 567 | - | |||||||||||||
| 568 | return executed 1152 times by 5 tests: m_engine;return m_engine;Executed by:
executed 1152 times by 5 tests: return m_engine;Executed by:
| 1152 | ||||||||||||
| 569 | } | - | ||||||||||||
| 570 | return executed 1156 times by 5 tests: nullptr;return nullptr;Executed by:
executed 1156 times by 5 tests: return nullptr;Executed by:
| 1156 | ||||||||||||
| 571 | } | - | ||||||||||||
| 572 | - | |||||||||||||
| 573 | void QQuickWorkerScript::componentComplete() | - | ||||||||||||
| 574 | { | - | ||||||||||||
| 575 | m_componentComplete = true; | - | ||||||||||||
| 576 | engine(); | - | ||||||||||||
| 577 | } executed 1152 times by 5 tests: end of blockExecuted by:
| 1152 | ||||||||||||
| 578 | bool QQuickWorkerScript::event(QEvent *event) | - | ||||||||||||
| 579 | { | - | ||||||||||||
| 580 | if (event->type() == (QEvent::Type)WorkerDataEvent::WorkerData
| 8-972 | ||||||||||||
| 581 | QQmlEngine *engine = qmlEngine(this); | - | ||||||||||||
| 582 | if (engine
| 0-972 | ||||||||||||
| 583 | WorkerDataEvent *workerEvent = static_cast<WorkerDataEvent *>(event); | - | ||||||||||||
| 584 | QV4::Scope scope(engine->handle()); | - | ||||||||||||
| 585 | QV4::ScopedValue value(scope, QV4::Serialize::deserialize(workerEvent->data(), scope.engine)); | - | ||||||||||||
| 586 | message(QQmlV4Handle(value)); | - | ||||||||||||
| 587 | } executed 972 times by 4 tests: end of blockExecuted by:
| 972 | ||||||||||||
| 588 | return executed 972 times by 4 tests: true;return true;Executed by:
executed 972 times by 4 tests: return true;Executed by:
| 972 | ||||||||||||
| 589 | } else if (event->type() == (QEvent::Type)WorkerErrorEvent::WorkerError
| 0-8 | ||||||||||||
| 590 | WorkerErrorEvent *workerEvent = static_cast<WorkerErrorEvent *>(event); | - | ||||||||||||
| 591 | QQmlEnginePrivate::warning(qmlEngine(this), workerEvent->error()); | - | ||||||||||||
| 592 | return executed 8 times by 1 test: true;return true;Executed by:
executed 8 times by 1 test: return true;Executed by:
| 8 | ||||||||||||
| 593 | } else { | - | ||||||||||||
| 594 | return never executed: QObject::event(event);return QObject::event(event);never executed: return QObject::event(event); | 0 | ||||||||||||
| 595 | } | - | ||||||||||||
| 596 | } | - | ||||||||||||
| 597 | - | |||||||||||||
| 598 | - | |||||||||||||
| 599 | - | |||||||||||||
| 600 | - | |||||||||||||
| Switch to Source code | Preprocessed file |