]> git.saurik.com Git - apple/javascriptcore.git/blob - inspector/JSJavaScriptCallFrame.cpp
08fd618a804d516dd415db673a524d702cc3e6be
[apple/javascriptcore.git] / inspector / JSJavaScriptCallFrame.cpp
1 /*
2 * Copyright (C) 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "JSJavaScriptCallFrame.h"
28
29 #if ENABLE(INSPECTOR)
30
31 #include "Error.h"
32 #include "JSCJSValue.h"
33 #include "JSCellInlines.h"
34 #include "JSJavaScriptCallFramePrototype.h"
35 #include "StructureInlines.h"
36
37 using namespace JSC;
38
39 namespace Inspector {
40
41 const ClassInfo JSJavaScriptCallFrame::s_info = { "JavaScriptCallFrame", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSJavaScriptCallFrame) };
42
43 JSJavaScriptCallFrame::JSJavaScriptCallFrame(VM& vm, Structure* structure, PassRefPtr<JavaScriptCallFrame> impl)
44 : JSDestructibleObject(vm, structure)
45 , m_impl(impl.leakRef())
46 {
47 }
48
49 void JSJavaScriptCallFrame::finishCreation(VM& vm)
50 {
51 Base::finishCreation(vm);
52 ASSERT(inherits(info()));
53 }
54
55 JSObject* JSJavaScriptCallFrame::createPrototype(VM& vm, JSGlobalObject* globalObject)
56 {
57 return JSJavaScriptCallFramePrototype::create(vm, globalObject, JSJavaScriptCallFramePrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
58 }
59
60 void JSJavaScriptCallFrame::destroy(JSC::JSCell* cell)
61 {
62 JSJavaScriptCallFrame* thisObject = static_cast<JSJavaScriptCallFrame*>(cell);
63 thisObject->JSJavaScriptCallFrame::~JSJavaScriptCallFrame();
64 }
65
66 void JSJavaScriptCallFrame::releaseImpl()
67 {
68 if (m_impl) {
69 m_impl->deref();
70 m_impl = nullptr;
71 }
72 }
73
74 JSJavaScriptCallFrame::~JSJavaScriptCallFrame()
75 {
76 releaseImpl();
77 }
78
79 JSValue JSJavaScriptCallFrame::evaluate(ExecState* exec)
80 {
81 JSValue exception;
82 JSValue result = impl().evaluate(exec->argument(0).toString(exec)->value(exec), exception);
83 if (exception)
84 exec->vm().throwException(exec, exception);
85
86 return result;
87 }
88
89 JSValue JSJavaScriptCallFrame::scopeType(ExecState* exec)
90 {
91 if (!impl().scopeChain())
92 return jsUndefined();
93
94 if (!exec->argument(0).isInt32())
95 return jsUndefined();
96 int index = exec->argument(0).asInt32();
97
98 JSScope* scopeChain = impl().scopeChain();
99 ScopeChainIterator end = scopeChain->end();
100
101 // FIXME: We should be identifying and returning CATCH_SCOPE appropriately.
102
103 bool foundLocalScope = false;
104 for (ScopeChainIterator iter = scopeChain->begin(); iter != end; ++iter) {
105 JSObject* scope = iter.get();
106 if (scope->isActivationObject()) {
107 if (!foundLocalScope) {
108 // First activation object is local scope, each successive activation object is closure.
109 if (!index)
110 return jsNumber(JSJavaScriptCallFrame::LOCAL_SCOPE);
111 foundLocalScope = true;
112 } else if (!index)
113 return jsNumber(JSJavaScriptCallFrame::CLOSURE_SCOPE);
114 }
115
116 if (!index) {
117 // Last in the chain is global scope.
118 if (++iter == end)
119 return jsNumber(JSJavaScriptCallFrame::GLOBAL_SCOPE);
120 return jsNumber(JSJavaScriptCallFrame::WITH_SCOPE);
121 }
122
123 --index;
124 }
125
126 ASSERT_NOT_REACHED();
127 return jsUndefined();
128 }
129
130 JSValue JSJavaScriptCallFrame::caller(ExecState* exec) const
131 {
132 return toJS(exec, globalObject(), impl().caller());
133 }
134
135 JSValue JSJavaScriptCallFrame::sourceID(ExecState*) const
136 {
137 return jsNumber(impl().sourceID());
138 }
139
140 JSValue JSJavaScriptCallFrame::line(ExecState*) const
141 {
142 return jsNumber(impl().line());
143 }
144
145 JSValue JSJavaScriptCallFrame::column(ExecState*) const
146 {
147 return jsNumber(impl().column());
148 }
149
150 JSValue JSJavaScriptCallFrame::functionName(ExecState* exec) const
151 {
152 return jsString(exec, impl().functionName());
153 }
154
155 JSValue JSJavaScriptCallFrame::scopeChain(ExecState* exec) const
156 {
157 if (!impl().scopeChain())
158 return jsNull();
159
160 JSScope* scopeChain = impl().scopeChain();
161 ScopeChainIterator iter = scopeChain->begin();
162 ScopeChainIterator end = scopeChain->end();
163
164 // We must always have something in the scope chain.
165 ASSERT(iter != end);
166
167 MarkedArgumentBuffer list;
168 do {
169 list.append(iter.get());
170 ++iter;
171 } while (iter != end);
172
173 return constructArray(exec, nullptr, globalObject(), list);
174 }
175
176 JSValue JSJavaScriptCallFrame::thisObject(ExecState*) const
177 {
178 return impl().thisValue();
179 }
180
181 JSValue JSJavaScriptCallFrame::type(ExecState* exec) const
182 {
183 switch (impl().type()) {
184 case DebuggerCallFrame::FunctionType:
185 return jsNontrivialString(exec, ASCIILiteral("function"));
186 case DebuggerCallFrame::ProgramType:
187 return jsNontrivialString(exec, ASCIILiteral("program"));
188 }
189
190 ASSERT_NOT_REACHED();
191 return jsNull();
192 }
193
194 JSValue toJS(ExecState* exec, JSGlobalObject* globalObject, JavaScriptCallFrame* impl)
195 {
196 if (!impl)
197 return jsNull();
198
199 JSObject* prototype = JSJavaScriptCallFrame::createPrototype(exec->vm(), globalObject);
200 Structure* structure = JSJavaScriptCallFrame::createStructure(exec->vm(), globalObject, prototype);
201 JSJavaScriptCallFrame* javaScriptCallFrame = JSJavaScriptCallFrame::create(exec->vm(), structure, impl);
202
203 return javaScriptCallFrame;
204 }
205
206 JSJavaScriptCallFrame* toJSJavaScriptCallFrame(JSValue value)
207 {
208 return value.inherits(JSJavaScriptCallFrame::info()) ? jsCast<JSJavaScriptCallFrame*>(value) : nullptr;
209 }
210
211 } // namespace Inspector
212
213 #endif // ENABLE(INSPECTOR)