Showing the changes to plugins/JSAPIExample/src/JSAPIExample.cpp (1 of 260 files). Show all
Unified · Split
| @@ -19,12 +19,15 @@ · 18 unmodified lines | ||
| 19 | 19 | #include <QtCore/QLoggingCategory> |
| 20 | 20 | #include <QtCore/QThread> |
| 21 | 21 | #include <QtCore/QTimer> |
| 22 | −#include <QtScript/QScriptEngine> | |
| 23 | −#include <QtScript/QScriptable> | |
| 24 | 22 | |
| 25 | 23 | #include <SettingHelpers.h> // for ::settingsFilename() |
| 26 | 24 | #include <SharedUtil.h> // for ::usecTimestampNow() |
| 27 | 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 | 32 | // NOTE: replace this with your own namespace when starting a new plugin (to avoid .so/.dll symbol clashes) |
| 30 | 33 | namespace REPLACE_ME_WITH_UNIQUE_NAME { |
| @@ -34,9 +37,9 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 3 unmodified lines | ||
| 34 | 37 | |
| 35 | 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 | 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 | 43 | context->throwError(message); |
| 41 | 44 | } else { |
| 42 | 45 | // otherwise just log the error |
| @@ -47,7 +50,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 4 unmodified lines | ||
| 47 | 50 | |
| 48 | 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 | 54 | Q_OBJECT |
| 52 | 55 | Q_PLUGIN_METADATA(IID "JSAPIExample" FILE "plugin.json") |
| 53 | 56 | Q_PROPERTY(QString version MEMBER _version CONSTANT) |
| @@ -60,9 +63,9 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 6 unmodified lines | ||
| 60 | 63 | return; |
| 61 | 64 | } |
| 62 | 65 | 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); | |
| 66 | 69 | // qCDebug(logger) << "setGlobalInstance" << objectName() << engine->property("fileName"); |
| 67 | 70 | }); |
| 68 | 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 | 74 | // NOTES: everything within the "public slots:" section below will be available from JS via overall plugin QObject |
| 72 | 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 | 77 | // (as such this plugin class and its methods remain forward-compatible with other engines like QML's QJSEngine) |
| 75 | 78 | |
| 76 | 79 | public slots: |
| @@ -146,7 +149,7 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 69 unmodified lines | ||
| 146 | 149 | |
| 147 | 150 | /** |
| 148 | 151 | * 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 | 153 | * it will be automatically cleaned up once no longer reachable from any JS variables/closures. |
| 151 | 154 | * @example <caption>access persistent settings stored in separate .json files</caption> |
| 152 | 155 | * var settings = JSAPIExample.getScopedSettings("example"); |
| @@ -157,18 +160,22 @@ namespace REPLACE_ME_WITH_UNIQUE_NAME { · 4 unmodified lines | ||
| 157 | 160 | * print("all example::* keys", settings.allKeys()); |
| 158 | 161 | * 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) { | |
| 161 | − auto engine = QScriptable::engine(); | |
| 163 | + ScriptValuePointer getScopedSettings(const QString& scope) { | |
| 164 | + auto engine = Scriptable::engine(); | |
| 162 | 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 | 172 | QString error; |
| 166 | − auto cppValue = createScopedSettings(scope, engine, error); | |
| 173 | + auto cppValue = createScopedSettings(scope, manager, error); | |
| 167 | 174 | if (!cppValue) { |
| 168 | 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 | 181 | private: |