]> git.saurik.com Git - apple/javascriptcore.git/blame - debugger/Debugger.cpp
JavaScriptCore-1218.35.tar.gz
[apple/javascriptcore.git] / debugger / Debugger.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
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 "config.h"
23#include "Debugger.h"
24
f9bf01c6 25#include "Error.h"
9dae56ea 26#include "Interpreter.h"
f9bf01c6
A
27#include "JSFunction.h"
28#include "JSGlobalObject.h"
93a37866 29#include "Operations.h"
9dae56ea 30#include "Parser.h"
f9bf01c6 31#include "Protect.h"
9dae56ea 32
14957cd0
A
33namespace {
34
35using namespace JSC;
36
6fe7ccc8 37class Recompiler : public MarkedBlock::VoidFunctor {
14957cd0 38public:
93a37866 39#if PLATFORM(IOS)
6fe7ccc8 40 Recompiler(JSC::Debugger*);
93a37866
A
41#else
42 Recompiler(Debugger*);
43#endif
14957cd0
A
44 ~Recompiler();
45 void operator()(JSCell*);
46
47private:
48 typedef HashSet<FunctionExecutable*> FunctionExecutableSet;
49 typedef HashMap<SourceProvider*, ExecState*> SourceProviderMap;
50
93a37866 51#if PLATFORM(IOS)
6fe7ccc8 52 JSC::Debugger* m_debugger;
93a37866
A
53#else
54 Debugger* m_debugger;
55#endif
14957cd0
A
56 FunctionExecutableSet m_functionExecutables;
57 SourceProviderMap m_sourceProviders;
58};
59
93a37866 60#if PLATFORM(IOS)
6fe7ccc8 61inline Recompiler::Recompiler(JSC::Debugger* debugger)
93a37866
A
62#else
63inline Recompiler::Recompiler(Debugger* debugger)
64#endif
14957cd0
A
65 : m_debugger(debugger)
66{
67}
68
69inline Recompiler::~Recompiler()
70{
71 // Call sourceParsed() after reparsing all functions because it will execute
72 // JavaScript in the inspector.
73 SourceProviderMap::const_iterator end = m_sourceProviders.end();
74 for (SourceProviderMap::const_iterator iter = m_sourceProviders.begin(); iter != end; ++iter)
93a37866 75 m_debugger->sourceParsed(iter->value, iter->key, -1, String());
14957cd0
A
76}
77
78inline void Recompiler::operator()(JSCell* cell)
79{
80 if (!cell->inherits(&JSFunction::s_info))
81 return;
82
6fe7ccc8 83 JSFunction* function = jsCast<JSFunction*>(cell);
14957cd0
A
84 if (function->executable()->isHostFunction())
85 return;
86
87 FunctionExecutable* executable = function->jsExecutable();
88
89 // Check if the function is already in the set - if so,
90 // we've already retranslated it, nothing to do here.
6fe7ccc8 91 if (!m_functionExecutables.add(executable).isNewEntry)
14957cd0
A
92 return;
93
93a37866
A
94 ExecState* exec = function->scope()->globalObject()->JSGlobalObject::globalExec();
95 executable->clearCodeIfNotCompiling();
96 executable->clearUnlinkedCodeForRecompilationIfNotCompiling();
97 if (m_debugger == function->scope()->globalObject()->debugger())
14957cd0
A
98 m_sourceProviders.add(executable->source().provider(), exec);
99}
100
101} // namespace
102
9dae56ea
A
103namespace JSC {
104
9dae56ea
A
105Debugger::~Debugger()
106{
107 HashSet<JSGlobalObject*>::iterator end = m_globalObjects.end();
108 for (HashSet<JSGlobalObject*>::iterator it = m_globalObjects.begin(); it != end; ++it)
109 (*it)->setDebugger(0);
110}
111
112void Debugger::attach(JSGlobalObject* globalObject)
113{
114 ASSERT(!globalObject->debugger());
115 globalObject->setDebugger(this);
116 m_globalObjects.add(globalObject);
117}
118
119void Debugger::detach(JSGlobalObject* globalObject)
120{
121 ASSERT(m_globalObjects.contains(globalObject));
122 m_globalObjects.remove(globalObject);
123 globalObject->setDebugger(0);
124}
125
93a37866 126void Debugger::recompileAllJSFunctions(VM* vm)
f9bf01c6
A
127{
128 // If JavaScript is running, it's not safe to recompile, since we'll end
129 // up throwing away code that is live on the stack.
93a37866
A
130 ASSERT(!vm->dynamicGlobalObject);
131 if (vm->dynamicGlobalObject)
f9bf01c6
A
132 return;
133
14957cd0 134 Recompiler recompiler(this);
93a37866 135 vm->heap.objectSpace().forEachLiveCell(recompiler);
f9bf01c6
A
136}
137
93a37866 138JSValue evaluateInGlobalCallFrame(const String& script, JSValue& exception, JSGlobalObject* globalObject)
9dae56ea
A
139{
140 CallFrame* globalCallFrame = globalObject->globalExec();
93a37866 141 VM& vm = globalObject->vm();
9dae56ea 142
93a37866 143 EvalExecutable* eval = EvalExecutable::create(globalCallFrame, vm.codeCache(), makeSource(script), false);
14957cd0 144 if (!eval) {
93a37866
A
145 exception = vm.exception;
146 vm.exception = JSValue();
14957cd0
A
147 return exception;
148 }
9dae56ea 149
93a37866
A
150 JSValue result = vm.interpreter->execute(eval, globalCallFrame, globalObject, globalCallFrame->scope());
151 if (vm.exception) {
152 exception = vm.exception;
153 vm.exception = JSValue();
14957cd0
A
154 }
155 ASSERT(result);
156 return result;
9dae56ea
A
157}
158
159} // namespace JSC