]> git.saurik.com Git - apple/javascriptcore.git/blame - debugger/DebuggerCallFrame.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / debugger / DebuggerCallFrame.cpp
CommitLineData
b5422865 1/*
81345200 2 * Copyright (C) 2008, 2013, 2014 Apple Inc. All rights reserved.
b5422865
A
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.
81345200 13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
b5422865
A
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
9dae56ea
A
29#include "config.h"
30#include "DebuggerCallFrame.h"
b5422865 31
9dae56ea 32#include "CodeBlock.h"
ed1e77d3
A
33#include "DebuggerEvalEnabler.h"
34#include "DebuggerScope.h"
9dae56ea 35#include "Interpreter.h"
81345200 36#include "JSFunction.h"
ed1e77d3 37#include "JSLexicalEnvironment.h"
81345200 38#include "JSCInlines.h"
9dae56ea 39#include "Parser.h"
81345200 40#include "StackVisitor.h"
ed1e77d3 41#include "StrongInlines.h"
b5422865 42
9dae56ea 43namespace JSC {
b5422865 44
81345200
A
45class LineAndColumnFunctor {
46public:
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
56private:
57 unsigned m_line;
58 unsigned m_column;
59};
60
ed1e77d3
A
61class FindCallerMidStackFunctor {
62public:
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
79private:
80 CallFrame* m_callFrame;
81 CallFrame* m_callerFrame;
82};
83
81345200
A
84DebuggerCallFrame::DebuggerCallFrame(CallFrame* callFrame)
85 : m_callFrame(callFrame)
9dae56ea 86{
81345200
A
87 m_position = positionForCallFrame(m_callFrame);
88}
b5422865 89
ed1e77d3 90RefPtr<DebuggerCallFrame> DebuggerCallFrame::callerFrame()
81345200
A
91{
92 ASSERT(isValid());
93 if (!isValid())
94 return 0;
f9bf01c6 95
81345200
A
96 if (m_caller)
97 return m_caller;
98
ed1e77d3
A
99 FindCallerMidStackFunctor functor(m_callFrame);
100 m_callFrame->vm().topCallFrame->iterate(functor);
101
102 CallFrame* callerFrame = functor.getCallerFrame();
81345200 103 if (!callerFrame)
ed1e77d3 104 return nullptr;
81345200
A
105
106 m_caller = DebuggerCallFrame::create(callerFrame);
107 return m_caller;
9dae56ea 108}
81345200
A
109
110JSC::JSGlobalObject* DebuggerCallFrame::vmEntryGlobalObject() const
ba379fdc 111{
81345200
A
112 ASSERT(isValid());
113 if (!isValid())
114 return 0;
115 return m_callFrame->vmEntryGlobalObject();
116}
f9bf01c6 117
81345200
A
118SourceID DebuggerCallFrame::sourceID() const
119{
120 ASSERT(isValid());
121 if (!isValid())
122 return noSourceID;
123 return sourceIDForCallFrame(m_callFrame);
124}
6fe7ccc8 125
81345200
A
126String DebuggerCallFrame::functionName() const
127{
128 ASSERT(isValid());
129 if (!isValid())
130 return String();
ed1e77d3 131 JSFunction* function = jsDynamicCast<JSFunction*>(m_callFrame->callee());
6fe7ccc8 132 if (!function)
93a37866 133 return String();
f9bf01c6 134
6fe7ccc8 135 return getCalculatedDisplayName(m_callFrame, function);
ba379fdc 136}
b5422865 137
ed1e77d3 138DebuggerScope* DebuggerCallFrame::scope()
81345200
A
139{
140 ASSERT(isValid());
141 if (!isValid())
142 return 0;
143
ed1e77d3
A
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));
81345200 156 }
ed1e77d3 157 return m_scope.get();
81345200
A
158}
159
9dae56ea
A
160DebuggerCallFrame::Type DebuggerCallFrame::type() const
161{
81345200
A
162 ASSERT(isValid());
163 if (!isValid())
164 return ProgramType;
165
ed1e77d3 166 if (jsDynamicCast<JSFunction*>(m_callFrame->callee()))
9dae56ea 167 return FunctionType;
b5422865 168
9dae56ea
A
169 return ProgramType;
170}
b5422865 171
81345200 172JSValue DebuggerCallFrame::thisValue() const
9dae56ea 173{
81345200
A
174 ASSERT(isValid());
175 return thisValueForCallFrame(m_callFrame);
9dae56ea 176}
b5422865 177
81345200 178// Evaluate some JavaScript code in the scope of this frame.
ed1e77d3 179JSValue DebuggerCallFrame::evaluate(const String& script, NakedPtr<Exception>& exception)
9dae56ea 180{
81345200
A
181 ASSERT(isValid());
182 CallFrame* callFrame = m_callFrame;
183 if (!callFrame)
184 return jsNull();
185
186 JSLockHolder lock(callFrame);
187
188 if (!callFrame->codeBlock())
ba379fdc 189 return JSValue();
14957cd0 190
ed1e77d3 191 DebuggerEvalEnabler evalEnabler(callFrame);
81345200 192 VM& vm = callFrame->vm();
ed1e77d3
A
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);
81345200
A
196 if (vm.exception()) {
197 exception = vm.exception();
198 vm.clearException();
199 return jsUndefined();
14957cd0
A
200 }
201
81345200 202 JSValue thisValue = thisValueForCallFrame(callFrame);
ed1e77d3 203 JSValue result = vm.interpreter->execute(eval, callFrame, thisValue, scope()->jsScope());
81345200
A
204 if (vm.exception()) {
205 exception = vm.exception();
206 vm.clearException();
14957cd0
A
207 }
208 ASSERT(result);
209 return result;
9dae56ea
A
210}
211
81345200
A
212void DebuggerCallFrame::invalidate()
213{
ed1e77d3 214 RefPtr<DebuggerCallFrame> frame = this;
81345200
A
215 while (frame) {
216 frame->m_callFrame = nullptr;
ed1e77d3
A
217 if (frame->m_scope) {
218 frame->m_scope->invalidateChain();
219 frame->m_scope.clear();
220 }
81345200
A
221 frame = frame->m_caller.release();
222 }
223}
224
225TextPosition 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
235SourceID 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
244JSValue 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
9dae56ea 257} // namespace JSC