2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "JSJavaScriptCallFrame.h"
32 #include "JSCJSValue.h"
33 #include "JSCellInlines.h"
34 #include "JSJavaScriptCallFramePrototype.h"
35 #include "StructureInlines.h"
41 const ClassInfo JSJavaScriptCallFrame::s_info = { "JavaScriptCallFrame", &Base::s_info, 0, CREATE_METHOD_TABLE(JSJavaScriptCallFrame) };
43 JSJavaScriptCallFrame::JSJavaScriptCallFrame(VM& vm, Structure* structure, PassRefPtr<JavaScriptCallFrame> impl)
44 : JSDestructibleObject(vm, structure)
45 , m_impl(impl.leakRef())
49 void JSJavaScriptCallFrame::finishCreation(VM& vm)
51 Base::finishCreation(vm);
52 ASSERT(inherits(info()));
55 JSObject* JSJavaScriptCallFrame::createPrototype(VM& vm, JSGlobalObject* globalObject)
57 return JSJavaScriptCallFramePrototype::create(vm, globalObject, JSJavaScriptCallFramePrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
60 void JSJavaScriptCallFrame::destroy(JSC::JSCell* cell)
62 JSJavaScriptCallFrame* thisObject = static_cast<JSJavaScriptCallFrame*>(cell);
63 thisObject->JSJavaScriptCallFrame::~JSJavaScriptCallFrame();
66 void JSJavaScriptCallFrame::releaseImpl()
74 JSJavaScriptCallFrame::~JSJavaScriptCallFrame()
79 JSValue JSJavaScriptCallFrame::evaluate(ExecState* exec)
82 JSValue result = impl().evaluate(exec->argument(0).toString(exec)->value(exec), exception);
84 exec->vm().throwException(exec, exception);
89 JSValue JSJavaScriptCallFrame::scopeType(ExecState* exec)
91 if (!impl().scopeChain())
94 if (!exec->argument(0).isInt32())
96 int index = exec->argument(0).asInt32();
98 JSScope* scopeChain = impl().scopeChain();
99 ScopeChainIterator end = scopeChain->end();
101 // FIXME: We should be identifying and returning CATCH_SCOPE appropriately.
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.
110 return jsNumber(JSJavaScriptCallFrame::LOCAL_SCOPE);
111 foundLocalScope = true;
113 return jsNumber(JSJavaScriptCallFrame::CLOSURE_SCOPE);
117 // Last in the chain is global scope.
119 return jsNumber(JSJavaScriptCallFrame::GLOBAL_SCOPE);
120 return jsNumber(JSJavaScriptCallFrame::WITH_SCOPE);
126 ASSERT_NOT_REACHED();
127 return jsUndefined();
130 JSValue JSJavaScriptCallFrame::caller(ExecState* exec) const
132 return toJS(exec, globalObject(), impl().caller());
135 JSValue JSJavaScriptCallFrame::sourceID(ExecState*) const
137 return jsNumber(impl().sourceID());
140 JSValue JSJavaScriptCallFrame::line(ExecState*) const
142 return jsNumber(impl().line());
145 JSValue JSJavaScriptCallFrame::column(ExecState*) const
147 return jsNumber(impl().column());
150 JSValue JSJavaScriptCallFrame::functionName(ExecState* exec) const
152 return jsString(exec, impl().functionName());
155 JSValue JSJavaScriptCallFrame::scopeChain(ExecState* exec) const
157 if (!impl().scopeChain())
160 JSScope* scopeChain = impl().scopeChain();
161 ScopeChainIterator iter = scopeChain->begin();
162 ScopeChainIterator end = scopeChain->end();
164 // We must always have something in the scope chain.
167 MarkedArgumentBuffer list;
169 list.append(iter.get());
171 } while (iter != end);
173 return constructArray(exec, nullptr, globalObject(), list);
176 JSValue JSJavaScriptCallFrame::thisObject(ExecState*) const
178 return impl().thisValue();
181 JSValue JSJavaScriptCallFrame::type(ExecState* exec) const
183 switch (impl().type()) {
184 case DebuggerCallFrame::FunctionType:
185 return jsNontrivialString(exec, ASCIILiteral("function"));
186 case DebuggerCallFrame::ProgramType:
187 return jsNontrivialString(exec, ASCIILiteral("program"));
190 ASSERT_NOT_REACHED();
194 JSValue toJS(ExecState* exec, JSGlobalObject* globalObject, JavaScriptCallFrame* impl)
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);
203 return javaScriptCallFrame;
206 JSJavaScriptCallFrame* toJSJavaScriptCallFrame(JSValue value)
208 return value.inherits(JSJavaScriptCallFrame::info()) ? jsCast<JSJavaScriptCallFrame*>(value) : nullptr;
211 } // namespace Inspector
213 #endif // ENABLE(INSPECTOR)