KJS

debugger.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2001 Harri Porten ([email protected])
4  * Copyright (C) 2001 Peter Kelly ([email protected])
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 
22 #include "debugger.h"
23 #include "nodes.h"
24 #include "ustring.h"
25 
26 #include "internal.h"
27 
28 using namespace KJS;
29 
30 // ------------------------------ Debugger -------------------------------------
31 
32 namespace KJS
33 {
34 struct AttachedInterpreter {
35  AttachedInterpreter(Interpreter *i, AttachedInterpreter *ai) : interp(i), next(ai)
36  {
37  ++Debugger::debuggersPresent;
38  }
39  ~AttachedInterpreter()
40  {
41  --Debugger::debuggersPresent;
42  }
43  Interpreter *interp;
44  AttachedInterpreter *next;
45 };
46 
47 }
48 
49 int Debugger::debuggersPresent = 0;
50 
52 {
53  lastLineRan = 0;
54  rep = new DebuggerImp();
55  lastSourceParsed = -1;
56 }
57 
59 {
60  detach(nullptr);
61  delete rep;
62 }
63 
65 {
66  Debugger *other = interp->debugger();
67  if (other == this) {
68  return;
69  }
70  if (other) {
71  other->detach(interp);
72  }
73  interp->setDebugger(this);
74  rep->interps = new AttachedInterpreter(interp, rep->interps);
75 }
76 
78 {
79  // iterate the addresses where AttachedInterpreter pointers are stored
80  // so we can unlink items from the list
81  AttachedInterpreter **p = &rep->interps;
82  AttachedInterpreter *q;
83  while ((q = *p)) {
84  if (!interp || q->interp == interp) {
85  *p = q->next;
86  q->interp->setDebugger(nullptr);
87  delete q;
88  } else {
89  p = &q->next;
90  }
91  }
92 
93  if (interp) {
94  latestExceptions.remove(interp);
95  } else {
96  latestExceptions.clear();
97  }
98 }
99 
100 bool Debugger::hasHandledException(ExecState *exec, JSValue *exception)
101 {
102  if (latestExceptions.get(exec->dynamicInterpreter()).get() == exception) {
103  return true;
104  }
105 
106  latestExceptions.set(exec->dynamicInterpreter(), exception);
107  return false;
108 }
109 
110 bool Debugger::sourceParsed(ExecState * /*exec*/, int /*sourceId*/, const UString &/*sourceURL*/,
111  const UString &/*source*/, int /*startingLineNumber*/, int /*errorLine*/, const UString & /*errorMsg*/)
112 {
113  return true;
114 }
115 
116 bool Debugger::exception(ExecState * /*exec*/, int /*sourceId*/, int /*lineno*/,
117  JSValue * /*exception*/)
118 {
119  return true;
120 }
121 
122 bool Debugger::atStatement(ExecState * /*exec*/, int /*sourceId*/, int /*firstLine*/,
123  int /*lastLine*/)
124 {
125  return true;
126 }
127 
128 void Debugger::reportAtStatement(ExecState *exec, int sourceId, int firstLine, int lastLine)
129 {
130  lastLineRan = firstLine;
131  atStatement(exec, sourceId, firstLine, lastLine);
132 }
133 
134 void Debugger::reportException(ExecState *exec, JSValue *exceptionVal)
135 {
136  if (!hasHandledException(exec, exceptionVal)) {
137  exception(exec, exec->currentBody() ? exec->currentBody()->sourceId() : lastSourceParsed, lastLineRan, exceptionVal);
138  }
139 }
140 
141 bool Debugger::enterContext(ExecState * /*exec*/, int /*sourceId*/, int /*lineno*/,
142  JSObject * /*function*/, const List & /*args*/)
143 {
144  return true;
145 }
146 
147 bool Debugger::exitContext(ExecState * /*exec*/, int /*sourceId*/, int /*lineno*/,
148  JSObject * /*function*/)
149 {
150  return true;
151 }
152 
153 bool Debugger::shouldReindentSources() const
154 {
155  return false;
156 }
157 
158 bool Debugger::shouldReportCaught() const
159 {
160  return false;
161 }
162 
163 void Debugger::reportSourceParsed(ExecState *exec, FunctionBodyNode *body,
164  int sourceId, UString sourceURL, const UString &source,
165  int startingLineNumber, int errorLine, const UString &errorMsg)
166 {
167  lastSourceParsed = sourceId;
168  UString code = source;
169  if (shouldReindentSources() && body) {
170  code = body->reindent(startingLineNumber);
171  }
172  sourceParsed(exec, sourceId, sourceURL, code, startingLineNumber, errorLine, errorMsg);
173 }
virtual void detach(Interpreter *interp)
Detach the debugger from an interpreter.
Definition: debugger.cpp:77
virtual bool exception(ExecState *exec, int sourceId, int lineno, JSValue *exception)
Called when an exception is thrown during script execution.
Definition: debugger.cpp:116
This AST node corresponds to the function body or top-level code in the AST, but is used to keep trac...
Definition: nodes.h:1276
JSValue is the base type for all primitives (Undefined, Null, Boolean, String, Number) and objects in...
Definition: value.h:58
Debugger()
Creates a new debugger.
Definition: debugger.cpp:51
virtual bool atStatement(ExecState *exec, int sourceId, int firstLine, int lastLine)
Called when a line of the script is reached (before it is executed)
Definition: debugger.cpp:122
virtual ~Debugger()
Destroys the debugger.
Definition: debugger.cpp:58
Represents the current state of script execution.
Definition: ExecState.h:53
Interpreter objects can be used to evaluate ECMAScript code.
Definition: interpreter.h:56
virtual bool enterContext(ExecState *exec, int sourceId, int lineno, JSObject *function, const List &args)
Called when the interpreter enters a new execution context (stack frame).
Definition: debugger.cpp:141
Native list type.
Definition: list.h:52
virtual bool sourceParsed(ExecState *exec, int sourceId, const UString &sourceURL, const UString &source, int startingLineNumber, int errorLine, const UString &errorMsg)
Called to notify the debugger that some javascript source code has been parsed.
Definition: debugger.cpp:110
virtual bool exitContext(ExecState *exec, int sourceId, int lineno, JSObject *function)
Called when the interpreter exits an execution context.
Definition: debugger.cpp:147
Interpreter * dynamicInterpreter() const
Returns the interpreter associated with this execution state.
Definition: ExecState.h:64
virtual void attach(Interpreter *interp)
Attaches the debugger to specified interpreter.
Definition: debugger.cpp:64
QAction * next(const QObject *recvr, const char *slot, QObject *parent)
Unicode string class.
Definition: ustring.h:153
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Sep 30 2023 03:56:09 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.