significant cleanup of ScriptEngineQtScript (which isn't exposed to the scripting engine and shouldn't be @jsdoc'ed)

This commit is contained in:
Heather Anderson 2021-09-05 23:36:22 -07:00 committed by ksuprynowicz
parent ef16b83362
commit bd4a035ffb
5 changed files with 17 additions and 288 deletions

View file

@ -116,6 +116,7 @@ public:
virtual QStringList uncaughtExceptionBacktrace() const = 0;
virtual int uncaughtExceptionLineNumber() const = 0;
virtual void updateMemoryCost(const qint64& deltaSize) = 0;
virtual void requestCollectGarbage() = 0;
public:
// helper to detect and log warnings when other code invokes QScriptEngine/BaseScriptEngine in thread-unsafe ways

View file

@ -2470,3 +2470,7 @@ QString ScriptManager::formatException(const ScriptValue& exception, bool includ
ScriptValue ScriptManager::evaluate(const QString& program, const QString& fileName) {
return _engine->evaluate(program, fileName);
}
void ScriptManager::requestGarbageCollection() {
_engine->requestCollectGarbage();
}

View file

@ -407,7 +407,7 @@ public:
QVariantMap fetchModuleSource(const QString& modulePath, const bool forceDownload = false);
ScriptValue instantiateModule(const ScriptValue& module, const QString& sourceCode);
Q_INVOKABLE ScriptValue evaluate(const QString& program, const QString& fileName = QString());
ScriptValue evaluate(const QString& program, const QString& fileName = QString());
/**jsdoc
* Calls a function repeatedly, at a set interval.
@ -531,6 +531,13 @@ public:
QFuture<QVariant> getLocalEntityScriptDetails(const EntityItemID& entityID) override;
/**jsdoc
* Manually runs the JavaScript garbage collector which reclaims memory by disposing of objects that are no longer
* reachable.
* @function Script.requestGarbageCollection
*/
Q_INVOKABLE void requestGarbageCollection();
/*@jsdoc
* @function Script.loadEntityScript
* @param {Uuid} entityID - Entity ID.
* @param {string} script - Script.
@ -718,14 +725,6 @@ signals:
*/
void finished(const QString& fileNameString, ScriptManagerPointer);
/**jsdoc
* @function Script.cleanupMenuItem
* @param {string} menuItem - Menu item.
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
void cleanupMenuItem(const QString& menuItemString);
/**jsdoc
* Triggered when the script prints a message to the program log via {@link print}, {@link Script.print},
* {@link console.log}, {@link console.debug}, {@link console.group}, {@link console.groupEnd}, {@link console.time}, or

View file

@ -354,37 +354,10 @@ ScriptEngineQtScript::ScriptEngineQtScript(ScriptManager* scriptManager) :
setAgent(_contextAgent);
}
bool ScriptEngineQtScript::isDebugMode() const {
#if defined(DEBUG)
return true;
#else
return false;
#endif
}
ScriptEngineQtScript::~ScriptEngineQtScript() {
delete _contextAgent;
}
void ScriptEngineQtScript::disconnectNonEssentialSignals() {
disconnect();
QThread* workerThread;
// Ensure the thread should be running, and does exist
if (_isRunning && _isThreaded && (workerThread = QScriptEngine::thread())) {
connect(this, &QObject::destroyed, workerThread, &QThread::quit);
connect(workerThread, &QThread::finished, workerThread, &QObject::deleteLater);
}
}
void ScriptEngineQtScript::executeOnScriptThread(std::function<void()> function, const Qt::ConnectionType& type ) {
if (QThread::currentThread() != QScriptEngine::thread()) {
QMetaObject::invokeMethod(this, "executeOnScriptThread", type, Q_ARG(std::function<void()>, function));
return;
}
function();
}
void ScriptEngineQtScript::registerEnum(const QString& enumName, QMetaEnum newEnum) {
if (!newEnum.isValid()) {
qCCritical(scriptengine) << "registerEnum called on invalid enum with name " << enumName;
@ -768,16 +741,6 @@ void ScriptEngineQtScript::updateMemoryCost(const qint64& deltaSize) {
}
}
void ScriptEngineQtScript::print(const QString& message) {
QString filename;
auto scriptManager = manager();
if (scriptManager) {
filename = scriptManager->getFilename();
}
emit printedMessage(message, filename);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ScriptEngine implementation

View file

@ -25,7 +25,6 @@
#include <QtCore/QString>
#include <QtScript/QScriptEngine>
#include <QtScriptTools/QScriptEngineDebugger>
#include "../ScriptEngine.h"
#include "../ScriptManager.h"
@ -97,19 +96,14 @@ public: // ScriptEngine implementation
virtual ScriptValue uncaughtException() const override;
virtual QStringList uncaughtExceptionBacktrace() const override;
virtual int uncaughtExceptionLineNumber() const override;
virtual void updateMemoryCost(const qint64& deltaSize) override;
virtual void requestCollectGarbage() override { collectGarbage(); }
// helper to detect and log warnings when other code invokes QScriptEngine/BaseScriptEngine in thread-unsafe ways
inline bool IS_THREADSAFE_INVOCATION(const QString& method) { return ScriptEngine::IS_THREADSAFE_INVOCATION(method); }
protected: // brought over from BaseScriptEngine
/**jsdoc
* @function Script.makeError
* @param {object} [other] - Other.
* @param {string} [type="Error"] - Error.
* @returns {object} Object.
* @deprecated This function is deprecated and will be removed.
*/
Q_INVOKABLE QScriptValue makeError(const QScriptValue& other = QScriptValue(), const QString& type = "Error");
QScriptValue makeError(const QScriptValue& other = QScriptValue(), const QString& type = "Error");
// if the currentContext() is valid then throw the passed exception; otherwise, immediately emit it.
// note: this is used in cases where C++ code might call into JS API methods directly
@ -126,23 +120,9 @@ public:
// NOTE - these are NOT intended to be public interfaces available to scripts, the are only Q_INVOKABLE so we can
// properly ensure they are only called on the correct thread
/**jsdoc
* @function Script.registerGlobalObject
* @param {string} name - Name.
* @param {object} object - Object.
* @deprecated This function is deprecated and will be removed.
*/
/// registers a global object by name
Q_INVOKABLE virtual void registerGlobalObject(const QString& name, QObject* object) override;
/**jsdoc
* @function Script.registerGetterSetter
* @param {string} name - Name.
* @param {function} getter - Getter.
* @param {function} setter - Setter.
* @param {string} [parent=""] - Parent.
* @deprecated This function is deprecated and will be removed.
*/
/// registers a global getter/setter
Q_INVOKABLE void registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter,
QScriptEngine::FunctionSignature setter, const QString& parent = QString(""));
@ -152,13 +132,6 @@ public:
ScriptEngine::FunctionSignature setter,
const QString& parent = QString("")) override;
/**jsdoc
* @function Script.registerFunction
* @param {string} name - Name.
* @param {function} function - Function.
* @param {number} [numArguments=-1] - Number of arguments.
* @deprecated This function is deprecated and will be removed.
*/
/// register a global function
Q_INVOKABLE void registerFunction(const QString& name, QScriptEngine::FunctionSignature fun, int numArguments = -1);
@ -167,14 +140,6 @@ public:
ScriptEngine::FunctionSignature fun,
int numArguments = -1) override;
/**jsdoc
* @function Script.registerFunction
* @param {string} parent - Parent.
* @param {string} name - Name.
* @param {function} function - Function.
* @param {number} [numArguments=-1] - Number of arguments.
* @deprecated This function is deprecated and will be removed.
*/
/// register a function as a method on a previously registered global object
Q_INVOKABLE void registerFunction(const QString& parent, const QString& name, QScriptEngine::FunctionSignature fun,
int numArguments = -1);
@ -185,34 +150,14 @@ public:
ScriptEngine::FunctionSignature fun,
int numArguments = -1) override;
/**jsdoc
* @function Script.registerEnum
* @param {string} name - Name.
* @param {object} enum - Enum.
* @deprecated This function is deprecated and will be removed.
*/
// WARNING: This function must be called after a registerGlobalObject that creates the namespace this enum is located in, or
// the globalObject won't function. E.g., if you have a Foo object and a Foo.FooType enum, Foo must be registered first.
/// registers a global enum
Q_INVOKABLE virtual void registerEnum(const QString& enumName, QMetaEnum newEnum) override;
/**jsdoc
* @function Script.registerValue
* @param {string} name - Name.
* @param {object} value - Value.
* @deprecated This function is deprecated and will be removed.
*/
/// registers a global object by name
Q_INVOKABLE void registerValue(const QString& valueName, QScriptValue value);
/**jsdoc
* @function Script.evaluate
* @param {string} program - Program.
* @param {string} filename - File name.
* @param {number} [lineNumber=-1] - Line number.
* @returns {object} Object.
* @deprecated This function is deprecated and will be removed.
*/
/// evaluate some code in the context of the ScriptEngineQtScript and return the result
Q_INVOKABLE virtual ScriptValue evaluate(const QString& program,
const QString& fileName) override; // this is also used by the script tool widget
@ -220,177 +165,10 @@ public:
Q_INVOKABLE virtual ScriptValue evaluate(const ScriptProgramPointer& program) override;
/**jsdoc
* @function Script.evaluateInClosure
* @param {object} locals - Locals.
* @param {object} program - Program.
* @returns {object} Object.
* @deprecated This function is deprecated and will be removed.
*/
Q_INVOKABLE virtual ScriptValue evaluateInClosure(const ScriptValue& locals, const ScriptProgramPointer& program) override;
/**jsdoc
* Checks whether the application was compiled as a debug build.
* @function Script.isDebugMode
* @returns {boolean} <code>true</code> if the application was compiled as a debug build, <code>false</code> if it was
* compiled as a release build.
*/
Q_INVOKABLE bool isDebugMode() const;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MODULE related methods
/**jsdoc
* Prints a message to the program log and emits {@link Script.printedMessage}.
* <p>Alternatively, you can use {@link print} or one of the {@link console} API methods.</p>
* @function Script.print
* @param {string} message - The message to print.
*/
Q_INVOKABLE void print(const QString& message);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Entity Script Related methods
/**jsdoc
* Manually runs the JavaScript garbage collector which reclaims memory by disposing of objects that are no longer
* reachable.
* @function Script.requestGarbageCollection
*/
Q_INVOKABLE void requestGarbageCollection() { collectGarbage(); }
bool isRunning() const { return _isRunning; } // used by ScriptWidget
bool isDebuggable() const { return _debuggable; }
void disconnectNonEssentialSignals();
// NOTE - this is used by the TypedArray implementation. we need to review this for thread safety
ArrayBufferClass* getArrayBufferClass() { return _arrayBufferClass; }
/**jsdoc
* @function Script.updateMemoryCost
* @param {number} deltaSize - Delta size.
* @deprecated This function is deprecated and will be removed.
*/
virtual void updateMemoryCost(const qint64& deltaSize) override;
signals:
/**jsdoc
* Triggered frequently at a system-determined interval.
* @function Script.update
* @param {number} deltaTime - The time since the last update, in s.
* @returns {Signal}
* @example <caption>Report script update intervals.</caption>
* Script.update.connect(function (deltaTime) {
* print("Update: " + deltaTime);
* });
*/
void update(float deltaTime);
/**jsdoc
* @function Script.finished
* @param {string} filename - File name.
* @param {object} engine - Engine.
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
void finished(const QString& fileNameString, ScriptEngineQtScriptPointer);
/**jsdoc
* @function Script.cleanupMenuItem
* @param {string} menuItem - Menu item.
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
void cleanupMenuItem(const QString& menuItemString);
/**jsdoc
* Triggered when the script prints a message to the program log via {@link print}, {@link Script.print},
* {@link console.log}, {@link console.debug}, {@link console.group}, {@link console.groupEnd}, {@link console.time}, or
* {@link console.timeEnd}.
* @function Script.printedMessage
* @param {string} message - The message.
* @param {string} scriptName - The name of the script that generated the message.
* @returns {Signal}
*/
void printedMessage(const QString& message, const QString& scriptName);
/**jsdoc
* Triggered when the script generates an error, {@link console.error} or {@link console.exception} is called, or
* {@link console.assert} is called and fails.
* @function Script.errorMessage
* @param {string} message - The error message.
* @param {string} scriptName - The name of the script that generated the error message.
* @returns {Signal}
*/
void errorMessage(const QString& message, const QString& scriptName);
/**jsdoc
* Triggered when the script generates a warning or {@link console.warn} is called.
* @function Script.warningMessage
* @param {string} message - The warning message.
* @param {string} scriptName - The name of the script that generated the warning message.
* @returns {Signal}
*/
void warningMessage(const QString& message, const QString& scriptName);
/**jsdoc
* Triggered when the script generates an information message or {@link console.info} is called.
* @function Script.infoMessage
* @param {string} message - The information message.
* @param {string} scriptName - The name of the script that generated the information message.
* @returns {Signal}
*/
void infoMessage(const QString& message, const QString& scriptName);
/**jsdoc
* Triggered when the running state of the script changes, e.g., from running to stopping.
* @function Script.runningStateChanged
* @returns {Signal}
*/
void runningStateChanged();
/**jsdoc
* @function Script.clearDebugWindow
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
void clearDebugWindow();
/**jsdoc
* @function Script.loadScript
* @param {string} scriptName - Script name.
* @param {boolean} isUserLoaded - Is user loaded.
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
void loadScript(const QString& scriptName, bool isUserLoaded);
/**jsdoc
* @function Script.reloadScript
* @param {string} scriptName - Script name.
* @param {boolean} isUserLoaded - Is user loaded.
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
void reloadScript(const QString& scriptName, bool isUserLoaded);
/**jsdoc
* Triggered when the script has stopped.
* @function Script.doneRunning
* @returns {Signal}
*/
void doneRunning();
/**jsdoc
* @function Script.entityScriptDetailsUpdated
* @returns {Signal}
* @deprecated This signal is deprecated and will be removed.
*/
// Emitted when an entity script is added or removed, or when the status of an entity
// script is updated (goes from RUNNING to ERROR_RUNNING_SCRIPT, for example)
void entityScriptDetailsUpdated();
inline ArrayBufferClass* getArrayBufferClass() { return _arrayBufferClass; }
public: // not for public use, but I don't like how Qt strings this along with private friend functions
virtual ScriptValue create(int type, const void* ptr) override;
@ -409,15 +187,6 @@ protected:
const QScriptEngine::ValueOwnership& ownership = QScriptEngine::AutoOwnership);
protected:
/**jsdoc
* @function Script.executeOnScriptThread
* @param {function} function - Function.
* @param {ConnectionType} [type=2] - Connection type.
* @deprecated This function is deprecated and will be removed.
*/
Q_INVOKABLE void executeOnScriptThread(std::function<void()> function, const Qt::ConnectionType& type = Qt::QueuedConnection );
QPointer<ScriptManager> _manager;
int _nextCustomType = 0;
@ -425,13 +194,6 @@ protected:
ScriptValue _undefinedValue;
mutable ScriptContextQtPointer _currContext;
std::atomic<bool> _isRunning { false };
bool _isThreaded { false };
QScriptEngineDebugger* _debugger { nullptr };
bool _debuggable { false };
qint64 _lastUpdate;
ArrayBufferClass* _arrayBufferClass;
ScriptContextQtAgent* _contextAgent{ nullptr };