]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | #include "Error.h" | |
26 | #include "Interpreter.h" | |
27 | #include "JSFunction.h" | |
28 | #include "JSGlobalObject.h" | |
29 | #include "Operations.h" | |
30 | #include "Parser.h" | |
31 | #include "Protect.h" | |
32 | ||
33 | namespace { | |
34 | ||
35 | using namespace JSC; | |
36 | ||
37 | class Recompiler : public MarkedBlock::VoidFunctor { | |
38 | public: | |
39 | #if PLATFORM(IOS) | |
40 | Recompiler(JSC::Debugger*); | |
41 | #else | |
42 | Recompiler(Debugger*); | |
43 | #endif | |
44 | ~Recompiler(); | |
45 | void operator()(JSCell*); | |
46 | ||
47 | private: | |
48 | typedef HashSet<FunctionExecutable*> FunctionExecutableSet; | |
49 | typedef HashMap<SourceProvider*, ExecState*> SourceProviderMap; | |
50 | ||
51 | #if PLATFORM(IOS) | |
52 | JSC::Debugger* m_debugger; | |
53 | #else | |
54 | Debugger* m_debugger; | |
55 | #endif | |
56 | FunctionExecutableSet m_functionExecutables; | |
57 | SourceProviderMap m_sourceProviders; | |
58 | }; | |
59 | ||
60 | #if PLATFORM(IOS) | |
61 | inline Recompiler::Recompiler(JSC::Debugger* debugger) | |
62 | #else | |
63 | inline Recompiler::Recompiler(Debugger* debugger) | |
64 | #endif | |
65 | : m_debugger(debugger) | |
66 | { | |
67 | } | |
68 | ||
69 | inline 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) | |
75 | m_debugger->sourceParsed(iter->value, iter->key, -1, String()); | |
76 | } | |
77 | ||
78 | inline void Recompiler::operator()(JSCell* cell) | |
79 | { | |
80 | if (!cell->inherits(&JSFunction::s_info)) | |
81 | return; | |
82 | ||
83 | JSFunction* function = jsCast<JSFunction*>(cell); | |
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. | |
91 | if (!m_functionExecutables.add(executable).isNewEntry) | |
92 | return; | |
93 | ||
94 | ExecState* exec = function->scope()->globalObject()->JSGlobalObject::globalExec(); | |
95 | executable->clearCodeIfNotCompiling(); | |
96 | executable->clearUnlinkedCodeForRecompilationIfNotCompiling(); | |
97 | if (m_debugger == function->scope()->globalObject()->debugger()) | |
98 | m_sourceProviders.add(executable->source().provider(), exec); | |
99 | } | |
100 | ||
101 | } // namespace | |
102 | ||
103 | namespace JSC { | |
104 | ||
105 | Debugger::~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 | ||
112 | void Debugger::attach(JSGlobalObject* globalObject) | |
113 | { | |
114 | ASSERT(!globalObject->debugger()); | |
115 | globalObject->setDebugger(this); | |
116 | m_globalObjects.add(globalObject); | |
117 | } | |
118 | ||
119 | void Debugger::detach(JSGlobalObject* globalObject) | |
120 | { | |
121 | ASSERT(m_globalObjects.contains(globalObject)); | |
122 | m_globalObjects.remove(globalObject); | |
123 | globalObject->setDebugger(0); | |
124 | } | |
125 | ||
126 | void Debugger::recompileAllJSFunctions(VM* vm) | |
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. | |
130 | ASSERT(!vm->dynamicGlobalObject); | |
131 | if (vm->dynamicGlobalObject) | |
132 | return; | |
133 | ||
134 | Recompiler recompiler(this); | |
135 | vm->heap.objectSpace().forEachLiveCell(recompiler); | |
136 | } | |
137 | ||
138 | JSValue evaluateInGlobalCallFrame(const String& script, JSValue& exception, JSGlobalObject* globalObject) | |
139 | { | |
140 | CallFrame* globalCallFrame = globalObject->globalExec(); | |
141 | VM& vm = globalObject->vm(); | |
142 | ||
143 | EvalExecutable* eval = EvalExecutable::create(globalCallFrame, vm.codeCache(), makeSource(script), false); | |
144 | if (!eval) { | |
145 | exception = vm.exception; | |
146 | vm.exception = JSValue(); | |
147 | return exception; | |
148 | } | |
149 | ||
150 | JSValue result = vm.interpreter->execute(eval, globalCallFrame, globalObject, globalCallFrame->scope()); | |
151 | if (vm.exception) { | |
152 | exception = vm.exception; | |
153 | vm.exception = JSValue(); | |
154 | } | |
155 | ASSERT(result); | |
156 | return result; | |
157 | } | |
158 | ||
159 | } // namespace JSC |