Qt 4.8
Public Types | Public Functions | Protected Functions | Protected Variables | List of all members
QScriptEngineAgent Class Reference

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

#include <qscriptengineagent.h>

Inheritance diagram for QScriptEngineAgent:
QJSDebuggerAgent QScriptDebuggerAgent

Public Types

enum  Extension { DebuggerInvocationRequest }
 This enum specifies the possible extensions to a QScriptEngineAgent. More...
 

Public Functions

virtual void contextPop ()
 This function is called when the current script context is about to be popped. More...
 
virtual void contextPush ()
 This function is called when a new script context has been pushed. More...
 
QScriptEngineengine () const
 Returns the QScriptEngine that this agent is associated with. More...
 
virtual void exceptionCatch (qint64 scriptId, const QScriptValue &exception)
 This function is called when the given exception is about to be caught, in the script identified by scriptId. More...
 
virtual void exceptionThrow (qint64 scriptId, const QScriptValue &exception, bool hasHandler)
 This function is called when the given exception has occurred in the engine, in the script identified by scriptId. More...
 
virtual QVariant extension (Extension extension, const QVariant &argument=QVariant())
 This virtual function can be reimplemented in a QScriptEngineAgent subclass to provide support for extensions. More...
 
virtual void functionEntry (qint64 scriptId)
 This function is called when a script function is called in the engine. More...
 
virtual void functionExit (qint64 scriptId, const QScriptValue &returnValue)
 This function is called when the currently executing script function is about to return. More...
 
virtual void positionChange (qint64 scriptId, int lineNumber, int columnNumber)
 This function is called when the engine is about to execute a new statement in the script identified by scriptId. More...
 
 QScriptEngineAgent (QScriptEngine *engine)
 Constructs a QScriptEngineAgent object for the given engine. More...
 
virtual void scriptLoad (qint64 id, const QString &program, const QString &fileName, int baseLineNumber)
 This function is called when the engine has parsed a script and has associated it with the given id. More...
 
virtual void scriptUnload (qint64 id)
 This function is called when the engine has discarded the script identified by the given id. More...
 
virtual bool supportsExtension (Extension extension) const
 Returns true if the QScriptEngineAgent supports the given extension; otherwise, false is returned. More...
 
virtual ~QScriptEngineAgent ()
 Destroys this QScriptEngineAgent. More...
 

Protected Functions

 QScriptEngineAgent (QScriptEngineAgentPrivate &dd, QScriptEngine *engine)
 

Protected Variables

QScopedPointer< QScriptEngineAgentPrivated_ptr
 

Detailed Description

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

Since
4.4

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

To process script loading and unloading events, reimplement the scriptLoad() and scriptUnload() functions. 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 scriptLoad(); subsequently, other event handlers can use the ID to identify a particular script. One common usage of 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.

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

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

var a = Math.random() + 2;

Evaluating the following script will {not} result in a call to 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), 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 functionEntry() and functionExit() functions. functionEntry() is called when a script function is about to be executed; functionExit() is called when a script function is about to return, either normally or due to an exception.

To process individual script statements, reimplement positionChange(). 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 exceptionThrow() and exceptionCatch(). exceptionThrow() is called when a script exception is thrown, before it has been handled. exceptionCatch() is called when an exception handler is present, and execution is about to be resumed at the handler code.

See also
QScriptEngine::setAgent(), QScriptContextInfo

Definition at line 42 of file qscriptengineagent.h.

Enumerations

◆ Extension

This enum specifies the possible extensions to a QScriptEngineAgent.

  • DebuggerInvocationRequest The agent handles debugger script statements.
See also
extension()
Enumerator
DebuggerInvocationRequest 

Definition at line 45 of file qscriptengineagent.h.

Constructors and Destructors

◆ QScriptEngineAgent() [1/2]

QScriptEngineAgent::QScriptEngineAgent ( QScriptEngine engine)

Constructs a QScriptEngineAgent object for the given engine.

The engine takes ownership of the agent.

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

Definition at line 227 of file qscriptengineagent.cpp.

229 {
230  d_ptr->q_ptr = this;
232  d_ptr->engine->ownedAgents.append(this);
233 }
static QScriptEnginePrivate * get(QScriptEngine *q)
QScopedPointer< QScriptEngineAgentPrivate > d_ptr
QList< QScriptEngineAgent * > ownedAgents
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QScriptEnginePrivate * engine

◆ ~QScriptEngineAgent()

QScriptEngineAgent::~QScriptEngineAgent ( )
virtual

Destroys this QScriptEngineAgent.

Definition at line 248 of file qscriptengineagent.cpp.

249 {
250  d_ptr->engine->agentDeleted(this); //### TODO: Can this throw?
251 }
QScopedPointer< QScriptEngineAgentPrivate > d_ptr
QScriptEnginePrivate * engine
void agentDeleted(QScriptEngineAgent *agent)

◆ QScriptEngineAgent() [2/2]

QScriptEngineAgent::QScriptEngineAgent ( QScriptEngineAgentPrivate dd,
QScriptEngine engine 
)
protected
Warning
This function is not part of the public interface.

Definition at line 238 of file qscriptengineagent.cpp.

239  : d_ptr(&dd)
240 {
241  d_ptr->q_ptr = this;
243 }
static QScriptEnginePrivate * get(QScriptEngine *q)
QScopedPointer< QScriptEngineAgentPrivate > d_ptr
QScriptEnginePrivate * engine

Functions

◆ contextPop()

void QScriptEngineAgent::contextPop ( )
virtual

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

The default implementation does nothing.

See also
contextPush(), functionExit()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 319 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::functionExit(), and QScriptEngine::popContext().

320 {
321 }

◆ contextPush()

void QScriptEngineAgent::contextPush ( )
virtual

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

The default implementation does nothing.

See also
contextPop(), functionEntry()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 307 of file qscriptengineagent.cpp.

Referenced by QScriptDeclarativeClass::pushCleanContext(), and QScriptEngine::pushContext().

308 {
309 }

◆ engine()

QScriptEngine * QScriptEngineAgent::engine ( ) const

◆ exceptionCatch()

void QScriptEngineAgent::exceptionCatch ( qint64  scriptId,
const QScriptValue exception 
)
virtual

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.

See also
exceptionThrow()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 440 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::exceptionCatch().

442 {
443  Q_UNUSED(scriptId);
444  Q_UNUSED(exception);
445 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ exceptionThrow()

void QScriptEngineAgent::exceptionThrow ( qint64  scriptId,
const QScriptValue exception,
bool  hasHandler 
)
virtual

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.

See also
exceptionCatch()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 421 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::exceptionThrow().

424 {
425  Q_UNUSED(scriptId);
426  Q_UNUSED(exception);
427  Q_UNUSED(hasHandler);
428 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ extension()

QVariant QScriptEngineAgent::extension ( Extension  extension,
const QVariant argument = QVariant() 
)
virtual

This virtual function can be reimplemented in a 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 QVariant. You can call supportsExtension() to check if an extension is supported by the QScriptEngineAgent. By default, no extensions are supported, and this function returns an invalid 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).

See also
supportsExtension()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 499 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::didReachBreakpoint(), and QJSDebuggerAgent::extension().

501 {
503  Q_UNUSED(argument);
504  return QVariant();
505 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
virtual QVariant extension(Extension extension, const QVariant &argument=QVariant())
This virtual function can be reimplemented in a QScriptEngineAgent subclass to provide support for ex...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ functionEntry()

void QScriptEngineAgent::functionEntry ( qint64  scriptId)
virtual

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 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 functionExit()) to keep track of the call stack and provide step-over functionality.

The default implementation does nothing.

See also
functionExit(), positionChange(), QScriptEngine::currentContext()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 343 of file qscriptengineagent.cpp.

344 {
345  Q_UNUSED(scriptId);
346 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ functionExit()

void QScriptEngineAgent::functionExit ( qint64  scriptId,
const QScriptValue returnValue 
)
virtual

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 QScriptContext associated with the script function call with QScriptEngine::currentContext().

If the engine's 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 functionEntry().

The default implementation does nothing.

See also
functionEntry(), QScriptEngine::hasUncaughtException()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 371 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::evaluateStop(), and QScriptEngineAgentPrivate::functionExit().

373 {
374  Q_UNUSED(scriptId);
375  Q_UNUSED(returnValue);
376 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ positionChange()

void QScriptEngineAgent::positionChange ( qint64  scriptId,
int  lineNumber,
int  columnNumber 
)
virtual

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
See also
scriptLoad(), functionEntry()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 395 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::atStatement().

397 {
398  Q_UNUSED(scriptId);
399  Q_UNUSED(lineNumber);
400  Q_UNUSED(columnNumber);
401 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ scriptLoad()

void QScriptEngineAgent::scriptLoad ( qint64  id,
const QString program,
const QString fileName,
int  baseLineNumber 
)
virtual

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 positionChange().

The default implementation does nothing.

See also
scriptUnload()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 275 of file qscriptengineagent.cpp.

277 {
278  Q_UNUSED(id);
279  Q_UNUSED(program);
280  Q_UNUSED(fileName);
281  Q_UNUSED(baseLineNumber);
282 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ scriptUnload()

void QScriptEngineAgent::scriptUnload ( qint64  id)
virtual

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.

See also
scriptLoad()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 295 of file qscriptengineagent.cpp.

296 {
297  Q_UNUSED(id);
298 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ supportsExtension()

bool QScriptEngineAgent::supportsExtension ( Extension  extension) const
virtual

Returns true if the QScriptEngineAgent supports the given extension; otherwise, false is returned.

By default, no extensions are supported.

See also
extension()

Reimplemented in QJSDebuggerAgent, and QScriptDebuggerAgent.

Definition at line 475 of file qscriptengineagent.cpp.

Referenced by QScriptEngineAgentPrivate::didReachBreakpoint().

476 {
478  return false;
479 }
virtual QVariant extension(Extension extension, const QVariant &argument=QVariant())
This virtual function can be reimplemented in a QScriptEngineAgent subclass to provide support for ex...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

Properties

◆ d_ptr

QScopedPointer<QScriptEngineAgentPrivate> QScriptEngineAgent::d_ptr
protected

Definition at line 80 of file qscriptengineagent.h.

Referenced by QScriptEngineAgent(), and ~QScriptEngineAgent().


The documentation for this class was generated from the following files: