source: trunk/src/script/qscriptengineagent.cpp@ 284

Last change on this file since 284 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.2 KB
Line 
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
54QT_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
135QScriptEngineAgentPrivate::QScriptEngineAgentPrivate()
136 : engine(0), q_ptr(0)
137{
138}
139
140QScriptEngineAgentPrivate::~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*/
154QScriptEngineAgent::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*/
164QScriptEngineAgent::QScriptEngineAgent(QScriptEngineAgentPrivate &dd, QScriptEngine *engine)
165 : d_ptr(&dd)
166{
167 d_ptr->q_ptr = this;
168 d_ptr->engine = engine;
169}