History

isolate calls to the QtScript libraries to an interface we control

Heather Anderson committed 5 years ago

Commit 1e018db1 parent 713b29e260 files · +6904 −5064Browse files

Showing the changes to plugins/JSAPIExample/src/JSAPIExample.cpp (1 of 260 files). Show all

Unified · Split

plugins/JSAPIExample/src/JSAPIExample.cpp+23−16
@@ -19,12 +19,15 @@ · 18 unmodified lines
19#include <QtCore/QLoggingCategory>19#include <QtCore/QLoggingCategory>
20#include <QtCore/QThread>20#include <QtCore/QThread>
21#include <QtCore/QTimer>21#include <QtCore/QTimer>
22#include <QtScript/QScriptEngine>
23#include <QtScript/QScriptable>
24 22
25#include <SettingHelpers.h> // for ::settingsFilename()23#include <SettingHelpers.h> // for ::settingsFilename()
26#include <SharedUtil.h> // for ::usecTimestampNow()24#include <SharedUtil.h> // for ::usecTimestampNow()
27#include <shared/ScriptInitializerMixin.h>25#include <shared/ScriptInitializerMixin.h>
26#include <ScriptContext.h>
27#include <ScriptEngine.h>
28#include <ScriptManager.h>
29#include <ScriptValue.h>
30#include <Scriptable.h>
28 31
29// NOTE: replace this with your own namespace when starting a new plugin (to avoid .so/.dll symbol clashes)32// NOTE: replace this with your own namespace when starting a new plugin (to avoid .so/.dll symbol clashes)
30namespace REPLACE_ME_WITH_UNIQUE_NAME {33namespace REPLACE_ME_WITH_UNIQUE_NAME {
@@ -34,9 +37,9 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 3 unmodified lines
34 37
35 QLoggingCategory logger { "jsapiexample" };38 QLoggingCategory logger { "jsapiexample" };
36 39
37 inline QVariant raiseScriptingError(QScriptContext* context, const QString& message, const QVariant& returnValue = QVariant()) {40 inline QVariant raiseScriptingError(ScriptContext* context, const QString& message, const QVariant& returnValue = QVariant()) {
38 if (context) {41 if (context) {
39 // when a QScriptContext is available throw an actual JS Exception (which can be caught using try/catch on JS side)42 // when a ScriptContext is available throw an actual JS Exception (which can be caught using try/catch on JS side)
40 context->throwError(message);43 context->throwError(message);
41 } else {44 } else {
42 // otherwise just log the error45 // otherwise just log the error
@@ -47,7 +50,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 4 unmodified lines
47 50
48 QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error);51 QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error);
49 52
50 class JSAPIExample : public QObject, public QScriptable {53 class JSAPIExample : public QObject, public Scriptable {
51 Q_OBJECT54 Q_OBJECT
52 Q_PLUGIN_METADATA(IID "JSAPIExample" FILE "plugin.json")55 Q_PLUGIN_METADATA(IID "JSAPIExample" FILE "plugin.json")
53 Q_PROPERTY(QString version MEMBER _version CONSTANT)56 Q_PROPERTY(QString version MEMBER _version CONSTANT)
@@ -60,9 +63,9 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 6 unmodified lines
60 return;63 return;
61 }64 }
62 qCWarning(logger) << "registering w/ScriptInitializerMixin..." << scriptInit.data();65 qCWarning(logger) << "registering w/ScriptInitializerMixin..." << scriptInit.data();
63 scriptInit->registerScriptInitializer([this](QScriptEngine* engine) {66 scriptInit->registerScriptInitializer([this](ScriptEngine* engine) {
64 auto value = engine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater);67 auto value = engine->newQObject(this, ScriptEngine::QtOwnership, ScriptEngine::ExcludeDeleteLater);
65 engine->globalObject().setProperty(objectName(), value);68 engine->globalObject()->setProperty(objectName(), value);
66 // qCDebug(logger) << "setGlobalInstance" << objectName() << engine->property("fileName");69 // qCDebug(logger) << "setGlobalInstance" << objectName() << engine->property("fileName");
67 });70 });
68 // qCInfo(logger) << "plugin loaded" << qApp << toString() << QThread::currentThread();71 // qCInfo(logger) << "plugin loaded" << qApp << toString() << QThread::currentThread();
@@ -70,7 +73,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 1 unmodified line
70 73
71 // NOTES: everything within the "public slots:" section below will be available from JS via overall plugin QObject74 // NOTES: everything within the "public slots:" section below will be available from JS via overall plugin QObject
72 // also, to demonstrate future-proofing JS API code, QVariant's are used throughout most of these examples --75 // also, to demonstrate future-proofing JS API code, QVariant's are used throughout most of these examples --
73 // which still makes them very Qt-specific, but avoids depending directly on deprecated QtScript/QScriptValue APIs.76 // which still makes them very Qt-specific, but avoids depending directly on deprecated ScriptValuePointer APIs.
74 // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)77 // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)
75 78
76 public slots:79 public slots:
@@ -146,7 +149,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 69 unmodified lines
146 149
147 /**150 /**
148 * Example of exposing a custom "managed" C++ QObject to JS151 * Example of exposing a custom "managed" C++ QObject to JS
149 * The lifecycle of the created QObject* instance becomes managed by the invoking QScriptEngine --152 * The lifecycle of the created QObject* instance becomes managed by the invoking ScriptEngine --
150 * it will be automatically cleaned up once no longer reachable from any JS variables/closures.153 * it will be automatically cleaned up once no longer reachable from any JS variables/closures.
151 * @example <caption>access persistent settings stored in separate .json files</caption>154 * @example <caption>access persistent settings stored in separate .json files</caption>
152 * var settings = JSAPIExample.getScopedSettings("example");155 * var settings = JSAPIExample.getScopedSettings("example");
@@ -157,18 +160,22 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 4 unmodified lines
157 * print("all example::* keys", settings.allKeys());160 * print("all example::* keys", settings.allKeys());
158 * settings = null; // optional best pratice; allows the object to be reclaimed ASAP by the JS garbage collector161 * settings = null; // optional best pratice; allows the object to be reclaimed ASAP by the JS garbage collector
159 */162 */
160 QScriptValue getScopedSettings(const QString& scope) {163 ScriptValuePointer getScopedSettings(const QString& scope) {
161 auto engine = QScriptable::engine();164 auto engine = Scriptable::engine();
162 if (!engine) {165 if (!engine) {
163 return QScriptValue::NullValue;166 return ScriptValuePointer();
167 }
168 auto manager = engine->manager();
169 if (!manager) {
170 return ScriptValuePointer();
164 }171 }
165 QString error;172 QString error;
166 auto cppValue = createScopedSettings(scope, engine, error);173 auto cppValue = createScopedSettings(scope, manager, error);
167 if (!cppValue) {174 if (!cppValue) {
168 raiseScriptingError(context(), "error creating scoped settings instance: " + error);175 raiseScriptingError(context(), "error creating scoped settings instance: " + error);
169 return QScriptValue::NullValue;176 return engine->nullValue();
170 }177 }
171 return engine->newQObject(cppValue, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeDeleteLater);178 return engine->newQObject(cppValue, ScriptEngine::ScriptOwnership, ScriptEngine::ExcludeDeleteLater);
172 }179 }
173 180
174 private:181 private: