From b8e9ce6b4ecda30ad838c1d3465c428591071a66 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 15 Aug 2018 16:43:13 +0200 Subject: Add API to QJSEngine for importing ECMAScript modules Now that the standard defines the concept of a module, it makes sense to offer a function in QJSEngine that can read files and load them. [ChangeLog][QtQml][QJSEngine] Added function to import ECMASCript modules from the file system or the Qt resource system. Change-Id: I72f8d49de948872221ac1b54fcfb066404bed9b9 Reviewed-by: Lars Knoll Reviewed-by: Paul Wicking --- src/qml/jsapi/qjsengine.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/qml/jsapi/qjsengine.h | 2 ++ 2 files changed, 51 insertions(+) (limited to 'src') diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index 5fa81ccc2a..d20f7eb97c 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include @@ -481,6 +482,54 @@ QJSValue QJSEngine::evaluate(const QString& program, const QString& fileName, in return retval; } +static QUrl moduleUrlForFileName(const QString &fileName) +{ + QString absolutePath = QFileInfo(fileName).canonicalFilePath(); + if (!absolutePath.startsWith(QLatin1Char(':'))) + return QUrl::fromLocalFile(absolutePath); + + absolutePath.remove(0, 1); + QUrl url; + url.setPath(absolutePath); + url.setScheme(QLatin1String("qrc")); + return url; +} + +/*! + Imports the module located at \a fileName and returns a module namespace object that + contains all exported variables, constants and functions as properties. + + If this is the first time the module is imported in the engine, the file is loaded + from the specified location in either the local file system or the Qt resource system + and evaluated as an ECMAScript module. The file is expected to be encoded in UTF-8 text. + + Subsequent imports of the same module will return the previously imported instance. Modules + are singletons and remain around until the engine is destroyed. + + The specified \a fileName will internally be normalized using \a QFileInfo::canonicalFilePath(). + That means that multiple imports of the same file on disk using different relative paths will + load the file only once. + + \note If an exception is thrown during the loading of the module, the return value + will be the exception (typically an \c{Error} object; see QJSValue::isError()). + + \since 5.12 + */ +QJSValue QJSEngine::importModule(const QString &fileName) +{ + const QUrl url = moduleUrlForFileName(fileName); + auto moduleUnit = m_v4Engine->loadModule(url); + if (m_v4Engine->hasException) + return QJSValue(m_v4Engine, m_v4Engine->catchException()); + + QV4::Scope scope(m_v4Engine); + QV4::Scoped moduleNamespace(scope, moduleUnit->instantiate(m_v4Engine)); + if (m_v4Engine->hasException) + return QJSValue(m_v4Engine, m_v4Engine->catchException()); + moduleUnit->evaluate(); + return QJSValue(m_v4Engine, moduleNamespace->asReturnedValue()); +} + /*! Creates a JavaScript object of class Object. diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index 36a3e475f2..5c8613ffd6 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -69,6 +69,8 @@ public: QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1); + QJSValue importModule(const QString &fileName); + QJSValue newObject(); QJSValue newArray(uint length = 0); -- cgit v1.2.3