aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4engine_p.h
diff options
context:
space:
mode:
authorAlex Shaw <alex.shaw.as@gmail.com>2021-04-25 17:57:37 -0400
committerAlex Shaw <alex.shaw.as@gmail.com>2021-05-01 17:26:00 -0400
commit3464655f5e2adaa1aed8925a9a54b8fb5f238f31 (patch)
treeebb7c46c7d4f1af6e4b1853a74d7502fee3c450f /src/qml/jsruntime/qv4engine_p.h
parent689522817dc559056f7c55f811a189c4821b7787 (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/jsruntime/qv4engine_p.h')
-rw-r--r--src/qml/jsruntime/qv4engine_p.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h
index e3e0a481cf..fe31b42c84 100644
--- a/src/qml/jsruntime/qv4engine_p.h
+++ b/src/qml/jsruntime/qv4engine_p.h
@@ -760,9 +760,17 @@ public:
mutable QMutex moduleMutex;
QHash<QUrl, QQmlRefPointer<ExecutableCompilationUnit>> modules;
+
+ // QV4::PersistentValue would be preferred, but using QHash will create copies,
+ // and QV4::PersistentValue doesn't like creating copies.
+ // Instead, we allocate a raw pointer using the same manual memory management
+ // technique in QV4::PersistentValue.
+ QHash<QUrl, QV4::Value*> nativeModules;
+
void injectModule(const QQmlRefPointer<ExecutableCompilationUnit> &moduleUnit);
QQmlRefPointer<ExecutableCompilationUnit> moduleForUrl(const QUrl &_url, const ExecutableCompilationUnit *referrer = nullptr) const;
QQmlRefPointer<ExecutableCompilationUnit> loadModule(const QUrl &_url, const ExecutableCompilationUnit *referrer = nullptr);
+ void registerModule(const QString &name, const QJSValue &module);
bool diskCacheEnabled() const;