Manages a single scripting engine. More...
#include <ScriptManager.h>
Inherits QObject, EntitiesScriptEngineProvider, and std::enable_shared_from_this< ScriptManager >.
Public Types | |
enum | Context { CLIENT_SCRIPT , ENTITY_CLIENT_SCRIPT , ENTITY_SERVER_SCRIPT , AGENT_SCRIPT , NETWORKLESS_TEST_SCRIPT } |
Context of the script. More... | |
enum | Type { CLIENT , ENTITY_CLIENT , ENTITY_SERVER , AGENT , AVATAR , NETWORKLESS_TEST } |
Type of the script. More... | |
Public Slots | |
void | updateMemoryCost (const qint64 &deltaSize) |
Script.updateMemoryCost. More... | |
Signals | |
void | attachDefaultEventHandlers () |
Triggered once before the first call to Script.addEventHandler happens on this ScriptManager connections assumed to use Qt::DirectConnection. More... | |
void | releaseEntityPacketSenderMessages (bool wait) |
Triggered repeatedly in the scripting loop to ensure entity edit messages get processed properly connections assumed to use Qt::DirectConnection. More... | |
Scripting events | |
void | scriptLoaded (const QString &scriptFilename) |
Script.scriptLoaded. More... | |
void | errorLoadingScript (const QString &scriptFilename) |
Script.errorLoadingScript. More... | |
void | update (float deltaTime) |
Triggered frequently at a system-determined interval. More... | |
void | scriptEnding () |
Triggered when the script is stopping. | |
void | finished (const QString &fileNameString, ScriptManagerPointer) |
Script.finished. More... | |
void | printedMessage (const QString &message, const QString &scriptName) |
Triggered when the script prints a message to the program log. More... | |
void | errorMessage (const QString &message, const QString &scriptName) |
Triggered when the script generates an error. More... | |
void | warningMessage (const QString &message, const QString &scriptName) |
Triggered when the script generates a warning. More... | |
void | infoMessage (const QString &message, const QString &scriptName) |
Triggered when the script generates an information message. More... | |
void | printedEntityMessage (const QString &message, const QString &fileName, int lineNumber, const EntityItemID &entityID, bool isServerScript) |
Triggered when a client side entity script prints a message to the program log. More... | |
void | errorEntityMessage (const QString &message, const QString &fileName, int lineNumber, const EntityItemID &entityID, bool isServerScript) |
Triggered when a client side entity script generates an error. More... | |
void | warningEntityMessage (const QString &message, const QString &fileName, int lineNumber, const EntityItemID &entityID, bool isServerScript) |
Triggered when a client side entity script generates a warning. More... | |
void | infoEntityMessage (const QString &message, const QString &fileName, int lineNumber, const EntityItemID &entityID, bool isServerScript) |
Triggered when a client side entity script generates an information message. More... | |
void | runningStateChanged () |
Triggered when the running state of the script changes, e.g., from running to stopping. | |
void | clearDebugWindow () |
Script.clearDebugWindow. More... | |
void | loadScript (const QString &scriptName, bool isUserLoaded) |
Script.loadScript. More... | |
void | reloadScript (const QString &scriptName, bool isUserLoaded) |
Script.reloadScript. More... | |
void | doneRunning () |
Triggered when the script has stopped. | |
void | entityScriptDetailsUpdated () |
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 | entityScriptPreloadFinished (const EntityItemID &entityID) |
Emitted when an entity script has finished running preload. More... | |
void | unhandledException (std::shared_ptr< ScriptException > exception) |
Triggered when a script generates an unhandled exception. More... | |
Public Member Functions | |
void | runInThread () |
Run the script in a dedicated thread. More... | |
void | run () |
Run the script in the caller's thread, exit when Script.stop() is called. More... | |
QString | getFilename () const |
Get the filename of the running script, without the path. More... | |
QString | getAbsoluteFilename () const |
Get the filename of the running script, with absolute path. More... | |
ScriptEnginePointer | engine () |
Underlying scripting engine. More... | |
bool | isStopped () const |
Whether the ScriptManager is stopped and unable to run scripts. More... | |
void | waitTillDoneRunning (bool shutdown=false) |
Stop any evaluating scripts and wait for the scripting thread to finish. More... | |
void | removeFromScriptEngines () |
Removes shared pointer to script engine from the list of all script engines. More... | |
void | loadURL (const QUrl &scriptURL, bool reload) |
Load a script from a given URL. More... | |
bool | hasValidScriptSuffix (const QString &scriptFileName) |
Determines whether a script filename has the right suffix. More... | |
void | setUserLoaded (bool isUserLoaded) |
Set whether this script was user-loaded. More... | |
bool | isUserLoaded () const |
Whether the script was user-loaded. More... | |
void | setQuitWhenFinished (const bool quitWhenFinished) |
Set whether to quit when finished. More... | |
bool | isQuitWhenFinished () const |
Whether to quit when finished. More... | |
void | setEmitScriptUpdatesFunction (std::function< bool()> func) |
Set a function that determines whether to emit update events. More... | |
void | scriptErrorMessage (const QString &message, const QString &fileName, int lineNumber) |
Logs a script error message and emits an errorMessage event. More... | |
void | scriptWarningMessage (const QString &message, const QString &fileName, int lineNumber) |
Logs a script warning message and emits an warningMessage event. More... | |
void | scriptInfoMessage (const QString &message, const QString &fileName, int lineNumber) |
Logs a script info message and emits an infoMessage event. More... | |
void | scriptPrintedMessage (const QString &message, const QString &fileName, int lineNumber) |
Logs a script printed message and emits an printedMessage event. More... | |
void | clearDebugLogWindow () |
Clears the debug log window. More... | |
int | getNumRunningEntityScripts () const |
Get the number of running entity scripts. More... | |
bool | getEntityScriptDetails (const EntityItemID &entityID, EntityScriptDetails &details) const |
Retrieves the details about an entity script. More... | |
bool | hasEntityScriptDetails (const EntityItemID &entityID) const |
Whether there are script details for a given entity ID. More... | |
void | setScriptEngines (QSharedPointer< ScriptEngines > &scriptEngines) |
Set a shared pointer to the ScriptEngines class. More... | |
void | forwardHandlerCall (const EntityItemID &entityID, const QString &eventName, const ScriptValueList &eventHanderArgs) |
Call all the registered event handlers on an entity for the specified name. More... | |
void | removeAllEventHandlers (const EntityItemID &entityID) |
Remove all event handlers for the specified entityID (i.e. the entity is being removed) More... | |
ScriptValue | getReturnValue () const |
Return value of the script that finished running. More... | |
Q_INVOKABLE QString | getExternalPath (ExternalResource::Bucket bucket, const QString &path) |
Gets the URL for an asset in an external resource bucket. More... | |
std::shared_ptr< ScriptException > | getUncaughtException () const |
Get the uncaught exception from the underlying script engine. More... | |
bool | getAbortOnUncaughtException () const |
Whether this engine will abort on an uncaught exception. More... | |
void | setAbortOnUncaughtException (bool value) |
Whether to abort on an uncaught exception. More... | |
bool | isDoneRunning () |
Returns true after script finished running and doneRunning signal was called. More... | |
Script support functions | |
These functions exist to support the scripting API | |
Q_INVOKABLE void | stop (bool marshal=false) |
Stops and unloads the current script. More... | |
Script support methods | |
These functions exist to support the scripting API | |
Q_INVOKABLE QString | getContext () const |
Gets the context that the script is running in: Interface/avatar, client entity, server entity, or assignment client. More... | |
Q_INVOKABLE bool | isClientScript () const |
Checks whether the script is running as an Interface or avatar script. More... | |
Q_INVOKABLE bool | isDebugMode () const |
Checks whether the application was compiled as a debug build. More... | |
Q_INVOKABLE bool | isEntityClientScript () const |
Checks whether the script is running as a client entity script. More... | |
Q_INVOKABLE bool | isEntityServerScript () const |
Checks whether the script is running as a server entity script. More... | |
Q_INVOKABLE bool | isAgentScript () const |
Checks whether the script is running as an assignment client script. More... | |
Q_INVOKABLE void | registerValue (const QString &valueName, ScriptValue value) |
Registers a global object by name. More... | |
Q_INVOKABLE QString | formatException (const ScriptValue &exception, bool includeExtendedDetails) |
Format an exception and return it as a string. More... | |
Q_INVOKABLE void | addEventHandler (const EntityItemID &entityID, const QString &eventName, const ScriptValue &handler) |
Adds a function to the list of functions called when a particular event occurs on a particular entity. More... | |
Q_INVOKABLE void | removeEventHandler (const EntityItemID &entityID, const QString &eventName, const ScriptValue &handler) |
Removes a function from the list of functions called when an entity event occurs on a particular entity. More... | |
Q_INVOKABLE void | load (const QString &loadfile) |
Starts running another script in Interface, if it isn't already running. The script is not automatically loaded next time Interface starts. More... | |
Q_INVOKABLE void | include (const QStringList &includeFiles, const ScriptValue &callback=ScriptValue()) |
Includes JavaScript from other files in the current script. More... | |
Q_INVOKABLE void | include (const QString &includeFile, const ScriptValue &callback=ScriptValue()) |
Includes JavaScript from another file in the current script. More... | |
Module support methods | |
Q_INVOKABLE ScriptValue | require (const QString &moduleId) |
Provides access to methods or objects provided in an external JavaScript or JSON file. More... | |
Q_INVOKABLE void | resetModuleCache (bool deleteScriptCache=false) |
Resets the module cache. More... | |
ScriptValue | currentModule () |
The current parent module from the running JS script. More... | |
bool | registerModuleWithParent (const ScriptValue &module, const ScriptValue &parent) |
Replaces or adds "module" to "parent.children[]" array. More... | |
ScriptValue | newModule (const QString &modulePath, const ScriptValue &parent=ScriptValue()) |
creates a new JS "module" Object with default metadata properties More... | |
QVariantMap | fetchModuleSource (const QString &modulePath, const bool forceDownload=false) |
Synchronously fetch a module's source code. More... | |
ScriptValue | instantiateModule (const ScriptValue &module, const QString &sourceCode) |
Evaluate a pending module object using the fetched source code. More... | |
ScriptValue | evaluate (const QString &program, const QString &fileName=QString()) |
Evaluate a program in the underlying scripting engine. More... | |
Q_INVOKABLE QTimer * | setInterval (const ScriptValue &function, int intervalMS) |
Calls a function repeatedly, at a set interval. More... | |
Q_INVOKABLE QTimer * | setTimeout (const ScriptValue &function, int timeoutMS) |
Calls a function once, after a delay. More... | |
Q_INVOKABLE void | clearInterval (QTimer *timer) |
Stops an interval timer. More... | |
Q_INVOKABLE void | clearInterval (QVariantMap timer) |
Stops an interval timer. More... | |
Q_INVOKABLE void | clearTimeout (QTimer *timer) |
Stops a timeout timer. More... | |
Q_INVOKABLE void | clearTimeout (QVariantMap timer) |
Stops a timeout timer Overloaded version is needed in case the timer has expired. More... | |
Q_INVOKABLE void | print (const QString &message) |
Prints a message to the program log. More... | |
Q_INVOKABLE QUrl | resolvePath (const QString &path) const |
Resolves a relative path to an absolute path. The relative path is relative to the script's location. More... | |
Q_INVOKABLE QUrl | resourcesPath () const |
Gets the path to the resources directory for QML files. More... | |
Q_INVOKABLE void | beginProfileRange (const QString &label) const |
Starts timing a section of code in order to send usage data about it to Overte. Shouldn't be used outside of the standard scripts. More... | |
Q_INVOKABLE void | endProfileRange (const QString &label) const |
Finishes timing a section of code in order to send usage data about it to Overte. Shouldn't be used outside of the standard scripts. More... | |
Entity Script methods | |
Q_INVOKABLE bool | isEntityScriptRunning (const EntityItemID &entityID) |
Checks whether an entity has an entity script running. More... | |
QVariant | cloneEntityScriptDetails (const EntityItemID &entityID) |
Clone the details of an entity script. More... | |
QFuture< QVariant > | getLocalEntityScriptDetails (const EntityItemID &entityID) override |
Get the details of a local entity script. More... | |
Q_INVOKABLE void | requestGarbageCollection () |
Manually runs the JavaScript garbage collector which reclaims memory by disposing of objects that are no longer reachable. | |
Q_INVOKABLE void | logBacktrace (const QString &title) |
Prints out current backtrace to the log. More... | |
Q_INVOKABLE void | loadEntityScript (const EntityItemID &entityID, const QString &entityScript, bool forceRedownload) |
Load an entity script. More... | |
Q_INVOKABLE void | unloadEntityScript (const EntityItemID &entityID, bool shouldRemoveFromMap=false) |
Unload an entity script. More... | |
Q_INVOKABLE void | unloadAllEntityScripts (bool blockingCall=false) |
Unload all entity scripts. More... | |
Q_INVOKABLE void | callEntityScriptMethod (const EntityItemID &entityID, const QString &methodName, const QStringList ¶ms=QStringList(), const QUuid &remoteCallerID=QUuid()) override |
Call a method on an entity script. More... | |
Q_INVOKABLE void | callEntityScriptMethod (const EntityItemID &entityID, const QString &methodName, const PointerEvent &event) |
Call a method on an entity script. More... | |
Q_INVOKABLE void | callEntityScriptMethod (const EntityItemID &entityID, const QString &methodName, const EntityItemID &otherID, const Collision &collision) |
Call a method on an entity script. More... | |
void | setType (Type type) |
Set the script type. More... | |
Type | getType () |
Returns the script type. More... | |
QString | getTypeAsString () const |
Returns the type of the script as a string. More... | |
bool | isFinished () const |
Whether the script has finished running. More... | |
bool | isRunning () const |
Whether the script is running. More... | |
bool | isStopping () const |
Whether this ScriptManager is stopping. Once this is true, it stays true. More... | |
void | disconnectNonEssentialSignals () |
Disconnect all signals, except essential ones. More... | |
Protected Member Functions | |
void | init () |
Initializes the underlying scripting engine. More... | |
Q_INVOKABLE void | executeOnScriptThread (std::function< void()> function, const Qt::ConnectionType &type=Qt::QueuedConnection) |
executeOnScriptThread More... | |
Q_INVOKABLE QString | _requireResolve (const QString &moduleId, const QString &relativeTo=QString()) |
Script._requireResolve. More... | |
QString | logException (const ScriptValue &exception) |
Log an exception. More... | |
void | updateEntityScriptStatus (const EntityItemID &entityID, const EntityScriptStatus &status, const QString &errorInfo=QString()) |
Updates the status of an entity script. More... | |
void | setEntityScriptDetails (const EntityItemID &entityID, const EntityScriptDetails &details) |
Set the details for an entity script. More... | |
void | setParentURL (const QString &parentURL) |
Set the parent URL, used to resolve relative paths. More... | |
QTimer * | setupTimerWithInterval (const ScriptValue &function, int intervalMS, bool isSingleShot) |
Creates a timer with the specified interval. More... | |
void | stopTimer (QTimer *timer) |
Stops a timer. More... | |
Q_INVOKABLE void | entityScriptContentAvailable (const EntityItemID &entityID, const QString &scriptOrURL, const QString &contents, bool isURL, bool success, const QString &status) |
Script.entityScriptContentAvailable. More... | |
void | doWithEnvironment (const EntityItemID &entityID, const QUrl &sandboxURL, std::function< void()> operation) |
Execute operation in the appropriate context for (the possibly empty) entityID. Even if entityID is supplied as currentEntityIdentifier, this still documents the source of the code being executed (e.g., if we ever sandbox different entity scripts, or provide different global values for different entity scripts). More... | |
void | callWithEnvironment (const EntityItemID &entityID, const QUrl &sandboxURL, const ScriptValue &function, const ScriptValue &thisObject, const ScriptValueList &args) |
Execute operation in the appropriate context for (the possibly empty) entityID. More... | |
Friends | |
ScriptManagerPointer | newScriptManager (Context context, const QString &scriptContents, const QString &fileNameString) |
Creates a new ScriptManager. More... | |
Manages a single scripting engine.
This class manages and sets up a single scripting engine to make it execute scripts.
It passes the objects needed to expose the public API, provides console access and error reporting and event management.
This manipulates a single underlying instance of ScriptEngine.
Part of this class' functionality exists only to provide helper functions to the scripts that are run by the scripting engine, and shouldn't be considered part of the C++ API. Those are the functions in the "Script support methods", "Module support methods", "Entity Script methods", and "Scripting signals" sections.
The script-facing interface is in ScriptManagerScriptingInterface and documented in JSDoc as the Script class.
The ScriptManager provides the following functionality to scripts:
Example:
The EntityScript functionality might also benefit from being split off into a separate class, for better organization.
Some more functionality can be shifted to ScriptManagerScriptingInterface, since it only provides services to scripts and isn't called from C++.
Context of the script.
enum ScriptManager::Type |
Type of the script.
|
protected |
Script._requireResolve.
moduleId | |
relativeTo |
void ScriptManager::addEventHandler | ( | const EntityItemID & | entityID, |
const QString & | eventName, | ||
const ScriptValue & | handler | ||
) |
Adds a function to the list of functions called when a particular event occurs on a particular entity.
entityID | Entity ID |
eventName | Name of the event |
handler | Event handler |
|
signal |
Triggered once before the first call to Script.addEventHandler happens on this ScriptManager connections assumed to use Qt::DirectConnection.
void ScriptManager::beginProfileRange | ( | const QString & | label | ) | const |
Starts timing a section of code in order to send usage data about it to Overte. Shouldn't be used outside of the standard scripts.
label |
void ScriptManager::callEntityScriptMethod | ( | const EntityItemID & | entityID, |
const QString & | methodName, | ||
const EntityItemID & | otherID, | ||
const Collision & | collision | ||
) |
Call a method on an entity script.
entityID | |
methodName | |
otherID | |
collision |
void ScriptManager::callEntityScriptMethod | ( | const EntityItemID & | entityID, |
const QString & | methodName, | ||
const PointerEvent & | event | ||
) |
Call a method on an entity script.
entityID | |
methodName | |
event |
|
override |
Call a method on an entity script.
entityID | |
methodName | |
params | |
remoteCallerID |
|
protected |
Execute operation in the appropriate context for (the possibly empty) entityID.
This is equivalent to doWithEnvironment(), only with separate arguments for the function, this object and arguments.
This is a convenience function, which performs:
entityID | Entity ID, may be null |
sandboxURL | Sandbox URL |
function | Function to call |
thisObject | "this" object to use for the call |
args | Arguments |
void ScriptManager::clearDebugLogWindow | ( | ) |
Clears the debug log window.
This only emits clearDebugWindow()
|
signal |
Script.clearDebugWindow.
|
inline |
Stops an interval timer.
timer | Timer to stop |
|
inline |
Stops an interval timer.
Overloaded version is needed in case the timer has expired
timer | Timer to stop |
|
inline |
Stops a timeout timer.
timer | Timer to stop |
|
inline |
Stops a timeout timer Overloaded version is needed in case the timer has expired.
timer | Timer to stop |
QVariant ScriptManager::cloneEntityScriptDetails | ( | const EntityItemID & | entityID | ) |
Clone the details of an entity script.
entityID | Entity ID |
ScriptValue ScriptManager::currentModule | ( | ) |
The current parent module from the running JS script.
void ScriptManager::disconnectNonEssentialSignals | ( | ) |
Disconnect all signals, except essential ones.
This disconnects all signals, except the destroyed() and finished() handlers that are needed for cleanup.
|
protected |
Execute operation in the appropriate context for (the possibly empty) entityID. Even if entityID is supplied as currentEntityIdentifier, this still documents the source of the code being executed (e.g., if we ever sandbox different entity scripts, or provide different global values for different entity scripts).
entityID | Entity ID, may be null |
sandboxURL | Sandbox URL |
operation | Operation to call |
void ScriptManager::endProfileRange | ( | const QString & | label | ) | const |
Finishes timing a section of code in order to send usage data about it to Overte. Shouldn't be used outside of the standard scripts.
label |
|
inline |
Underlying scripting engine.
|
protected |
Script.entityScriptContentAvailable.
entityID | |
scriptOrURL | |
contents | |
isURL | |
success | |
status |
|
signal |
Emitted when an entity script has finished running preload.
entityID |
|
signal |
Triggered when a client side entity script generates an error.
message | |
fileName | Name of the file in which message was generated. |
lineNumber | Number of the line on which message was generated. |
entityID | |
isServerScript | true if entity script is server-side, false if it is client-side. |
|
signal |
Script.errorLoadingScript.
scriptFilename |
|
signal |
Triggered when the script generates an error.
message | |
scriptName |
ScriptValue ScriptManager::evaluate | ( | const QString & | program, |
const QString & | fileName = QString() |
||
) |
Evaluate a program in the underlying scripting engine.
This simply calls to ScriptEngine::evaluate()
program | Source of the program |
fileName | Filename it was obtained from |
|
protected |
executeOnScriptThread
function | |
type |
QVariantMap ScriptManager::fetchModuleSource | ( | const QString & | modulePath, |
const bool | forceDownload = false |
||
) |
Synchronously fetch a module's source code.
The return value is a map containing the following fields:
modulePath | Path to the module's source code |
forceDownload | Force a redownload even if the source is already in the cache |
|
signal |
Script.finished.
fileNameString |
QString ScriptManager::formatException | ( | const ScriptValue & | exception, |
bool | includeExtendedDetails | ||
) |
Format an exception and return it as a string.
exception | Exception object, containing the exception information. |
includeExtendedDetails | Include additional troubleshooting information from the "detail" property, if there's one |
void ScriptManager::forwardHandlerCall | ( | const EntityItemID & | entityID, |
const QString & | eventName, | ||
const ScriptValueList & | eventHanderArgs | ||
) |
Call all the registered event handlers on an entity for the specified name.
Look up the handler associated with eventName and entityID. If found, evalute the argGenerator thunk and call the handler with those args
entityID | |
eventName | |
eventHanderArgs |
|
inline |
Whether this engine will abort on an uncaught exception.
QString ScriptManager::getAbsoluteFilename | ( | ) | const |
Get the filename of the running script, with absolute path.
QString ScriptManager::getContext | ( | ) | const |
Gets the context that the script is running in: Interface/avatar, client entity, server entity, or assignment client.
bool ScriptManager::getEntityScriptDetails | ( | const EntityItemID & | entityID, |
EntityScriptDetails & | details | ||
) | const |
Retrieves the details about an entity script.
entityID | Entity ID |
details | Returned details |
QString ScriptManager::getExternalPath | ( | ExternalResource::Bucket | bucket, |
const QString & | path | ||
) |
Gets the URL for an asset in an external resource bucket.
bucket | |
path |
QString ScriptManager::getFilename | ( | ) | const |
Get the filename of the running script, without the path.
|
override |
Get the details of a local entity script.
Same as cloneEntityScriptDetails, only as a QFuture.
entityID | Entity ID |
int ScriptManager::getNumRunningEntityScripts | ( | ) | const |
Get the number of running entity scripts.
|
inline |
Return value of the script that finished running.
This should only be used after the script terminates.
|
inline |
Returns the script type.
QString ScriptManager::getTypeAsString | ( | ) | const |
Returns the type of the script as a string.
std::shared_ptr< ScriptException > ScriptManager::getUncaughtException | ( | ) | const |
Get the uncaught exception from the underlying script engine.
bool ScriptManager::hasEntityScriptDetails | ( | const EntityItemID & | entityID | ) | const |
Whether there are script details for a given entity ID.
entityID | Entity ID |
bool ScriptManager::hasValidScriptSuffix | ( | const QString & | scriptFileName | ) |
Determines whether a script filename has the right suffix.
scriptFileName |
void ScriptManager::include | ( | const QString & | includeFile, |
const ScriptValue & | callback = ScriptValue() |
||
) |
Includes JavaScript from another file in the current script.
If a callback is specified, the included files will be loaded asynchronously and the callback will be called when all of the files have finished loading. If no callback is specified, the included files will be loaded synchronously and will block execution until all of the files have finished loading.
includeFile | |
callback |
void ScriptManager::include | ( | const QStringList & | includeFiles, |
const ScriptValue & | callback = ScriptValue() |
||
) |
Includes JavaScript from other files in the current script.
If a callback is specified, the included files will be loaded asynchronously and the callback will be called when all of the files have finished loading. If no callback is specified, the included files will be loaded synchronously and will block execution until all of the files have finished loading.
includeFiles | List of files to include |
callback | Callback to call when the files have finished loading. |
|
signal |
Triggered when a client side entity script generates an information message.
message | |
fileName | Name of the file in which message was generated. |
lineNumber | Number of the line on which message was generated. |
entityID | |
isServerScript | true if entity script is server-side, false if it is client-side. |
|
signal |
Triggered when the script generates an information message.
message | |
scriptName |
|
protected |
Initializes the underlying scripting engine.
This sets up the scripting engine with the default APIs
ScriptValue ScriptManager::instantiateModule | ( | const ScriptValue & | module, |
const QString & | sourceCode | ||
) |
Evaluate a pending module object using the fetched source code.
module | Module object |
sourceCode | Source code to evaluate |
|
inline |
Checks whether the script is running as an assignment client script.
|
inline |
Checks whether the script is running as an Interface or avatar script.
bool ScriptManager::isDebugMode | ( | ) | const |
Checks whether the application was compiled as a debug build.
|
inline |
Returns true after script finished running and doneRunning signal was called.
|
inline |
Checks whether the script is running as a client entity script.
|
inline |
Checks whether an entity has an entity script running.
entityID |
|
inline |
Checks whether the script is running as a server entity script.
|
inline |
Whether the script has finished running.
The finished status is set by stop()
|
inline |
|
inline |
Whether the script is running.
bool ScriptManager::isStopped | ( | ) | const |
Whether the ScriptManager is stopped and unable to run scripts.
This is always false for NETWORKLESS_TEST_SCRIPT scripts.
Otherwise, it checks whether scriptEngines is set and is not stopped.
|
inline |
Whether this ScriptManager is stopping. Once this is true, it stays true.
|
inline |
Whether the script was user-loaded.
This is used by Application to track if a script is user loaded or not.
void ScriptManager::load | ( | const QString & | loadfile | ) |
Starts running another script in Interface, if it isn't already running. The script is not automatically loaded next time Interface starts.
The script is loaded as a stand-alone script.
loadfile | File to load |
void ScriptManager::loadEntityScript | ( | const EntityItemID & | entityID, |
const QString & | entityScript, | ||
bool | forceRedownload | ||
) |
Load an entity script.
entityID | |
entityScript | |
forceRedownload |
|
signal |
Script.loadScript.
scriptName | |
isUserLoaded |
void ScriptManager::loadURL | ( | const QUrl & | scriptURL, |
bool | reload | ||
) |
Load a script from a given URL.
If the script engine is not already running, this will download the URL and start the process of seting it up to run.
scriptURL | URL where to load the script from. Can be http, https, atp, or file protocol. The file extension has to pass hasValidScriptSuffix(). |
reload | Load the script again even if it's in the cache. |
void ScriptManager::logBacktrace | ( | const QString & | title | ) |
Prints out current backtrace to the log.
title |
|
protected |
Log an exception.
This both sends an exception to the log as an error message, and returns the formatted text as a string.
exception | Exception |
ScriptValue ScriptManager::newModule | ( | const QString & | modulePath, |
const ScriptValue & | parent = ScriptValue() |
||
) |
creates a new JS "module" Object with default metadata properties
This imitates what is provided by https://nodejs.org/api/modules.html
modulePath | File path to the module |
parent | Parent module |
void ScriptManager::print | ( | const QString & | message | ) |
Prints a message to the program log.
message |
|
signal |
Triggered when a client side entity script prints a message to the program log.
message | |
fileName | Name of the file in which message was generated. |
lineNumber | Number of the line on which message was generated. |
entityID | |
isServerScript | true if entity script is server-side, false if it is client-side. |
|
signal |
Triggered when the script prints a message to the program log.
message | |
scriptName |
bool ScriptManager::registerModuleWithParent | ( | const ScriptValue & | module, |
const ScriptValue & | parent | ||
) |
Replaces or adds "module" to "parent.children[]" array.
This is an internal use function used as a part of the 'require' implementation.
module | Module to register |
parent | Parent |
void ScriptManager::registerValue | ( | const QString & | valueName, |
ScriptValue | value | ||
) |
Registers a global object by name.
valueName | |
value |
|
signal |
Triggered repeatedly in the scripting loop to ensure entity edit messages get processed properly connections assumed to use Qt::DirectConnection.
wait |
|
signal |
Script.reloadScript.
scriptName | |
isUserLoaded |
void ScriptManager::removeAllEventHandlers | ( | const EntityItemID & | entityID | ) |
Remove all event handlers for the specified entityID (i.e. the entity is being removed)
entityID | Entity ID |
void ScriptManager::removeEventHandler | ( | const EntityItemID & | entityID, |
const QString & | eventName, | ||
const ScriptValue & | handler | ||
) |
Removes a function from the list of functions called when an entity event occurs on a particular entity.
entityID | Entity ID |
eventName | Name if the event |
handler | Event handler |
void ScriptManager::removeFromScriptEngines | ( | ) |
Removes shared pointer to script engine from the list of all script engines.
This allows deletion of the script engine once all shared pointer instances are gone. This function is called for entity script engines when they are being destroyed.
ScriptValue ScriptManager::require | ( | const QString & | moduleId | ) |
Provides access to methods or objects provided in an external JavaScript or JSON file.
Implements CommonJS/Node.js like require/module support
moduleId | Module to load |
void ScriptManager::resetModuleCache | ( | bool | deleteScriptCache = false | ) |
Resets the module cache.
deleteScriptCache |
QUrl ScriptManager::resolvePath | ( | const QString & | path | ) | const |
Resolves a relative path to an absolute path. The relative path is relative to the script's location.
path |
QUrl ScriptManager::resourcesPath | ( | ) | const |
Gets the path to the resources directory for QML files.
void ScriptManager::run | ( | ) |
Run the script in the caller's thread, exit when Script.stop() is called.
Most scripts never stop running, so this function will never return for them.
void ScriptManager::runInThread | ( | ) |
Run the script in a dedicated thread.
This will have the side effect of evaluating the current script contents and calling run(). Callers will likely want to register the script with external services before calling this.
This function will return immediately, and work will continue on the newly created thread.
void ScriptManager::scriptErrorMessage | ( | const QString & | message, |
const QString & | fileName, | ||
int | lineNumber | ||
) |
Logs a script error message and emits an errorMessage event.
Emits errorMessage()
message | Message to send to the log |
fileName | Name of the file in which message was generated. Empty string when no file name is available. |
lineNumber | Number of the line on which message was generated. -1 if there line number is not available. |
void ScriptManager::scriptInfoMessage | ( | const QString & | message, |
const QString & | fileName, | ||
int | lineNumber | ||
) |
Logs a script info message and emits an infoMessage event.
Emits infoMessage()
message | Message to send to the log |
fileName | Name of the file in which message was generated. Empty string when no file name is available. |
lineNumber | Number of the line on which message was generated. -1 if there line number is not available. |
|
signal |
Script.scriptLoaded.
scriptFilename |
void ScriptManager::scriptPrintedMessage | ( | const QString & | message, |
const QString & | fileName, | ||
int | lineNumber | ||
) |
Logs a script printed message and emits an printedMessage event.
These are messages scripts provide by calling the print function. Emits printedMessage()
message | Message to send to the log |
fileName | Name of the file in which message was generated. Empty string when no file name is available. |
lineNumber | Number of the line on which message was generated. -1 if there line number is not available. |
void ScriptManager::scriptWarningMessage | ( | const QString & | message, |
const QString & | fileName, | ||
int | lineNumber | ||
) |
Logs a script warning message and emits an warningMessage event.
Emits warningMessage()
message | Message to send to the log |
fileName | Name of the file in which message was generated. Empty string when no file name is available. |
lineNumber | Number of the line on which message was generated. -1 if there line number is not available. |
|
inline |
Whether to abort on an uncaught exception.
value |
|
inline |
|
protected |
Set the details for an entity script.
entityID | Entity ID |
details | Details |
QTimer * ScriptManager::setInterval | ( | const ScriptValue & | function, |
int | intervalMS | ||
) |
Calls a function repeatedly, at a set interval.
function | Function to call |
intervalMS | Interval at which to call the function, in ms |
|
inlineprotected |
Set the parent URL, used to resolve relative paths.
Relative paths are resolved respect of this URL
parentURL | Parent URL |
|
inline |
|
inline |
Set a shared pointer to the ScriptEngines class.
This is used to ask ScriptEngines whether the system is being stopped. Setting this is optional.
isStopped() is implemented by asking ScriptEngines.
scriptEngines | ScriptEngines class |
QTimer * ScriptManager::setTimeout | ( | const ScriptValue & | function, |
int | timeoutMS | ||
) |
Calls a function once, after a delay.
function | Function to call |
timeoutMS | How long to wait before calling the function, in ms |
|
inline |
Set the script type.
type | Type of this script |
|
protected |
Creates a timer with the specified interval.
function | Function to call when the interval elapses |
intervalMS | Interval in milliseconds |
isSingleShot | Whether the timer happens continuously or a single time |
|
inline |
Set whether this script was user-loaded.
This is used by Application to track if a script is user loaded or not.
isUserLoaded | Script is user-loaded. |
void ScriptManager::stop | ( | bool | marshal = false | ) |
Stops and unloads the current script.
marshal | Deprecated |
|
protected |
Stops a timer.
timer | Timer to stop |
|
signal |
Triggered when a script generates an unhandled exception.
exception |
void ScriptManager::unloadAllEntityScripts | ( | bool | blockingCall = false | ) |
Unload all entity scripts.
blockingCall |
void ScriptManager::unloadEntityScript | ( | const EntityItemID & | entityID, |
bool | shouldRemoveFromMap = false |
||
) |
Unload an entity script.
entityID | |
shouldRemoveFromMap |
|
signal |
Triggered frequently at a system-determined interval.
deltaTime |
|
protected |
Updates the status of an entity script.
Emits entityScriptDetailsUpdated()
entityID | Entity ID |
status | Status |
errorInfo | Description of the error, if any |
|
slot |
Script.updateMemoryCost.
Sends a memory cost update to the underlying scripting engine
deltaSize | Difference in memory usage |
void ScriptManager::waitTillDoneRunning | ( | bool | shutdown = false | ) |
Stop any evaluating scripts and wait for the scripting thread to finish.
shutdown | True if we are currently shutting down. Setting this to true will allow processing events emitted during the script's shutdown, such as scripts saving settings. |
|
signal |
Triggered when a client side entity script generates a warning.
message | |
fileName | Name of the file in which message was generated. |
lineNumber | Number of the line on which message was generated. |
entityID | |
isServerScript | true if entity script is server-side, false if it is client-side. |
|
signal |
Triggered when the script generates a warning.
message | |
scriptName |
|
friend |
Creates a new ScriptManager.
context | Context in which scripts will run |
scriptContents | Contents of the script to run |
fileNameString | Filename for the script |