diff options
author | Alex Shaw <alex.shaw.as@gmail.com> | 2021-04-25 17:57:37 -0400 |
---|---|---|
committer | Alex Shaw <alex.shaw.as@gmail.com> | 2021-05-01 17:26:00 -0400 |
commit | 3464655f5e2adaa1aed8925a9a54b8fb5f238f31 (patch) | |
tree | ebb7c46c7d4f1af6e4b1853a74d7502fee3c450f /src/qml/jsapi/qjsengine.h | |
parent | 689522817dc559056f7c55f811a189c4821b7787 (diff) |
Add QJSEngine::registerModule
Some applications that use JavaScript as a scripting language may want
to extend JS through C++ code. The current way to do that is with
global objects.
ES6 provides a better way of encapsulating code: modules.
registerModule() allows an application to provide a QJSValue as a named module.
Developers familiar with Node.js will find this very easy to use.
Example:
```c++
QJSValue num(666);
myEngine.registerModule("themarkofthebeast", num);
```
```js
import badnews from "themarkofthebeast";
```
[ChangeLog][QtQml][QJSEngine] Adds the ability to register QJSValues in
C++ as modules for importing in MJS files.
Change-Id: I0c98dcb746aa2aa15aa2ab3082129d106413a23b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/jsapi/qjsengine.h')
-rw-r--r-- | src/qml/jsapi/qjsengine.h | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/src/qml/jsapi/qjsengine.h b/src/qml/jsapi/qjsengine.h index ac79b30ebc..84730afe2b 100644 --- a/src/qml/jsapi/qjsengine.h +++ b/src/qml/jsapi/qjsengine.h @@ -71,6 +71,7 @@ public: QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1, QStringList *exceptionStackTrace = nullptr); QJSValue importModule(const QString &fileName); + bool registerModule(const QString &moduleName, const QJSValue &value); QJSValue newObject(); QJSValue newSymbol(const QString &name); |