From 1a4eb3ac62eb8d3d1ab71e5e15ef14dddc420810 Mon Sep 17 00:00:00 2001 From: Dale Glass Date: Thu, 13 Oct 2022 20:34:32 +0200 Subject: [PATCH] Keep return script value --- libraries/script-engine/src/ScriptManager.cpp | 10 ++-- libraries/script-engine/src/ScriptManager.h | 49 ++++++++++--------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/libraries/script-engine/src/ScriptManager.cpp b/libraries/script-engine/src/ScriptManager.cpp index e4273fe5ac..81fb31bdce 100644 --- a/libraries/script-engine/src/ScriptManager.cpp +++ b/libraries/script-engine/src/ScriptManager.cpp @@ -297,10 +297,10 @@ ScriptManager::ScriptManager(Context context, const QString& scriptContents, con } }); } - + if (!_areMetaTypesInitialized) { initMetaTypes(); - } + } } QString ScriptManager::getTypeAsString() const { @@ -826,9 +826,9 @@ void ScriptManager::run() { // (because we're a client script) hifi::scripting::setLocalAccessSafeThread(true); } - + _engine->enterIsolateOnThisThread(); - + auto filenameParts = _fileNameString.split("/"); auto name = filenameParts.size() > 0 ? filenameParts[filenameParts.size() - 1] : "unknown"; PROFILE_SET_THREAD_NAME("Script: " + name); @@ -848,7 +848,7 @@ void ScriptManager::run() { { PROFILE_RANGE(script, _fileNameString); - _engine->evaluate(_scriptContents, _fileNameString); + _returnValue = _engine->evaluate(_scriptContents, _fileNameString); _engine->maybeEmitUncaughtException(__FUNCTION__); } #ifdef _WIN32 diff --git a/libraries/script-engine/src/ScriptManager.h b/libraries/script-engine/src/ScriptManager.h index b777eb90e2..3d149c5e8d 100644 --- a/libraries/script-engine/src/ScriptManager.h +++ b/libraries/script-engine/src/ScriptManager.h @@ -216,7 +216,7 @@ public: * Script.setInterval(function () { * print("Hello"); * }, 1000); - * + * * Script.setTimeout(function () { * Script.stop(true); * }, 5000); @@ -254,7 +254,7 @@ public: /**jsdoc * Checks whether the script is running as an Interface or avatar script. * @function Script.isClientScript - * @returns {boolean} true if the script is running as an Interface or avatar script, false if it + * @returns {boolean} true if the script is running as an Interface or avatar script, false if it * isn't. */ Q_INVOKABLE bool isClientScript() const { return _context == CLIENT_SCRIPT; } @@ -262,7 +262,7 @@ public: /**jsdoc * Checks whether the application was compiled as a debug build. * @function Script.isDebugMode - * @returns {boolean} true if the application was compiled as a debug build, false if it was + * @returns {boolean} true if the application was compiled as a debug build, false if it was * compiled as a release build. */ Q_INVOKABLE bool isDebugMode() const; @@ -284,7 +284,7 @@ public: /**jsdoc * Checks whether the script is running as an assignment client script. * @function Script.isAgentScript - * @returns {boolean} true if the script is running as an assignment client script, false if it + * @returns {boolean} true if the script is running as an assignment client script, false if it * isn't. */ Q_INVOKABLE bool isAgentScript() const { return _context == AGENT_SCRIPT; } @@ -316,7 +316,7 @@ public: * @function Script.addEventHandler * @param {Uuid} entityID - The ID of the entity. * @param {Script.EntityEvent} eventName - The name of the event. - * @param {Script~entityEventCallback|Script~pointerEventCallback|Script~collisionEventCallback} handler - The function to + * @param {Script~entityEventCallback|Script~pointerEventCallback|Script~collisionEventCallback} handler - The function to * call when the event occurs on the entity. It can be either the name of a function or an in-line definition. * @example Report when a mouse press occurs on a particular entity. * var entityID = Entities.addEntity({ @@ -325,11 +325,11 @@ public: * dimensions: { x: 0.5, y: 0.5, z: 0.5 }, * lifetime: 300 // Delete after 5 minutes. * }); - * + * * function reportMousePress(entityID, event) { * print("Mouse pressed on entity: " + JSON.stringify(event)); * } - * + * * Script.addEventHandler(entityID, "mousePressOnEntity", reportMousePress); */ Q_INVOKABLE void addEventHandler(const EntityItemID& entityID, const QString& eventName, const ScriptValue& handler); @@ -345,7 +345,7 @@ public: Q_INVOKABLE void removeEventHandler(const EntityItemID& entityID, const QString& eventName, const ScriptValue& handler); /**jsdoc - * Starts running another script in Interface, if it isn't already running. The script is not automatically loaded next + * Starts running another script in Interface, if it isn't already running. The script is not automatically loaded next * time Interface starts. *

Supported Script Types: Interface Scripts • Avatar Scripts

*

See also, {@link ScriptDiscoveryService.loadScript}.

@@ -367,13 +367,13 @@ public: Q_INVOKABLE void load(const QString& loadfile); /**jsdoc - * Includes JavaScript from other files in the current script. If a callback is specified, the files are loaded and - * included asynchronously, otherwise they are included synchronously (i.e., script execution blocks while the files are + * Includes JavaScript from other files in the current script. If a callback is specified, the files are loaded and + * included asynchronously, otherwise they are included synchronously (i.e., script execution blocks while the files are * included). * @function Script.include * @variation 0 * @param {string[]} filenames - The URLs of the scripts to include. Each can be relative to the current script. - * @param {function} [callback=null] - The function to call back when the scripts have been included. It can be either the + * @param {function} [callback=null] - The function to call back when the scripts have been included. It can be either the * name of a function or an in-line definition. */ Q_INVOKABLE void include(const QStringList& includeFiles, const ScriptValue& callback = ScriptValue()); @@ -383,7 +383,7 @@ public: * asynchronously, otherwise it is included synchronously (i.e., script execution blocks while the file is included). * @function Script.include * @param {string} filename - The URL of the script to include. It can be relative to the current script. - * @param {function} [callback=null] - The function to call back when the script has been included. It can be either the + * @param {function} [callback=null] - The function to call back when the script has been included. It can be either the * name of a function or an in-line definition. * @example Include a script file asynchronously. * // First file: scriptA.js @@ -407,12 +407,12 @@ public: // MODULE related methods /**jsdoc - * Provides access to methods or objects provided in an external JavaScript or JSON file. + * Provides access to methods or objects provided in an external JavaScript or JSON file. * See {@link https://docs.overte.org/script/js-tips.html} for further details. * @function Script.require - * @param {string} module - The module to use. May be a JavaScript file, a JSON file, or the name of a system module such + * @param {string} module - The module to use. May be a JavaScript file, a JSON file, or the name of a system module such * as "appUi" (i.e., the "appUi.js" system module JavaScript file). - * @returns {object|array} The value assigned to module.exports in the JavaScript file, or the value defined + * @returns {object|array} The value assigned to module.exports in the JavaScript file, or the value defined * in the JSON file. */ Q_INVOKABLE ScriptValue require(const QString& moduleId); @@ -525,7 +525,7 @@ public: Q_INVOKABLE QUrl resourcesPath() const; /**jsdoc - * Starts timing a section of code in order to send usage data about it to Vircadia. Shouldn't be used outside of the + * Starts timing a section of code in order to send usage data about it to Vircadia. Shouldn't be used outside of the * standard scripts. * @function Script.beginProfileRange * @param {string} label - A name that identifies the section of code. @@ -533,7 +533,7 @@ public: Q_INVOKABLE void beginProfileRange(const QString& label) const; /**jsdoc - * Finishes timing a section of code in order to send usage data about it to Vircadia. Shouldn't be used outside of + * Finishes timing a section of code in order to send usage data about it to Vircadia. Shouldn't be used outside of * the standard scripts. * @function Script.endProfileRange * @param {string} label - A name that identifies the section of code. @@ -558,7 +558,7 @@ public: QFuture getLocalEntityScriptDetails(const EntityItemID& entityID) override; /**jsdoc - * Manually runs the JavaScript garbage collector which reclaims memory by disposing of objects that are no longer + * Manually runs the JavaScript garbage collector which reclaims memory by disposing of objects that are no longer * reachable. * @function Script.requestGarbageCollection */ @@ -683,9 +683,9 @@ public: * but this method will return the asset's current URL.) * @function Script.getExternalPath * @param {Script.ResourceBucket} bucket - The external resource bucket that the asset is in. - * @param {string} path - The path within the external resource bucket where the asset is located. + * @param {string} path - The path within the external resource bucket where the asset is located. *

Normally, this should start with a path or filename to be appended to the bucket URL. - * Alternatively, it can be a relative path starting with ./ or ../, to navigate within the + * Alternatively, it can be a relative path starting with ./ or ../, to navigate within the * resource bucket's URL.

* @Returns {string} The URL of an external asset. * @example Report the URL of a default particle. @@ -762,8 +762,8 @@ signals: void finished(const QString& fileNameString, ScriptManagerPointer); /**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 + * 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. @@ -773,7 +773,7 @@ signals: 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 + * 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. @@ -900,7 +900,7 @@ signals: protected: // Is called by the constructor, bceause all types need to be registered before method discovery with ScriptObjectV8Proxy::investigate() void initMetaTypes(); - + void init(); /**jsdoc @@ -968,6 +968,7 @@ protected: mutable QReadWriteLock _entityScriptsLock { QReadWriteLock::Recursive }; QHash _entityScripts; EntityScriptContentAvailableMap _contentAvailableQueue; + ScriptValue _returnValue; bool _isThreaded { false }; qint64 _lastUpdate;