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
1919 #include <QtCore/QLoggingCategory>
2020 #include <QtCore/QThread>
2121 #include <QtCore/QTimer>
22−#include <QtScript/QScriptEngine>
23−#include <QtScript/QScriptable>
2422
2523 #include <SettingHelpers.h> // for ::settingsFilename()
2624 #include <SharedUtil.h> // for ::usecTimestampNow()
2725 #include <shared/ScriptInitializerMixin.h>
26+#include <ScriptContext.h>
27+#include <ScriptEngine.h>
28+#include <ScriptManager.h>
29+#include <ScriptValue.h>
30+#include <Scriptable.h>
2831
2932 // NOTE: replace this with your own namespace when starting a new plugin (to avoid .so/.dll symbol clashes)
3033 namespace REPLACE_ME_WITH_UNIQUE_NAME {
@@ -34,9 +37,9 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 3 unmodified lines
3437
3538 QLoggingCategory logger { "jsapiexample" };
3639
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()) {
3841 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)
4043 context->throwError(message);
4144 } else {
4245 // otherwise just log the error
@@ -47,7 +50,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 4 unmodified lines
4750
4851 QObject* createScopedSettings(const QString& scope, QObject* parent, QString& error);
4952
50− class JSAPIExample : public QObject, public QScriptable {
53+ class JSAPIExample : public QObject, public Scriptable {
5154 Q_OBJECT
5255 Q_PLUGIN_METADATA(IID "JSAPIExample" FILE "plugin.json")
5356 Q_PROPERTY(QString version MEMBER _version CONSTANT)
@@ -60,9 +63,9 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 6 unmodified lines
6063 return;
6164 }
6265 qCWarning(logger) << "registering w/ScriptInitializerMixin..." << scriptInit.data();
63− scriptInit->registerScriptInitializer([this](QScriptEngine* engine) {
64− auto value = engine->newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeDeleteLater);
65− engine->globalObject().setProperty(objectName(), value);
66+ scriptInit->registerScriptInitializer([this](ScriptEngine* engine) {
67+ auto value = engine->newQObject(this, ScriptEngine::QtOwnership, ScriptEngine::ExcludeDeleteLater);
68+ engine->globalObject()->setProperty(objectName(), value);
6669 // qCDebug(logger) << "setGlobalInstance" << objectName() << engine->property("fileName");
6770 });
6871 // qCInfo(logger) << "plugin loaded" << qApp << toString() << QThread::currentThread();
@@ -70,7 +73,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 1 unmodified line
7073
7174 // NOTES: everything within the "public slots:" section below will be available from JS via overall plugin QObject
7275 // 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.
7477 // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine)
7578
7679 public slots:
@@ -146,7 +149,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 69 unmodified lines
146149
147150 /**
148151 * 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 --
150153 * it will be automatically cleaned up once no longer reachable from any JS variables/closures.
151154 * @example <caption>access persistent settings stored in separate .json files</caption>
152155 * var settings = JSAPIExample.getScopedSettings("example");
@@ -157,18 +160,22 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 4 unmodified lines
157160 * print("all example::* keys", settings.allKeys());
158161 * settings = null; // optional best pratice; allows the object to be reclaimed ASAP by the JS garbage collector
159162 */
160− QScriptValue getScopedSettings(const QString& scope) {
161− auto engine = QScriptable::engine();
163+ ScriptValuePointer getScopedSettings(const QString& scope) {
164+ auto engine = Scriptable::engine();
162165 if (!engine) {
163− return QScriptValue::NullValue;
166+ return ScriptValuePointer();
167+ }
168+ auto manager = engine->manager();
169+ if (!manager) {
170+ return ScriptValuePointer();
164171 }
165172 QString error;
166− auto cppValue = createScopedSettings(scope, engine, error);
173+ auto cppValue = createScopedSettings(scope, manager, error);
167174 if (!cppValue) {
168175 raiseScriptingError(context(), "error creating scoped settings instance: " + error);
169− return QScriptValue::NullValue;
176+ return engine->nullValue();
170177 }
171− return engine->newQObject(cppValue, QScriptEngine::ScriptOwnership, QScriptEngine::ExcludeDeleteLater);
178+ return engine->newQObject(cppValue, ScriptEngine::ScriptOwnership, ScriptEngine::ExcludeDeleteLater);
172179 }
173180
174181 private: