aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/qml/doc/snippets/qml/workerscript/script.mjs (renamed from src/qml/doc/snippets/qml/workerscript/script.js)0
-rw-r--r--src/qml/doc/snippets/qml/workerscript/workerscript.qml2
-rw-r--r--src/qml/types/qquickworkerscript.cpp55
-rw-r--r--tests/auto/qml/qquickworkerscript/data/messagehandler.mjs4
-rw-r--r--tests/auto/qml/qquickworkerscript/data/script_module.mjs5
-rw-r--r--tests/auto/qml/qquickworkerscript/tst_qquickworkerscript.cpp22
6 files changed, 62 insertions, 26 deletions
diff --git a/src/qml/doc/snippets/qml/workerscript/script.js b/src/qml/doc/snippets/qml/workerscript/script.mjs
index f55dee3507..f55dee3507 100644
--- a/src/qml/doc/snippets/qml/workerscript/script.js
+++ b/src/qml/doc/snippets/qml/workerscript/script.mjs
diff --git a/src/qml/doc/snippets/qml/workerscript/workerscript.qml b/src/qml/doc/snippets/qml/workerscript/workerscript.qml
index e3fe3c92e6..cc637d34cf 100644
--- a/src/qml/doc/snippets/qml/workerscript/workerscript.qml
+++ b/src/qml/doc/snippets/qml/workerscript/workerscript.qml
@@ -61,7 +61,7 @@ Rectangle {
WorkerScript {
id: myWorker
- source: "script.js"
+ source: "script.mjs"
onMessage: myText.text = messageObject.reply
}
diff --git a/src/qml/types/qquickworkerscript.cpp b/src/qml/types/qquickworkerscript.cpp
index c4fc9a0532..4fef0fab7c 100644
--- a/src/qml/types/qquickworkerscript.cpp
+++ b/src/qml/types/qquickworkerscript.cpp
@@ -268,21 +268,31 @@ void QQuickWorkerScriptEnginePrivate::processLoad(int id, const QUrl &url)
return;
QV4::ExecutionEngine *v4 = QV8Engine::getV4(script);
- QV4::Scope scope(v4);
- QScopedPointer<QV4::Script> program;
script->source = url;
- QString error;
- program.reset(QV4::Script::createFromFileOrCache(v4, /*qml context*/nullptr, fileName, url, &error));
- if (program.isNull()) {
- if (!error.isEmpty())
- qWarning().nospace() << error;
- return;
- }
+ if (fileName.endsWith(QLatin1String(".mjs"))) {
+ auto moduleUnit = v4->loadModule(url);
+ if (moduleUnit) {
+ if (moduleUnit->instantiate(v4))
+ moduleUnit->evaluate();
+ } else {
+ v4->throwError(QStringLiteral("Could not load module file"));
+ }
+ } else {
+ QString error;
+ QV4::Scope scope(v4);
+ QScopedPointer<QV4::Script> program;
+ program.reset(QV4::Script::createFromFileOrCache(v4, /*qmlContext*/nullptr, fileName, url, &error));
+ if (program.isNull()) {
+ if (!error.isEmpty())
+ qWarning().nospace() << error;
+ return;
+ }
- if (!v4->hasException)
- program->run();
+ if (!v4->hasException)
+ program->run();
+ }
if (v4->hasException) {
QQmlError error = v4->catchExceptionAsQmlError();
@@ -484,22 +494,28 @@ void QQuickWorkerScriptEngine::run()
\snippet qml/workerscript/workerscript.qml 0
- The above worker script specifies a JavaScript file, "script.js", that handles
- the operations to be performed in the new thread. Here is \c script.js:
+ The above worker script specifies a JavaScript file, "script.mjs", that handles
+ the operations to be performed in the new thread. Here is \c script.mjs:
- \quotefile qml/workerscript/script.js
+ \quotefile qml/workerscript/script.mjs
When the user clicks anywhere within the rectangle, \c sendMessage() is
called, triggering the \tt WorkerScript.onMessage() handler in
- \tt script.js. This in turn sends a reply message that is then received
+ \tt script.mjs. This in turn sends a reply message that is then received
by the \tt onMessage() handler of \tt myWorker.
+ The example uses a script that is an ECMAScript module, because it has the ".mjs" extension.
+ It can use import statements to access functionality from other modules and it is run in JavaScript
+ strict mode.
+
+ If a worker script has the extension ".js" instead, then it is considered to contain plain JavaScript
+ statements and it is run in non-strict mode.
\section3 Restrictions
Since the \c WorkerScript.onMessage() function is run in a separate thread, the
JavaScript file is evaluated in a context separate from the main QML engine. This means
- that unlike an ordinary JavaScript file that is imported into QML, the \c script.js
+ that unlike an ordinary JavaScript file that is imported into QML, the \c script.mjs
in the above example cannot access the properties, methods or other attributes
of the QML item, nor can it access any context properties set on the QML object
through QQmlContext.
@@ -507,7 +523,8 @@ void QQuickWorkerScriptEngine::run()
Additionally, there are restrictions on the types of values that can be passed to and
from the worker script. See the sendMessage() documentation for details.
- Worker script can not use \l {qtqml-javascript-imports.html}{.import} syntax.
+ Worker scripts that are plain JavaScript sources can not use \l {qtqml-javascript-imports.html}{.import} syntax.
+ Scripts that are ECMAScript modules can freely use import and export statements.
\sa {Qt Quick Examples - Threading},
{Threaded ListModel Example}
@@ -527,6 +544,10 @@ QQuickWorkerScript::~QQuickWorkerScript()
This holds the url of the JavaScript file that implements the
\tt WorkerScript.onMessage() handler for threaded operations.
+
+ If the file name component of the url ends with ".mjs", then the script
+ is parsed as an ECMAScript module and run in strict mode. Otherwise it is considered to be
+ plain script.
*/
QUrl QQuickWorkerScript::source() const
{
diff --git a/tests/auto/qml/qquickworkerscript/data/messagehandler.mjs b/tests/auto/qml/qquickworkerscript/data/messagehandler.mjs
new file mode 100644
index 0000000000..749ff561da
--- /dev/null
+++ b/tests/auto/qml/qquickworkerscript/data/messagehandler.mjs
@@ -0,0 +1,4 @@
+
+export function messageHandler(msg) {
+ WorkerScript.sendMessage("Hello from the module")
+}
diff --git a/tests/auto/qml/qquickworkerscript/data/script_module.mjs b/tests/auto/qml/qquickworkerscript/data/script_module.mjs
new file mode 100644
index 0000000000..eaa191c5a7
--- /dev/null
+++ b/tests/auto/qml/qquickworkerscript/data/script_module.mjs
@@ -0,0 +1,5 @@
+
+import { messageHandler as handler } from "./messagehandler.mjs";
+
+WorkerScript.onMessage = handler
+
diff --git a/tests/auto/qml/qquickworkerscript/tst_qquickworkerscript.cpp b/tests/auto/qml/qquickworkerscript/tst_qquickworkerscript.cpp
index e21a53b4f3..4ad58ba56c 100644
--- a/tests/auto/qml/qquickworkerscript/tst_qquickworkerscript.cpp
+++ b/tests/auto/qml/qquickworkerscript/tst_qquickworkerscript.cpp
@@ -77,24 +77,30 @@ private:
void tst_QQuickWorkerScript::source()
{
QQmlComponent component(&m_engine, testFileUrl("worker.qml"));
- QQuickWorkerScript *worker = qobject_cast<QQuickWorkerScript*>(component.create());
+ QScopedPointer<QQuickWorkerScript>worker(qobject_cast<QQuickWorkerScript*>(component.create()));
QVERIFY(worker != nullptr);
const QMetaObject *mo = worker->metaObject();
QVariant value(100);
- QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
- waitForEchoMessage(worker);
- QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), value);
+ QVERIFY(QMetaObject::invokeMethod(worker.data(), "testSend", Q_ARG(QVariant, value)));
+ waitForEchoMessage(worker.data());
+ QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.data()).value<QVariant>(), value);
QUrl source = testFileUrl("script_fixed_return.js");
worker->setSource(source);
QCOMPARE(worker->source(), source);
- QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value)));
- waitForEchoMessage(worker);
- QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value<QVariant>(), qVariantFromValue(QString("Hello_World")));
+ QVERIFY(QMetaObject::invokeMethod(worker.data(), "testSend", Q_ARG(QVariant, value)));
+ waitForEchoMessage(worker.data());
+ QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.data()).value<QVariant>(), qVariantFromValue(QString("Hello_World")));
+
+ source = testFileUrl("script_module.mjs");
+ worker->setSource(source);
+ QCOMPARE(worker->source(), source);
+ QVERIFY(QMetaObject::invokeMethod(worker.data(), "testSend", Q_ARG(QVariant, value)));
+ waitForEchoMessage(worker.data());
+ QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker.data()).value<QVariant>(), qVariantFromValue(QString("Hello from the module")));
qApp->processEvents();
- delete worker;
}
void tst_QQuickWorkerScript::messaging()