Skip to content

Commit

Permalink
Scripting: Document the .mjs treatment and fixed some issues
Browse files Browse the repository at this point in the history
* Make __filename also available in .mjs files
* Fixed reporting of errors that happen during importing of modules
  • Loading branch information
bjorn committed Feb 4, 2022
1 parent 994492e commit 8ebab81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/reference/scripting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ An extension can be placed either directly in an extensions directory, or in a
sub-directory. All scripts files found in these directories are executed on
startup.

.. raw:: html

<div class="new">Since Tiled 1.8</div>

When using the ``.mjs`` extension, script files are loaded as `JavaScript
modules`_. They will then be able to use the `import`_ and `export`_ statements
to split up their functionality over multiple JavaScript files. Such extensions
also don't pollute the global scope, avoiding potential name collisions between
different extensions.

When any loaded script is changed or when any files are added/removed from the
extensions directory, the script engine is automatically reinstantiated and the
scripts are reloaded. This way there is no need to restart Tiled when
Expand Down Expand Up @@ -121,3 +131,6 @@ __filename
.. _List of JavaScript Objects and Functions: https://doc.qt.io/qt-5/qtqml-javascript-functionlist.html
.. _QML module: https://doc.qt.io/qt-5/qtqml-index.html
.. _@mapeditor/tiled-api: https://www.npmjs.com/package/@mapeditor/tiled-api
.. _JavaScript modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
.. _import: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
.. _export: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
22 changes: 21 additions & 1 deletion src/tiled/scriptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,27 @@ void ScriptManager::loadExtension(const QString &path)
}
#else
Tiled::INFO(tr("Importing module '%1'").arg(absolutePath));
checkError(mEngine->importModule(absolutePath));

QJSValue globalObject = mEngine->globalObject();
globalObject.setProperty(QStringLiteral("__filename"), absolutePath);

QJSValue result = mEngine->importModule(absolutePath);

// According to the documentation, importModule could return an
// error object, though in practice this doesn't appear to happen.
if (!checkError(result)) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
// This appears to be the way to report exceptions
checkError(mEngine->catchError());
#else
// With Qt 5 it seems we need to get a little creative, like
// calling evaluate to let that catch a potentially raised
// exception.
checkError(mEngine->evaluate(QString()));
#endif
}

globalObject.deleteProperty(QStringLiteral("__filename"));
#endif
}
mWatcher.addPath(absolutePath);
Expand Down

0 comments on commit 8ebab81

Please sign in to comment.