diff options
Diffstat (limited to 'src/qmlworkerscript')
-rw-r--r-- | src/qmlworkerscript/dependencies.json | 2 | ||||
-rw-r--r-- | src/qmlworkerscript/qmlworkerscript.pro | 6 | ||||
-rw-r--r-- | src/qmlworkerscript/qqmlworkerscriptmodule.cpp | 7 | ||||
-rw-r--r-- | src/qmlworkerscript/qqmlworkerscriptmodule_p.h | 1 | ||||
-rw-r--r-- | src/qmlworkerscript/qquickworkerscript.cpp | 15 | ||||
-rw-r--r-- | src/qmlworkerscript/qquickworkerscript_p.h | 7 | ||||
-rw-r--r-- | src/qmlworkerscript/qtqmlworkerscriptglobal_p.h | 2 | ||||
-rw-r--r-- | src/qmlworkerscript/qv4serialize.cpp | 49 |
8 files changed, 62 insertions, 27 deletions
diff --git a/src/qmlworkerscript/dependencies.json b/src/qmlworkerscript/dependencies.json new file mode 100644 index 0000000000..0d4f101c7a --- /dev/null +++ b/src/qmlworkerscript/dependencies.json @@ -0,0 +1,2 @@ +[ +] diff --git a/src/qmlworkerscript/qmlworkerscript.pro b/src/qmlworkerscript/qmlworkerscript.pro index 9f5e0e809a..84466062e1 100644 --- a/src/qmlworkerscript/qmlworkerscript.pro +++ b/src/qmlworkerscript/qmlworkerscript.pro @@ -19,4 +19,10 @@ SOURCES += \ include(../3rdparty/masm/masm-defs.pri) +QMLTYPES_FILENAME = plugins.qmltypes +QMLTYPES_INSTALL_DIR = $$[QT_INSTALL_QML]/QtQml/WorkerScript.2 +QML_IMPORT_NAME = QtQml.WorkerScript +IMPORT_VERSION = 2.$$QT_MINOR_VERSION +CONFIG += qmltypes install_qmltypes install_metatypes + load(qt_module) diff --git a/src/qmlworkerscript/qqmlworkerscriptmodule.cpp b/src/qmlworkerscript/qqmlworkerscriptmodule.cpp index 98e82dbeba..f6ad5b87e8 100644 --- a/src/qmlworkerscript/qqmlworkerscriptmodule.cpp +++ b/src/qmlworkerscript/qqmlworkerscriptmodule.cpp @@ -47,16 +47,11 @@ QT_BEGIN_NAMESPACE void QQmlWorkerScriptModule::registerQuickTypes() { // Don't add anything here. These are only for backwards compatibility. + // Also, don't convert to qmlRegisterTypesAndRevisions as that will add future revisions. const char uri[] = "QtQuick"; qmlRegisterType<QQuickWorkerScript>(uri, 2, 0, "WorkerScript"); } #endif // QT_VERSION < QT_VERSION_CHECK(6, 0, 0) -void QQmlWorkerScriptModule::defineModule() -{ - const char uri[] = "QtQml.WorkerScript"; - qmlRegisterType<QQuickWorkerScript>(uri, 2, 0, "WorkerScript"); -} - QT_END_NAMESPACE diff --git a/src/qmlworkerscript/qqmlworkerscriptmodule_p.h b/src/qmlworkerscript/qqmlworkerscriptmodule_p.h index a2efb304c1..b7748d12a0 100644 --- a/src/qmlworkerscript/qqmlworkerscriptmodule_p.h +++ b/src/qmlworkerscript/qqmlworkerscriptmodule_p.h @@ -61,7 +61,6 @@ public: #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) static void registerQuickTypes(); #endif - static void defineModule(); }; QT_END_NAMESPACE diff --git a/src/qmlworkerscript/qquickworkerscript.cpp b/src/qmlworkerscript/qquickworkerscript.cpp index 635d508daf..20294d7ba7 100644 --- a/src/qmlworkerscript/qquickworkerscript.cpp +++ b/src/qmlworkerscript/qquickworkerscript.cpp @@ -548,6 +548,17 @@ void QQuickWorkerScript::setSource(const QUrl &source) } /*! + \qmlproperty bool WorkerScript::ready + + This holds whether the WorkerScript has been initialized and is ready + for receiving messages via \tt WorkerScript.sendMessage(). +*/ +bool QQuickWorkerScript::ready() const +{ + return m_engine != nullptr; +} + +/*! \qmlmethod WorkerScript::sendMessage(jsobject message) Sends the given \a message to a worker script handler in another @@ -607,6 +618,8 @@ QQuickWorkerScriptEngine *QQuickWorkerScript::engine() if (m_source.isValid()) m_engine->executeUrl(m_scriptId, m_source); + emit readyChanged(); + return m_engine; } return nullptr; @@ -623,8 +636,6 @@ void QQuickWorkerScript::componentComplete() This signal is emitted when a message \a msg is received from a worker script in another thread through a call to sendMessage(). - - The corresponding handler is \c onMessage. */ bool QQuickWorkerScript::event(QEvent *event) diff --git a/src/qmlworkerscript/qquickworkerscript_p.h b/src/qmlworkerscript/qquickworkerscript_p.h index 87cf2e9754..e56ce84b6d 100644 --- a/src/qmlworkerscript/qquickworkerscript_p.h +++ b/src/qmlworkerscript/qquickworkerscript_p.h @@ -53,6 +53,7 @@ #include <qqml.h> +#include <QtQmlWorkerScript/private/qtqmlworkerscriptglobal_p.h> #include <QtQml/qqmlparserstatus.h> #include <QtCore/qthread.h> #include <QtQml/qjsvalue.h> @@ -87,6 +88,9 @@ class Q_AUTOTEST_EXPORT QQuickWorkerScript : public QObject, public QQmlParserSt { Q_OBJECT Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(bool ready READ ready NOTIFY readyChanged REVISION 15) + + QML_NAMED_ELEMENT(WorkerScript); Q_INTERFACES(QQmlParserStatus) public: @@ -96,11 +100,14 @@ public: QUrl source() const; void setSource(const QUrl &); + bool ready() const; + public Q_SLOTS: void sendMessage(QQmlV4Function*); Q_SIGNALS: void sourceChanged(); + Q_REVISION(15) void readyChanged(); void message(const QJSValue &messageObject); protected: diff --git a/src/qmlworkerscript/qtqmlworkerscriptglobal_p.h b/src/qmlworkerscript/qtqmlworkerscriptglobal_p.h index 34236cd79e..c75d5f3129 100644 --- a/src/qmlworkerscript/qtqmlworkerscriptglobal_p.h +++ b/src/qmlworkerscript/qtqmlworkerscriptglobal_p.h @@ -57,4 +57,6 @@ #define Q_QMLWORKERSCRIPT_PRIVATE_EXPORT Q_QMLWORKERSCRIPT_EXPORT #define Q_QMLWORKERSCRIPT_AUTOTEST_EXPORT Q_AUTOTEST_EXPORT +void Q_QMLWORKERSCRIPT_PRIVATE_EXPORT qml_register_types_QtQml_WorkerScript(); + #endif // QTQMLWORKERSCRIPTGLOBAL_P_H diff --git a/src/qmlworkerscript/qv4serialize.cpp b/src/qmlworkerscript/qv4serialize.cpp index a5e62d3e35..f0a644a8b8 100644 --- a/src/qmlworkerscript/qv4serialize.cpp +++ b/src/qmlworkerscript/qv4serialize.cpp @@ -81,6 +81,7 @@ enum Type { WorkerDate, WorkerRegexp, WorkerListModel, + WorkerUrl, #if QT_CONFIG(qml_sequence_object) WorkerSequence #endif @@ -142,10 +143,29 @@ static inline void *popPtr(const char *&data) return rv; } +#define ALIGN(size) (((size) + 3) & ~3) +static inline void serializeString(QByteArray &data, const QString &str, Type type) +{ + int length = str.length(); + if (length > 0xFFFFFF) { + push(data, valueheader(WorkerUndefined)); + return; + } + int utf16size = ALIGN(length * sizeof(quint16)); + + reserve(data, utf16size + sizeof(quint32)); + push(data, valueheader(type, length)); + + int offset = data.size(); + data.resize(data.size() + utf16size); + char *buffer = data.data() + offset; + + memcpy(buffer, str.constData(), length*sizeof(QChar)); +} + // XXX TODO: Check that worker script is exception safe in the case of // serialization/deserialization failures -#define ALIGN(size) (((size) + 3) & ~3) void Serialize::serialize(QByteArray &data, const QV4::Value &v, ExecutionEngine *engine) { QV4::Scope scope(engine); @@ -159,22 +179,7 @@ void Serialize::serialize(QByteArray &data, const QV4::Value &v, ExecutionEngine } else if (v.isBoolean()) { push(data, valueheader(v.booleanValue() == true ? WorkerTrue : WorkerFalse)); } else if (v.isString()) { - const QString &qstr = v.toQString(); - int length = qstr.length(); - if (length > 0xFFFFFF) { - push(data, valueheader(WorkerUndefined)); - return; - } - int utf16size = ALIGN(length * sizeof(quint16)); - - reserve(data, utf16size + sizeof(quint32)); - push(data, valueheader(WorkerString, length)); - - int offset = data.size(); - data.resize(data.size() + utf16size); - char *buffer = data.data() + offset; - - memcpy(buffer, qstr.constData(), length*sizeof(QChar)); + serializeString(data, v.toQString(), WorkerString); } else if (v.as<FunctionObject>()) { // XXX TODO: Implement passing function objects between the main and // worker scripts @@ -259,6 +264,11 @@ void Serialize::serialize(QByteArray &data, const QV4::Value &v, ExecutionEngine return; } #endif + const QVariant variant = engine->toVariant(v, QMetaType::QUrl, false); + if (variant.userType() == QMetaType::QUrl) { + serializeString(data, variant.value<QUrl>().toString(), WorkerUrl); + return; + } // regular object QV4::ScopedValue val(scope, v); @@ -340,11 +350,14 @@ ReturnedValue Serialize::deserialize(const char *&data, ExecutionEngine *engine) case WorkerFalse: return QV4::Encode(false); case WorkerString: + case WorkerUrl: { quint32 size = headersize(header); QString qstr((const QChar *)data, size); data += ALIGN(size * sizeof(quint16)); - return QV4::Encode(engine->newString(qstr)); + return (type == WorkerUrl) + ? engine->fromVariant(QVariant::fromValue(QUrl(qstr))) + : Encode(engine->newString(qstr)); } case WorkerFunction: Q_ASSERT(!"Unreachable"); |