1/*
2 * Copyright (C) 2014-2019 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#include "DebuggerScope.h"
30#include "Error.h"
31#include "IdentifierInlines.h"
32#include "JSCInlines.h"
33#include "JSJavaScriptCallFramePrototype.h"
34#include "ObjectConstructor.h"
35
36using namespace JSC;
37
38namespace Inspector {
39
40const ClassInfo JSJavaScriptCallFrame::s_info = { "JavaScriptCallFrame", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSJavaScriptCallFrame) };
41
42JSJavaScriptCallFrame::JSJavaScriptCallFrame(VM& vm, Structure* structure, Ref<JavaScriptCallFrame>&& impl)
43 : JSDestructibleObject(vm, structure)
44 , m_impl(&impl.leakRef())
45{
46}
47
48void JSJavaScriptCallFrame::finishCreation(VM& vm)
49{
50 Base::finishCreation(vm);
51 ASSERT(inherits(vm, info()));
52}
53
54JSObject* JSJavaScriptCallFrame::createPrototype(VM& vm, JSGlobalObject* globalObject)
55{
56 return JSJavaScriptCallFramePrototype::create(vm, globalObject, JSJavaScriptCallFramePrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
57}
58
59void JSJavaScriptCallFrame::destroy(JSC::JSCell* cell)
60{
61 JSJavaScriptCallFrame* thisObject = static_cast<JSJavaScriptCallFrame*>(cell);
62 thisObject->JSJavaScriptCallFrame::~JSJavaScriptCallFrame();
63}
64
65void JSJavaScriptCallFrame::releaseImpl()
66{
67 if (auto impl = std::exchange(m_impl, nullptr))
68 impl->deref();
69}
70
71JSJavaScriptCallFrame::~JSJavaScriptCallFrame()
72{
73 releaseImpl();
74}
75
76JSValue JSJavaScriptCallFrame::evaluateWithScopeExtension(JSGlobalObject* globalObject, CallFrame* callFrame)
77{
78 VM& vm = globalObject->vm();
79 auto scope = DECLARE_THROW_SCOPE(vm);
80
81 JSValue scriptValue = callFrame->argument(0);
82 if (!scriptValue.isString())
83 return throwTypeError(globalObject, scope, "JSJavaScriptCallFrame.evaluateWithScopeExtension first argument must be a string."_s);
84
85 String script = asString(scriptValue)->value(globalObject);
86 RETURN_IF_EXCEPTION(scope, JSValue());
87
88 NakedPtr<Exception> exception;
89 JSObject* scopeExtension = callFrame->argument(1).getObject();
90 JSValue result = impl().evaluateWithScopeExtension(script, scopeExtension, exception);
91 if (exception)
92 throwException(globalObject, scope, exception);
93
94 return result;
95}
96
97static JSValue valueForScopeType(DebuggerScope* scope)
98{
99 if (scope->isCatchScope())
100 return jsNumber(JSJavaScriptCallFrame::CATCH_SCOPE);
101 if (scope->isFunctionNameScope())
102 return jsNumber(JSJavaScriptCallFrame::FUNCTION_NAME_SCOPE);
103 if (scope->isWithScope())
104 return jsNumber(JSJavaScriptCallFrame::WITH_SCOPE);
105 if (scope->isNestedLexicalScope())
106 return jsNumber(JSJavaScriptCallFrame::NESTED_LEXICAL_SCOPE);
107 if (scope->isGlobalLexicalEnvironment())
108 return jsNumber(JSJavaScriptCallFrame::GLOBAL_LEXICAL_ENVIRONMENT_SCOPE);
109 if (scope->isGlobalScope())
110 return jsNumber(JSJavaScriptCallFrame::GLOBAL_SCOPE);
111
112 ASSERT(scope->isClosureScope());
113 return jsNumber(JSJavaScriptCallFrame::CLOSURE_SCOPE);
114}
115
116static JSValue valueForScopeLocation(JSGlobalObject* globalObject, const DebuggerLocation& location)
117{
118 if (location.sourceID == noSourceID)
119 return jsNull();
120
121 // Debugger.Location protocol object.
122 VM& vm = globalObject->vm();
123 JSObject* result = constructEmptyObject(globalObject);
124 result->putDirect(vm, Identifier::fromString(vm, "scriptId"), jsString(vm, String::number(location.sourceID)));
125 result->putDirect(vm, Identifier::fromString(vm, "lineNumber"), jsNumber(location.line));
126 result->putDirect(vm, Identifier::fromString(vm, "columnNumber"), jsNumber(location.column));
127 return result;
128}
129
130JSValue JSJavaScriptCallFrame::scopeDescriptions(JSGlobalObject* globalObject)
131{
132 VM& vm = globalObject->vm();
133 auto throwScope = DECLARE_THROW_SCOPE(vm);
134
135 DebuggerScope* scopeChain = impl().scopeChain();
136 if (!scopeChain)
137 return jsUndefined();
138
139 int index = 0;
140 JSArray* array = constructEmptyArray(globalObject, nullptr);
141
142 DebuggerScope::iterator end = scopeChain->end();
143 for (DebuggerScope::iterator iter = scopeChain->begin(); iter != end; ++iter) {
144 DebuggerScope* scope = iter.get();
145 JSObject* description = constructEmptyObject(globalObject);
146 description->putDirect(vm, Identifier::fromString(vm, "type"), valueForScopeType(scope));
147 description->putDirect(vm, Identifier::fromString(vm, "name"), jsString(vm, scope->name()));
148 description->putDirect(vm, Identifier::fromString(vm, "location"), valueForScopeLocation(globalObject, scope->location()));
149 array->putDirectIndex(globalObject, index++, description);
150 RETURN_IF_EXCEPTION(throwScope, JSValue());
151 }
152
153 return array;
154}
155
156JSValue JSJavaScriptCallFrame::caller(JSGlobalObject* lexicalGlobalObject) const
157{
158 return toJS(lexicalGlobalObject, this->globalObject(lexicalGlobalObject->vm()), impl().caller());
159}
160
161JSValue JSJavaScriptCallFrame::sourceID(JSGlobalObject*) const
162{
163 return jsNumber(impl().sourceID());
164}
165
166JSValue JSJavaScriptCallFrame::line(JSGlobalObject*) const
167{
168 return jsNumber(impl().line());
169}
170
171JSValue JSJavaScriptCallFrame::column(JSGlobalObject*) const
172{
173 return jsNumber(impl().column());
174}
175
176JSValue JSJavaScriptCallFrame::functionName(JSGlobalObject* globalObject) const
177{
178 return jsString(globalObject->vm(), impl().functionName());
179}
180
181JSValue JSJavaScriptCallFrame::scopeChain(JSGlobalObject* globalObject) const
182{
183 VM& vm = globalObject->vm();
184 auto scope = DECLARE_THROW_SCOPE(vm);
185
186 if (!impl().scopeChain())
187 return jsNull();
188
189 DebuggerScope* scopeChain = impl().scopeChain();
190 DebuggerScope::iterator iter = scopeChain->begin();
191 DebuggerScope::iterator end = scopeChain->end();
192
193 // We must always have something in the scope chain.
194 ASSERT(iter != end);
195
196 MarkedArgumentBuffer list;
197 do {
198 list.append(iter.get());
199 ++iter;
200 } while (iter != end);
201 if (UNLIKELY(list.hasOverflowed())) {
202 throwOutOfMemoryError(globalObject, scope);
203 return { };
204 }
205
206 return constructArray(this->globalObject(vm), static_cast<ArrayAllocationProfile*>(nullptr), list);
207}
208
209JSValue JSJavaScriptCallFrame::thisObject(JSGlobalObject* globalObject) const
210{
211 return impl().thisValue(globalObject->vm());
212}
213
214JSValue JSJavaScriptCallFrame::isTailDeleted(JSC::JSGlobalObject*) const
215{
216 return jsBoolean(impl().isTailDeleted());
217}
218
219JSValue JSJavaScriptCallFrame::type(JSGlobalObject* globalObject) const
220{
221 VM& vm = globalObject->vm();
222 switch (impl().type()) {
223 case DebuggerCallFrame::FunctionType:
224 return jsNontrivialString(vm, "function"_s);
225 case DebuggerCallFrame::ProgramType:
226 return jsNontrivialString(vm, "program"_s);
227 }
228
229 ASSERT_NOT_REACHED();
230 return jsNull();
231}
232
233JSValue toJS(JSGlobalObject* lexicalGlobalObject, JSGlobalObject* globalObject, JavaScriptCallFrame* impl)
234{
235 if (!impl)
236 return jsNull();
237
238 VM& vm = lexicalGlobalObject->vm();
239 JSObject* prototype = JSJavaScriptCallFrame::createPrototype(vm, globalObject);
240 Structure* structure = JSJavaScriptCallFrame::createStructure(vm, globalObject, prototype);
241 JSJavaScriptCallFrame* javaScriptCallFrame = JSJavaScriptCallFrame::create(vm, structure, *impl);
242
243 return javaScriptCallFrame;
244}
245
246} // namespace Inspector
247
248