| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 qt-sales@nokia.com.
|
|---|
| 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)
|
|---|
| 300 | {
|
|---|
| 301 | Q_UNUSED(scriptId);
|
|---|
| 302 | Q_UNUSED(returnValue);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | /*!
|
|---|
| 306 | This function is called when the engine is about to execute a new
|
|---|
| 307 | statement in the script identified by \a scriptId. The statement
|
|---|
| 308 | begins on the line and column specified by \a lineNumber and \a
|
|---|
| 309 | columnNumber. This event is not generated for native Qt Script
|
|---|
| 310 | functions.
|
|---|
| 311 |
|
|---|
| 312 | Reimplement this function to handle this event. For example, a
|
|---|
| 313 | debugger implementation could reimplement this function to provide
|
|---|
| 314 | line-by-line stepping, and a profiler implementation could use it to
|
|---|
| 315 | count the number of times each statement is executed.
|
|---|
| 316 |
|
|---|
| 317 | The default implementation does nothing.
|
|---|
| 318 |
|
|---|
| 319 | \sa scriptLoad(), functionEntry()
|
|---|
| 320 | */
|
|---|
| 321 | void QScriptEngineAgent::positionChange(qint64 scriptId,
|
|---|
| 322 | int lineNumber, int columnNumber)
|
|---|
| 323 | {
|
|---|
| 324 | Q_UNUSED(scriptId);
|
|---|
| 325 | Q_UNUSED(lineNumber);
|
|---|
| 326 | Q_UNUSED(columnNumber);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | /*!
|
|---|
| 330 | This function is called when the given \a exception has occurred in
|
|---|
| 331 | the engine, in the script identified by \a scriptId. If the
|
|---|
| 332 | exception was thrown by a native Qt Script function, \a scriptId is
|
|---|
| 333 | -1.
|
|---|
| 334 |
|
|---|
| 335 | If \a hasHandler is true, there is a \c{catch} or \c{finally} block
|
|---|
| 336 | that will handle the exception. If \a hasHandler is false, there is
|
|---|
| 337 | no handler for the exception.
|
|---|
| 338 |
|
|---|
| 339 | Reimplement this function if you want to handle this event. For
|
|---|
| 340 | example, a debugger can notify the user when an uncaught exception
|
|---|
| 341 | occurs (i.e. \a hasHandler is false).
|
|---|
| 342 |
|
|---|
| 343 | The default implementation does nothing.
|
|---|
| 344 |
|
|---|
| 345 | \sa exceptionCatch()
|
|---|
| 346 | */
|
|---|
| 347 | void QScriptEngineAgent::exceptionThrow(qint64 scriptId,
|
|---|
| 348 | const QScriptValue &exception,
|
|---|
| 349 | bool hasHandler)
|
|---|
| 350 | {
|
|---|
| 351 | Q_UNUSED(scriptId);
|
|---|
| 352 | Q_UNUSED(exception);
|
|---|
| 353 | Q_UNUSED(hasHandler);
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | /*!
|
|---|
| 357 | This function is called when the given \a exception is about to be
|
|---|
| 358 | caught, in the script identified by \a scriptId.
|
|---|
| 359 |
|
|---|
| 360 | Reimplement this function if you want to handle this event.
|
|---|
| 361 |
|
|---|
| 362 | The default implementation does nothing.
|
|---|
| 363 |
|
|---|
| 364 | \sa exceptionThrow()
|
|---|
| 365 | */
|
|---|
| 366 | void QScriptEngineAgent::exceptionCatch(qint64 scriptId,
|
|---|
| 367 | const QScriptValue &exception)
|
|---|
| 368 | {
|
|---|
| 369 | Q_UNUSED(scriptId);
|
|---|
| 370 | Q_UNUSED(exception);
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | #if 0
|
|---|
| 374 | /*!
|
|---|
| 375 | This function is called when a property of the given \a object has
|
|---|
| 376 | been added, changed or removed.
|
|---|
| 377 |
|
|---|
| 378 | Reimplement this function if you want to handle this event.
|
|---|
| 379 |
|
|---|
| 380 | The default implementation does nothing.
|
|---|
| 381 | */
|
|---|
| 382 | void QScriptEngineAgent::propertyChange(qint64 scriptId,
|
|---|
| 383 | const QScriptValue &object,
|
|---|
| 384 | const QString &propertyName,
|
|---|
| 385 | PropertyChange change)
|
|---|
| 386 | {
|
|---|
| 387 | Q_UNUSED(scriptId);
|
|---|
| 388 | Q_UNUSED(object);
|
|---|
| 389 | Q_UNUSED(propertyName);
|
|---|
| 390 | Q_UNUSED(change);
|
|---|
| 391 | }
|
|---|
| 392 | #endif
|
|---|
| 393 |
|
|---|
| 394 | /*!
|
|---|
| 395 | Returns true if the QScriptEngineAgent supports the given \a
|
|---|
| 396 | extension; otherwise, false is returned. By default, no extensions
|
|---|
| 397 | are supported.
|
|---|
| 398 |
|
|---|
| 399 | \sa extension()
|
|---|
| 400 | */
|
|---|
| 401 | bool QScriptEngineAgent::supportsExtension(Extension extension) const
|
|---|
| 402 | {
|
|---|
| 403 | Q_UNUSED(extension);
|
|---|
| 404 | return false;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | /*!
|
|---|
| 408 | This virtual function can be reimplemented in a QScriptEngineAgent
|
|---|
| 409 | subclass to provide support for extensions. The optional \a argument
|
|---|
| 410 | can be provided as input to the \a extension; the result must be
|
|---|
| 411 | returned in the form of a QVariant. You can call supportsExtension()
|
|---|
| 412 | to check if an extension is supported by the QScriptEngineAgent. By
|
|---|
| 413 | default, no extensions are supported, and this function returns an
|
|---|
| 414 | invalid QVariant.
|
|---|
| 415 |
|
|---|
| 416 | If you implement the DebuggerInvocationRequest extension, Qt Script
|
|---|
| 417 | will call this function when a \c{debugger} statement is encountered
|
|---|
| 418 | in a script. The \a argument is a QVariantList containing three
|
|---|
| 419 | items: The first item is the scriptId (a qint64), the second item is
|
|---|
| 420 | the line number (an int), and the third item is the column number
|
|---|
| 421 | (an int).
|
|---|
| 422 |
|
|---|
| 423 | \sa supportsExtension()
|
|---|
| 424 | */
|
|---|
| 425 | QVariant QScriptEngineAgent::extension(Extension extension,
|
|---|
| 426 | const QVariant &argument)
|
|---|
| 427 | {
|
|---|
| 428 | Q_UNUSED(extension);
|
|---|
| 429 | Q_UNUSED(argument);
|
|---|
| 430 | return QVariant();
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /*!
|
|---|
| 434 | Returns the QScriptEngine that this agent is associated with.
|
|---|
| 435 | */
|
|---|
| 436 | QScriptEngine *QScriptEngineAgent::engine() const
|
|---|
| 437 | {
|
|---|
| 438 | Q_D(const QScriptEngineAgent);
|
|---|
| 439 | return d->engine;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | QT_END_NAMESPACE
|
|---|
| 443 |
|
|---|
| 444 | #endif // QT_NO_SCRIPT
|
|---|