1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtScript module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL-ONLY$
|
---|
10 | ** GNU Lesser General Public License Usage
|
---|
11 | ** This file may be used under the terms of the GNU Lesser
|
---|
12 | ** General Public License version 2.1 as published by the Free Software
|
---|
13 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
14 | ** packaging of this file. Please review the following information to
|
---|
15 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
17 | **
|
---|
18 | ** If you have questions regarding the use of this file, please contact
|
---|
19 | ** Nokia at [email protected].
|
---|
20 | ** $QT_END_LICENSE$
|
---|
21 | **
|
---|
22 | ****************************************************************************/
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 | #include "qscriptengineagent.h"
|
---|
26 | #include "qscriptengineagent_p.h"
|
---|
27 | #include "qscriptengine.h"
|
---|
28 | #include "qscriptengine_p.h"
|
---|
29 |
|
---|
30 | #include "CodeBlock.h"
|
---|
31 | #include "Instruction.h"
|
---|
32 |
|
---|
33 | QT_BEGIN_NAMESPACE
|
---|
34 |
|
---|
35 | /*!
|
---|
36 | \since 4.4
|
---|
37 | \class QScriptEngineAgent
|
---|
38 |
|
---|
39 | \brief The QScriptEngineAgent class provides an interface to report events pertaining to QScriptEngine execution.
|
---|
40 |
|
---|
41 | \ingroup script
|
---|
42 |
|
---|
43 |
|
---|
44 | The QScriptEngineAgent class is the basis of tools that monitor and/or control the execution of a
|
---|
45 | QScriptEngine, such as debuggers and profilers.
|
---|
46 |
|
---|
47 | To process script loading and unloading events, reimplement the
|
---|
48 | scriptLoad() and scriptUnload() functions. scriptLoad() is called
|
---|
49 | after the input to QScriptEngine::evaluate() has been parsed, right
|
---|
50 | before the given script is executed. The engine assigns each
|
---|
51 | script an ID, which is available as one of the arguments to
|
---|
52 | scriptLoad(); subsequently, other event handlers can use the ID to
|
---|
53 | identify a particular script. One common usage of scriptLoad() is
|
---|
54 | to retain the script text, filename and base line number (the
|
---|
55 | original input to QScriptEngine::evaluate()), so that other event
|
---|
56 | handlers can e.g. map a line number to the corresponding line of
|
---|
57 | text.
|
---|
58 |
|
---|
59 | scriptUnload() is called when the QScriptEngine has no further use
|
---|
60 | for a script; the QScriptEngineAgent may at this point safely
|
---|
61 | discard any resources associated with the script (such as the
|
---|
62 | script text). Note that after scriptUnload() has been called, the
|
---|
63 | QScriptEngine may reuse the relevant script ID for new scripts
|
---|
64 | (i.e. as argument to a subsequent call to scriptLoad()).
|
---|
65 |
|
---|
66 | Evaluating the following script will result in scriptUnload()
|
---|
67 | being called immediately after evaluation has completed:
|
---|
68 |
|
---|
69 | \snippet doc/src/snippets/code/src_script_qscriptengineagent.cpp 0
|
---|
70 |
|
---|
71 | Evaluating the following script will \b{not} result in a call to
|
---|
72 | scriptUnload() when evaluation has completed:
|
---|
73 |
|
---|
74 | \snippet doc/src/snippets/code/src_script_qscriptengineagent.cpp 1
|
---|
75 |
|
---|
76 | The script isn't unloaded because it defines a function (\c{cube})
|
---|
77 | that remains in the script environment after evaluation has
|
---|
78 | completed. If a subsequent script removed the \c{cube} function
|
---|
79 | (e.g. by setting it to \c{null}), scriptUnload() would be called
|
---|
80 | when the function is garbage collected. In general terms, a script
|
---|
81 | isn't unloaded until the engine has determined that none of its
|
---|
82 | contents is referenced.
|
---|
83 |
|
---|
84 | To process script function calls and returns, reimplement the
|
---|
85 | functionEntry() and functionExit() functions. functionEntry() is
|
---|
86 | called when a script function is about to be executed;
|
---|
87 | functionExit() is called when a script function is about to return,
|
---|
88 | either normally or due to an exception.
|
---|
89 |
|
---|
90 | To process individual script statements, reimplement
|
---|
91 | positionChange(). positionChange() is called each time the engine is
|
---|
92 | about to execute a new statement of a script, and thus offers the
|
---|
93 | finest level of script monitoring.
|
---|
94 |
|
---|
95 | To process exceptions, reimplement exceptionThrow() and
|
---|
96 | exceptionCatch(). exceptionThrow() is called when a script exception
|
---|
97 | is thrown, before it has been handled. exceptionCatch() is called
|
---|
98 | when an exception handler is present, and execution is about to be
|
---|
99 | resumed at the handler code.
|
---|
100 |
|
---|
101 | \sa QScriptEngine::setAgent(), QScriptContextInfo
|
---|
102 | */
|
---|
103 |
|
---|
104 | /*!
|
---|
105 | \enum QScriptEngineAgent::Extension
|
---|
106 |
|
---|
107 | This enum specifies the possible extensions to a QScriptEngineAgent.
|
---|
108 |
|
---|
109 | \value DebuggerInvocationRequest The agent handles \c{debugger} script statements.
|
---|
110 |
|
---|
111 | \sa extension()
|
---|
112 | */
|
---|
113 |
|
---|
114 |
|
---|
115 | void QScriptEngineAgentPrivate::attach()
|
---|
116 | {
|
---|
117 | if (engine->originalGlobalObject()->debugger())
|
---|
118 | engine->originalGlobalObject()->setDebugger(0);
|
---|
119 | JSC::Debugger::attach(engine->originalGlobalObject());
|
---|
120 | }
|
---|
121 |
|
---|
122 | void QScriptEngineAgentPrivate::detach()
|
---|
|
---|