| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qscriptengineagent.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_SCRIPT
|
|---|
| 45 |
|
|---|
| 46 | #include "qscriptvalue.h"
|
|---|
| 47 | #include "qscriptengineagent_p.h"
|
|---|
| 48 | #include "qscriptengine_p.h"
|
|---|
| 49 | #include "qscriptcontext_p.h"
|
|---|
| 50 | #include "qscriptmember_p.h"
|
|---|
| 51 | #include "qscriptobject_p.h"
|
|---|
| 52 | #include "qscriptvalueimpl_p.h"
|
|---|
| 53 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | /*!
|
|---|
| 57 | \since 4.4
|
|---|
| 58 | \class QScriptEngineAgent
|
|---|
| 59 |
|
|---|
| 60 | \brief The QScriptEngineAgent class provides an interface to report events pertaining to QScriptEngine execution.
|
|---|
| 61 |
|
|---|
| 62 | \ingroup script
|
|---|
| 63 | \mainclass
|
|---|
| 64 |
|
|---|
| 65 | The QScriptEngineAgent class is the basis of tools that monitor and/or control the execution of a
|
|---|
| 66 | QScriptEngine, such as debuggers and profilers.
|
|---|
| 67 |
|
|---|
| 68 | To process script loading and unloading events, reimplement the
|
|---|
| 69 | scriptLoad() and scriptUnload() functions. scriptLoad() is called
|
|---|
| 70 | after the input to QScriptEngine::evaluate() has been parsed, right
|
|---|
| 71 | before the given script is executed. The engine assigns each
|
|---|
| 72 | script an ID, which is available as one of the arguments to
|
|---|
| 73 | scriptLoad(); subsequently, other event handlers can use the ID to
|
|---|
| 74 | identify a particular script. One common usage of scriptLoad() is
|
|---|
| 75 | to retain the script text, filename and base line number (the
|
|---|
| 76 | original input to QScriptEngine::evaluate()), so that other event
|
|---|
| 77 | handlers can e.g. map a line number to the corresponding line of
|
|---|
| 78 | text.
|
|---|
| 79 |
|
|---|
| 80 | scriptUnload() is called when the QScriptEngine has no further use
|
|---|
| 81 | for a script; the QScriptEngineAgent may at this point safely
|
|---|
| 82 | discard any resources associated with the script (such as the
|
|---|
| 83 | script text). Note that after scriptUnload() has been called, the
|
|---|
| 84 | QScriptEngine may reuse the relevant script ID for new scripts
|
|---|
| 85 | (i.e. as argument to a subsequent call to scriptLoad()).
|
|---|
| 86 |
|
|---|
| 87 | Evaluating the following script will result in scriptUnload()
|
|---|
| 88 | being called immediately after evaluation has completed:
|
|---|
| 89 |
|
|---|
| 90 | \snippet doc/src/snippets/code/src_script_qscriptengineagent.cpp 0
|
|---|
| 91 |
|
|---|
| 92 | Evaluating the following script will \b{not} result in a call to
|
|---|
| 93 | scriptUnload() when evaluation has completed:
|
|---|
| 94 |
|
|---|
| 95 | \snippet doc/src/snippets/code/src_script_qscriptengineagent.cpp 1
|
|---|
| 96 |
|
|---|
| 97 | The script isn't unloaded because it defines a function (\c{cube})
|
|---|
| 98 | that remains in the script environment after evaluation has
|
|---|
| 99 | completed. If a subsequent script removed the \c{cube} function
|
|---|
| 100 | (e.g. by setting it to \c{null}), scriptUnload() would be called
|
|---|
| 101 | when the function is garbage collected. In general terms, a script
|
|---|
| 102 | isn't unloaded until the engine has determined that none of its
|
|---|
| 103 | contents is referenced.
|
|---|
| 104 |
|
|---|
| 105 | To process script function calls and returns, reimplement the
|
|---|
| 106 | functionEntry() and functionExit() functions. functionEntry() is
|
|---|
| 107 | called when a script function is about to be executed;
|
|---|
| 108 | functionExit() is called when a script function is about to return,
|
|---|
| 109 | either normally or due to an exception.
|
|---|
| 110 |
|
|---|
| 111 | To process individual script statements, reimplement
|
|---|
| 112 | positionChange(). positionChange() is called each time the engine is
|
|---|
| 113 | about to execute a new statement of a script, and thus offers the
|
|---|
| 114 | finest level of script monitoring.
|
|---|
| 115 |
|
|---|
| 116 | To process exceptions, reimplement exceptionThrow() and
|
|---|
| 117 | exceptionCatch(). exceptionThrow() is called when a script exception
|
|---|
| 118 | is thrown, before it has been handled. exceptionCatch() is called
|
|---|
| 119 | when an exception handler is present, and execution is about to be
|
|---|
| 120 | resumed at the handler code.
|
|---|
| 121 |
|
|---|
| 122 | \sa QScriptEngine::setAgent(), QScriptContextInfo
|
|---|
| 123 | */
|
|---|
| 124 |
|
|---|
| 125 | /*!
|
|---|
| 126 | \enum QScriptEngineAgent::Extension
|
|---|
| 127 |
|
|---|
| 128 | This enum specifies the possible extensions to a QScriptEngineAgent.
|
|---|
| 129 |
|
|---|
| 130 | \value DebuggerInvocationRequest The agent handles \c{debugger} script statements.
|
|---|
| 131 |
|
|---|
| 132 | \sa extension()
|
|---|
| 133 | */
|
|---|
| 134 |
|
|---|
| 135 | QScriptEngineAgentPrivate::QScriptEngineAgentPrivate()
|
|---|
| 136 | : engine(0), q_ptr(0)
|
|---|
| 137 | {
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | QScriptEngineAgentPrivate::~QScriptEngineAgentPrivate()
|
|---|
| 141 | {
|
|---|
| 142 | QScriptEnginePrivate *eng_p = QScriptEnginePrivate::get(engine);
|
|---|
| 143 | eng_p->agentDeleted(q_ptr);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /*!
|
|---|
| 147 | Constructs a QScriptEngineAgent object for the given \a engine.
|
|---|
| 148 |
|
|---|
| 149 | The engine takes ownership of the agent.
|
|---|
| 150 |
|
|---|
| 151 | Call QScriptEngine::setAgent() to make this agent the active
|
|---|
| 152 | agent.
|
|---|
| 153 | */
|
|---|
| 154 | QScriptEngineAgent::QScriptEngineAgent(QScriptEngine *engine)
|
|---|
| 155 | : d_ptr(new QScriptEngineAgentPrivate)
|
|---|
| 156 | {
|
|---|
| 157 | d_ptr->q_ptr = this;
|
|---|
| 158 | d_ptr->engine = engine;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /*!
|
|---|
| 162 | \internal
|
|---|
| 163 | */
|
|---|
| 164 | QScriptEngineAgent::QScriptEngineAgent(QScriptEngineAgentPrivate &dd, QScriptEngine *engine)
|
|---|
| 165 | : d_ptr(&dd)
|
|---|
| 166 | {
|
|---|
| 167 | d_ptr->q_ptr = this;
|
|---|
| 168 | d_ptr->engine = engine;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*!
|
|---|
| 172 | Destroys this QScriptEngineAgent.
|
|---|
| 173 | */
|
|---|
| 174 | QScriptEngineAgent::~QScriptEngineAgent()
|
|---|
| 175 | {
|
|---|
| 176 | delete d_ptr;
|
|---|
| 177 | d_ptr = 0;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /*!
|
|---|
| 181 |
|
|---|
| 182 | This function is called when the engine has parsed a script and has
|
|---|
| 183 | associated it with the given \a id. The id can be used to identify
|
|---|
| 184 | this particular script in subsequent event notifications.
|
|---|
| 185 |
|
|---|
| 186 | \a program, \a fileName and \a baseLineNumber are the original
|
|---|
| 187 | arguments to the QScriptEngine::evaluate() call that triggered this
|
|---|
| 188 | event.
|
|---|
| 189 |
|
|---|
| 190 | This function is called just before the script is about to be
|
|---|
| 191 | evaluated.
|
|---|
| 192 |
|
|---|
| 193 | You can reimplement this function to record information about the
|
|---|
| 194 | script; for example, by retaining the script text, you can obtain
|
|---|
| 195 | the line of text corresponding to a line number in a subsequent
|
|---|
| 196 | call to positionChange().
|
|---|
| 197 |
|
|---|
| 198 | The default implementation does nothing.
|
|---|
| 199 |
|
|---|
| 200 | \sa scriptUnload()
|
|---|
| 201 | */
|
|---|
| 202 | void QScriptEngineAgent::scriptLoad(qint64 id, const QString &program,
|
|---|
| 203 | const QString &fileName, int baseLineNumber)
|
|---|
| 204 | {
|
|---|
| 205 | Q_UNUSED(id);
|
|---|
| 206 | Q_UNUSED(program);
|
|---|
| 207 | Q_UNUSED(fileName);
|
|---|
| 208 | Q_UNUSED(baseLineNumber);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /*!
|
|---|
| 212 | This function is called when the engine has discarded the script
|
|---|
| 213 | identified by the given \a id.
|
|---|
| 214 |
|
|---|
| 215 | You can reimplement this function to clean up any resources you have
|
|---|
| 216 | associated with the script.
|
|---|
| 217 |
|
|---|
| 218 | The default implementation does nothing.
|
|---|
| 219 |
|
|---|
| 220 | \sa scriptLoad()
|
|---|
| 221 | */
|
|---|
| 222 | void QScriptEngineAgent::scriptUnload(qint64 id)
|
|---|
| 223 | {
|
|---|
| 224 | Q_UNUSED(id);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /*!
|
|---|
| 228 | This function is called when a new script context has been pushed.
|
|---|
| 229 |
|
|---|
| 230 | The default implementation does nothing.
|
|---|
| 231 |
|
|---|
| 232 | \sa contextPop(), functionEntry()
|
|---|
| 233 | */
|
|---|
| 234 | void QScriptEngineAgent::contextPush()
|
|---|
| 235 | {
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /*!
|
|---|
| 239 | This function is called when the current script context is about to
|
|---|
| 240 | be popped.
|
|---|
| 241 |
|
|---|
| 242 | The default implementation does nothing.
|
|---|
| 243 |
|
|---|
| 244 | \sa contextPush(), functionExit()
|
|---|
| 245 | */
|
|---|
| 246 | void QScriptEngineAgent::contextPop()
|
|---|
| 247 | {
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /*!
|
|---|
| 251 | This function is called when a script function is called in the
|
|---|
| 252 | engine. If the script function is not a native Qt Script function,
|
|---|
| 253 | it resides in the script identified by \a scriptId; otherwise, \a
|
|---|
| 254 | scriptId is -1.
|
|---|
| 255 |
|
|---|
| 256 | This function is called just before execution of the script function
|
|---|
| 257 | begins. You can obtain the QScriptContext associated with the
|
|---|
| 258 | function call with QScriptEngine::currentContext(). The arguments
|
|---|
| 259 | passed to the function are available.
|
|---|
| 260 |
|
|---|
| 261 | Reimplement this function to handle this event. For example, a
|
|---|
| 262 | debugger implementation could reimplement this function (and
|
|---|
| 263 | functionExit()) to keep track of the call stack and provide
|
|---|
| 264 | step-over functionality.
|
|---|
| 265 |
|
|---|
| 266 | The default implementation does nothing.
|
|---|
| 267 |
|
|---|
| 268 | \sa functionExit(), positionChange(), QScriptEngine::currentContext()
|
|---|
| 269 | */
|
|---|
| 270 | void QScriptEngineAgent::functionEntry(qint64 scriptId)
|
|---|
| 271 | {
|
|---|
| 272 | Q_UNUSED(scriptId);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | /*!
|
|---|
| 276 | This function is called when the currently executing script function
|
|---|
| 277 | is about to return. If the script function is not a native Qt Script
|
|---|
| 278 | function, it resides in the script identified by \a scriptId;
|
|---|
| 279 | otherwise, \a scriptId is -1. The \a returnValue is the value that
|
|---|
| 280 | the script function will return.
|
|---|
| 281 |
|
|---|
| 282 | This function is called just before the script function returns.
|
|---|
| 283 | You can still access the QScriptContext associated with the
|
|---|
| 284 | script function call with QScriptEngine::currentContext().
|
|---|
| 285 |
|
|---|
| 286 | If the engine's
|
|---|
| 287 | \l{QScriptEngine::hasUncaughtException()}{hasUncaughtException}()
|
|---|
| 288 | function returns true, the script function is exiting due to an
|
|---|
| 289 | exception; otherwise, the script function is returning normally.
|
|---|
| 290 |
|
|---|
| 291 | Reimplement this function to handle this event; typically you will
|
|---|
| 292 | then also want to reimplement functionEntry().
|
|---|
| 293 |
|
|---|
| 294 | The default implementation does nothing.
|
|---|
| 295 |
|
|---|
| 296 | \sa functionEntry(), QScriptEngine::hasUncaughtException()
|
|---|
| 297 | */
|
|---|
| 298 | void QScriptEngineAgent::functionExit(qint64 scriptId,
|
|---|
| 299 | const QScriptValue &returnValue)
|
|---|
|
|---|