]> git.saurik.com Git - apple/javascriptcore.git/blob - debugger/DebuggerCallFrame.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / debugger / DebuggerCallFrame.cpp
1 /*
2 * Copyright (C) 2008, 2013, 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "DebuggerCallFrame.h"
31
32 #include "CodeBlock.h"
33 #include "DebuggerEvalEnabler.h"
34 #include "DebuggerScope.h"
35 #include "Interpreter.h"
36 #include "JSFunction.h"
37 #include "JSLexicalEnvironment.h"
38 #include "JSCInlines.h"
39 #include "Parser.h"
40 #include "StackVisitor.h"
41 #include "StrongInlines.h"
42
43 namespace JSC {
44
45 class LineAndColumnFunctor {
46 public:
47 StackVisitor::Status operator()(StackVisitor& visitor)
48 {
49 visitor->computeLineAndColumn(m_line, m_column);
50 return StackVisitor::Done;
51 }
52
53 unsigned line() const { return m_line; }
54 unsigned column() const { return m_column; }
55
56 private:
57 unsigned m_line;
58 unsigned m_column;
59 };
60
61 class FindCallerMidStackFunctor {
62 public:
63 FindCallerMidStackFunctor(CallFrame* callFrame)
64 : m_callFrame(callFrame)
65 , m_callerFrame(nullptr)
66 { }
67
68 StackVisitor::Status operator()(StackVisitor& visitor)
69 {
70 if (visitor->callFrame() == m_callFrame) {
71 m_callerFrame = visitor->callerFrame();
72 return StackVisitor::Done;
73 }
74 return StackVisitor::Continue;
75 }
76
77 CallFrame* getCallerFrame() const { return m_callerFrame; }
78
79 private:
80 CallFrame* m_callFrame;
81 CallFrame* m_callerFrame;
82 };
83
84 DebuggerCallFrame::DebuggerCallFrame(CallFrame* callFrame)
85 : m_callFrame(callFrame)
86 {
87 m_position = positionForCallFrame(m_callFrame);
88 }
89
90 RefPtr<DebuggerCallFrame> DebuggerCallFrame::callerFrame()
91 {
92 ASSERT(isValid());
93 if (!isValid())
94 return 0;
95
96 if (m_caller)
97 return m_caller;
98
99 FindCallerMidStackFunctor functor(m_callFrame);
100 m_callFrame->vm().topCallFrame->iterate(functor);
101
102 CallFrame* callerFrame = functor.getCallerFrame();
103 if (!callerFrame)
104 return nullptr;
105
106 m_caller = DebuggerCallFrame::create(callerFrame);
107 return m_caller;
108 }
109
110 JSC::JSGlobalObject* DebuggerCallFrame::vmEntryGlobalObject() const
111 {
112 ASSERT(isValid());
113 if (!isValid())
114 return 0;
115 return m_callFrame->vmEntryGlobalObject();
116 }
117
118 SourceID DebuggerCallFrame::sourceID() const
119 {
120 ASSERT(isValid());
121 if (!isValid())
122 return noSourceID;
123 return sourceIDForCallFrame(m_callFrame);
124 }
125
126 String DebuggerCallFrame::functionName() const
127 {
128 ASSERT(isValid());
129 if (!isValid())
130 return String();
131 JSFunction* function = jsDynamicCast<JSFunction*>(m_callFrame->callee());
132 if (!function)
133 return String();
134
135 return getCalculatedDisplayName(m_callFrame, function);
136 }
137
138 DebuggerScope* DebuggerCallFrame::scope()
139 {
140 ASSERT(isValid());
141 if (!isValid())
142 return 0;
143
144 if (!m_scope) {
145 VM& vm = m_callFrame->vm();
146 JSScope* scope;
147 CodeBlock* codeBlock = m_callFrame->codeBlock();
148 if (codeBlock && codeBlock->scopeRegister().isValid())
149 scope = m_callFrame->scope(codeBlock->scopeRegister().offset());
150 else if (JSCallee* callee = jsDynamicCast<JSCallee*>(m_callFrame->callee()))
151 scope = callee->scope();
152 else
153 scope = m_callFrame->lexicalGlobalObject();
154
155 m_scope.set(vm, DebuggerScope::create(vm, scope));
156 }
157 return m_scope.get();
158 }
159
160 DebuggerCallFrame::Type DebuggerCallFrame::type() const
161 {
162 ASSERT(isValid());
163 if (!isValid())
164 return ProgramType;
165
166 if (jsDynamicCast<JSFunction*>(m_callFrame->callee()))
167 return FunctionType;
168
169 return ProgramType;
170 }
171
172 JSValue DebuggerCallFrame::thisValue() const
173 {
174 ASSERT(isValid());
175 return thisValueForCallFrame(m_callFrame);
176 }
177
178 // Evaluate some JavaScript code in the scope of this frame.
179 JSValue DebuggerCallFrame::evaluate(const String& script, NakedPtr<Exception>& exception)
180 {
181 ASSERT(isValid());
182 CallFrame* callFrame = m_callFrame;
183 if (!callFrame)
184 return jsNull();
185
186 JSLockHolder lock(callFrame);
187
188 if (!callFrame->codeBlock())
189 return JSValue();
190
191 DebuggerEvalEnabler evalEnabler(callFrame);
192 VM& vm = callFrame->vm();
193 auto& codeBlock = *callFrame->codeBlock();
194 ThisTDZMode thisTDZMode = codeBlock.unlinkedCodeBlock()->constructorKind() == ConstructorKind::Derived ? ThisTDZMode::AlwaysCheck : ThisTDZMode::CheckIfNeeded;
195 EvalExecutable* eval = EvalExecutable::create(callFrame, makeSource(script), codeBlock.isStrictMode(), thisTDZMode);
196 if (vm.exception()) {
197 exception = vm.exception();
198 vm.clearException();
199 return jsUndefined();
200 }
201
202 JSValue thisValue = thisValueForCallFrame(callFrame);
203 JSValue result = vm.interpreter->execute(eval, callFrame, thisValue, scope()->jsScope());
204 if (vm.exception()) {
205 exception = vm.exception();
206 vm.clearException();
207 }
208 ASSERT(result);
209 return result;
210 }
211
212 void DebuggerCallFrame::invalidate()
213 {
214 RefPtr<DebuggerCallFrame> frame = this;
215 while (frame) {
216 frame->m_callFrame = nullptr;
217 if (frame->m_scope) {
218 frame->m_scope->invalidateChain();
219 frame->m_scope.clear();
220 }
221 frame = frame->m_caller.release();
222 }
223 }
224
225 TextPosition DebuggerCallFrame::positionForCallFrame(CallFrame* callFrame)
226 {
227 if (!callFrame)
228 return TextPosition();
229
230 LineAndColumnFunctor functor;
231 callFrame->iterate(functor);
232 return TextPosition(OrdinalNumber::fromOneBasedInt(functor.line()), OrdinalNumber::fromOneBasedInt(functor.column()));
233 }
234
235 SourceID DebuggerCallFrame::sourceIDForCallFrame(CallFrame* callFrame)
236 {
237 ASSERT(callFrame);
238 CodeBlock* codeBlock = callFrame->codeBlock();
239 if (!codeBlock)
240 return noSourceID;
241 return codeBlock->ownerExecutable()->sourceID();
242 }
243
244 JSValue DebuggerCallFrame::thisValueForCallFrame(CallFrame* callFrame)
245 {
246 if (!callFrame)
247 return jsNull();
248
249 ECMAMode ecmaMode = NotStrictMode;
250 CodeBlock* codeBlock = callFrame->codeBlock();
251 if (codeBlock && codeBlock->isStrictMode())
252 ecmaMode = StrictMode;
253 JSValue thisValue = callFrame->thisValue().toThis(callFrame, ecmaMode);
254 return thisValue;
255 }
256
257 } // namespace JSC