QScriptEngineAgent

Synopsis

Functions

Virtual functions

Detailed Description

The PySide.QtScript.QScriptEngineAgent class provides an interface to report events pertaining to PySide.QtScript.QScriptEngine execution.

The PySide.QtScript.QScriptEngineAgent class is the basis of tools that monitor and/or control the execution of a PySide.QtScript.QScriptEngine , such as debuggers and profilers.

To process script loading and unloading events, reimplement the PySide.QtScript.QScriptEngineAgent.scriptLoad() and PySide.QtScript.QScriptEngineAgent.scriptUnload() functions. PySide.QtScript.QScriptEngineAgent.scriptLoad() is called after the input to QScriptEngine.evaluate() has been parsed, right before the given script is executed. The engine assigns each script an ID, which is available as one of the arguments to PySide.QtScript.QScriptEngineAgent.scriptLoad() ; subsequently, other event handlers can use the ID to identify a particular script. One common usage of PySide.QtScript.QScriptEngineAgent.scriptLoad() is to retain the script text, filename and base line number (the original input to QScriptEngine.evaluate() ), so that other event handlers can e.g. map a line number to the corresponding line of text.

PySide.QtScript.QScriptEngineAgent.scriptUnload() is called when the PySide.QtScript.QScriptEngine has no further use for a script; the PySide.QtScript.QScriptEngineAgent may at this point safely discard any resources associated with the script (such as the script text). Note that after PySide.QtScript.QScriptEngineAgent.scriptUnload() has been called, the PySide.QtScript.QScriptEngine may reuse the relevant script ID for new scripts (i.e. as argument to a subsequent call to PySide.QtScript.QScriptEngineAgent.scriptLoad() ).

Evaluating the following script will result in PySide.QtScript.QScriptEngineAgent.scriptUnload() being called immediately after evaluation has completed:

var a = Math.random() + 2;

Evaluating the following script will b{not} result in a call to PySide.QtScript.QScriptEngineAgent.scriptUnload() when evaluation has completed:

function cube(a) {
    return a * a * a;
}

var a = cube(3);

The script isn’t unloaded because it defines a function (cube ) that remains in the script environment after evaluation has completed. If a subsequent script removed the cube function (e.g. by setting it to null ), PySide.QtScript.QScriptEngineAgent.scriptUnload() would be called when the function is garbage collected. In general terms, a script isn’t unloaded until the engine has determined that none of its contents is referenced.

To process script function calls and returns, reimplement the PySide.QtScript.QScriptEngineAgent.functionEntry() and PySide.QtScript.QScriptEngineAgent.functionExit() functions. PySide.QtScript.QScriptEngineAgent.functionEntry() is called when a script function is about to be executed; PySide.QtScript.QScriptEngineAgent.functionExit() is called when a script function is about to return, either normally or due to an exception.

To process individual script statements, reimplement PySide.QtScript.QScriptEngineAgent.positionChange() . PySide.QtScript.QScriptEngineAgent.positionChange() is called each time the engine is about to execute a new statement of a script, and thus offers the finest level of script monitoring.

To process exceptions, reimplement PySide.QtScript.QScriptEngineAgent.exceptionThrow() and PySide.QtScript.QScriptEngineAgent.exceptionCatch() . PySide.QtScript.QScriptEngineAgent.exceptionThrow() is called when a script exception is thrown, before it has been handled. PySide.QtScript.QScriptEngineAgent.exceptionCatch() is called when an exception handler is present, and execution is about to be resumed at the handler code.

class PySide.QtScript.QScriptEngineAgent(engine)
Parameters:enginePySide.QtScript.QScriptEngine

Constructs a PySide.QtScript.QScriptEngineAgent object for the given engine .

The engine takes ownership of the agent.

Call QScriptEngine.setAgent() to make this agent the active agent.

PySide.QtScript.QScriptEngineAgent.Extension

This enum specifies the possible extensions to a PySide.QtScript.QScriptEngineAgent .

Constant Description
QScriptEngineAgent.DebuggerInvocationRequest The agent handles debugger script statements.
PySide.QtScript.QScriptEngineAgent.contextPop()

This function is called when the current script context is about to be popped.

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.contextPush()

This function is called when a new script context has been pushed.

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.engine()
Return type:PySide.QtScript.QScriptEngine

Returns the PySide.QtScript.QScriptEngine that this agent is associated with.

PySide.QtScript.QScriptEngineAgent.exceptionCatch(scriptId, exception)
Parameters:

This function is called when the given exception is about to be caught, in the script identified by scriptId .

Reimplement this function if you want to handle this event.

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.exceptionThrow(scriptId, exception, hasHandler)
Parameters:

This function is called when the given exception has occurred in the engine, in the script identified by scriptId . If the exception was thrown by a native Qt Script function, scriptId is -1.

If hasHandler is true, there is a catch or finally block that will handle the exception. If hasHandler is false, there is no handler for the exception.

Reimplement this function if you want to handle this event. For example, a debugger can notify the user when an uncaught exception occurs (i.e. hasHandler is false).

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.extension(extension[, argument=None])
Parameters:
Return type:

object

This virtual function can be reimplemented in a PySide.QtScript.QScriptEngineAgent subclass to provide support for extensions. The optional argument can be provided as input to the extension ; the result must be returned in the form of a PySide.QtCore.QVariant . You can call PySide.QtScript.QScriptEngineAgent.supportsExtension() to check if an extension is supported by the PySide.QtScript.QScriptEngineAgent . By default, no extensions are supported, and this function returns an invalid PySide.QtCore.QVariant .

If you implement the DebuggerInvocationRequest extension, Qt Script will call this function when a debugger statement is encountered in a script. The argument is a QVariantList containing three items: The first item is the scriptId (a qint64), the second item is the line number (an int), and the third item is the column number (an int).

PySide.QtScript.QScriptEngineAgent.functionEntry(scriptId)
Parameters:scriptIdPySide.QtCore.qint64

This function is called when a script function is called in the engine. If the script function is not a native Qt Script function, it resides in the script identified by scriptId ; otherwise, scriptId is -1.

This function is called just before execution of the script function begins. You can obtain the PySide.QtScript.QScriptContext associated with the function call with QScriptEngine.currentContext() . The arguments passed to the function are available.

Reimplement this function to handle this event. For example, a debugger implementation could reimplement this function (and PySide.QtScript.QScriptEngineAgent.functionExit() ) to keep track of the call stack and provide step-over functionality.

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.functionExit(scriptId, returnValue)
Parameters:

This function is called when the currently executing script function is about to return. If the script function is not a native Qt Script function, it resides in the script identified by scriptId ; otherwise, scriptId is -1. The returnValue is the value that the script function will return.

This function is called just before the script function returns. You can still access the PySide.QtScript.QScriptContext associated with the script function call with QScriptEngine.currentContext() .

If the engine’s PySide.QtScript.QScriptEngine.hasUncaughtException() () function returns true, the script function is exiting due to an exception; otherwise, the script function is returning normally.

Reimplement this function to handle this event; typically you will then also want to reimplement PySide.QtScript.QScriptEngineAgent.functionEntry() .

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.positionChange(scriptId, lineNumber, columnNumber)
Parameters:
  • scriptIdPySide.QtCore.qint64
  • lineNumberPySide.QtCore.int
  • columnNumberPySide.QtCore.int

This function is called when the engine is about to execute a new statement in the script identified by scriptId . The statement begins on the line and column specified by lineNumber This event is not generated for native Qt Script functions.

Reimplement this function to handle this event. For example, a debugger implementation could reimplement this function to provide line-by-line stepping, and a profiler implementation could use it to count the number of times each statement is executed.

The default implementation does nothing.

Note

columnNumber is undefined

PySide.QtScript.QScriptEngineAgent.scriptLoad(id, program, fileName, baseLineNumber)
Parameters:
  • idPySide.QtCore.qint64
  • program – unicode
  • fileName – unicode
  • baseLineNumberPySide.QtCore.int

This function is called when the engine has parsed a script and has associated it with the given id . The id can be used to identify this particular script in subsequent event notifications.

program , fileName and baseLineNumber are the original arguments to the QScriptEngine.evaluate() call that triggered this event.

This function is called just before the script is about to be evaluated.

You can reimplement this function to record information about the script; for example, by retaining the script text, you can obtain the line of text corresponding to a line number in a subsequent call to PySide.QtScript.QScriptEngineAgent.positionChange() .

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.scriptUnload(id)
Parameters:idPySide.QtCore.qint64

This function is called when the engine has discarded the script identified by the given id .

You can reimplement this function to clean up any resources you have associated with the script.

The default implementation does nothing.

PySide.QtScript.QScriptEngineAgent.supportsExtension(extension)
Parameters:extensionPySide.QtScript.QScriptEngineAgent.Extension
Return type:PySide.QtCore.bool

Returns true if the PySide.QtScript.QScriptEngineAgent supports the given extension ; otherwise, false is returned. By default, no extensions are supported.