fix logic
[personal-kdelibs.git] / kjs / debugger.cpp
blobb0b8c4b7f970c307b0082ea919a6437b69686ed1
1 // -*- c-basic-offset: 2 -*-
2 /*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
5 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "debugger.h"
24 #include "nodes.h"
25 #include <config.h>
26 #include "ustring.h"
28 #include "internal.h"
30 using namespace KJS;
32 // ------------------------------ Debugger -------------------------------------
34 namespace KJS {
35 struct AttachedInterpreter
37 AttachedInterpreter(Interpreter *i, AttachedInterpreter *ai) : interp(i), next(ai) { ++Debugger::debuggersPresent; }
38 ~AttachedInterpreter() { --Debugger::debuggersPresent; }
39 Interpreter *interp;
40 AttachedInterpreter *next;
45 int Debugger::debuggersPresent = 0;
47 Debugger::Debugger()
49 lastLineRan = 0;
50 rep = new DebuggerImp();
53 Debugger::~Debugger()
55 detach(0);
56 delete rep;
59 void Debugger::attach(Interpreter* interp)
61 Debugger *other = interp->debugger();
62 if (other == this)
63 return;
64 if (other)
65 other->detach(interp);
66 interp->setDebugger(this);
67 rep->interps = new AttachedInterpreter(interp, rep->interps);
70 void Debugger::detach(Interpreter* interp)
72 // iterate the addresses where AttachedInterpreter pointers are stored
73 // so we can unlink items from the list
74 AttachedInterpreter **p = &rep->interps;
75 AttachedInterpreter *q;
76 while ((q = *p)) {
77 if (!interp || q->interp == interp) {
78 *p = q->next;
79 q->interp->setDebugger(0);
80 delete q;
81 } else
82 p = &q->next;
85 if (interp)
86 latestExceptions.remove(interp);
87 else
88 latestExceptions.clear();
91 bool Debugger::hasHandledException(ExecState *exec, JSValue *exception)
93 if (latestExceptions.get(exec->dynamicInterpreter()).get() == exception)
94 return true;
96 latestExceptions.set(exec->dynamicInterpreter(), exception);
97 return false;
100 bool Debugger::sourceParsed(ExecState * /*exec*/, int /*sourceId*/, const UString &/*sourceURL*/,
101 const UString &/*source*/, int /*startingLineNumber*/, int /*errorLine*/, const UString & /*errorMsg*/)
103 return true;
106 bool Debugger::exception(ExecState * /*exec*/, int /*sourceId*/, int /*lineno*/,
107 JSValue * /*exception*/)
109 return true;
112 bool Debugger::atStatement(ExecState * /*exec*/, int /*sourceId*/, int /*firstLine*/,
113 int /*lastLine*/)
115 return true;
118 void Debugger::reportAtStatement(ExecState *exec, int sourceId, int firstLine, int lastLine)
120 lastLineRan = firstLine;
121 atStatement(exec, sourceId, firstLine, lastLine);
124 void Debugger::reportException(ExecState *exec, JSValue *exceptionVal)
126 if (!hasHandledException(exec, exceptionVal))
127 exception(exec, exec->currentBody()->sourceId(), lastLineRan, exceptionVal);
130 bool Debugger::enterContext(ExecState * /*exec*/, int /*sourceId*/, int /*lineno*/,
131 JSObject * /*function*/, const List & /*args*/)
133 return true;
136 bool Debugger::exitContext(ExecState * /*exec*/, int /*sourceId*/, int /*lineno*/,
137 JSObject * /*function*/)
139 return true;
142 bool Debugger::shouldReindentSources() const
144 return false;
147 bool Debugger::shouldReportCaught() const
149 return false;
152 void Debugger::reportSourceParsed(ExecState *exec, FunctionBodyNode *body, const UString &source,
153 int startingLineNumber, int errorLine, const UString &errorMsg)
155 UString code = source;
156 if (shouldReindentSources())
157 code = body->reindent(startingLineNumber);
158 sourceParsed(exec, body->sourceId(), body->sourceURL(), code, startingLineNumber, errorLine, errorMsg);