| 1 | /* |
| 2 | * Copyright (C) 2014-2017 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 | |
| 36 | using namespace JSC; |
| 37 | |
| 38 | namespace Inspector { |
| 39 | |
| 40 | const ClassInfo JSJavaScriptCallFrame::s_info = { "JavaScriptCallFrame" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSJavaScriptCallFrame) }; |
| 41 | |
| 42 | JSJavaScriptCallFrame::JSJavaScriptCallFrame(VM& vm, Structure* structure, Ref<JavaScriptCallFrame>&& impl) |
| 43 | : JSDestructibleObject(vm, structure) |
| 44 | , m_impl(&impl.leakRef()) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void JSJavaScriptCallFrame::finishCreation(VM& vm) |
| 49 | { |
| 50 | Base::finishCreation(vm); |
| 51 | ASSERT(inherits(vm, info())); |
| 52 | } |
| 53 | |
| 54 | JSObject* JSJavaScriptCallFrame::createPrototype(VM& vm, JSGlobalObject* globalObject) |
| 55 | { |
| 56 | return JSJavaScriptCallFramePrototype::create(vm, globalObject, JSJavaScriptCallFramePrototype::createStructure(vm, globalObject, globalObject->objectPrototype())); |
| 57 | } |
| 58 | |
| 59 | void JSJavaScriptCallFrame::destroy(JSC::JSCell* cell) |
| 60 | { |
| 61 | JSJavaScriptCallFrame* thisObject = static_cast<JSJavaScriptCallFrame*>(cell); |
| 62 | thisObject->JSJavaScriptCallFrame::~JSJavaScriptCallFrame(); |
| 63 | } |
| 64 | |
| 65 | void JSJavaScriptCallFrame::releaseImpl() |
| 66 | { |
| 67 | if (auto impl = std::exchange(m_impl, nullptr)) |
| 68 | impl->deref(); |
| 69 | } |
| 70 | |
| 71 | JSJavaScriptCallFrame::~JSJavaScriptCallFrame() |
| 72 | { |
| 73 | releaseImpl(); |
| 74 | } |
| 75 | |
| 76 | JSValue JSJavaScriptCallFrame::evaluateWithScopeExtension(ExecState* exec) |
| 77 | { |
| 78 | VM& vm = exec->vm(); |
| 79 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 80 | |
| 81 | JSValue scriptValue = exec->argument(0); |
| 82 | if (!scriptValue.isString()) |
| 83 | return throwTypeError(exec, scope, "JSJavaScriptCallFrame.evaluateWithScopeExtension first argument must be a string."_s ); |
| 84 | |
| 85 | String script = asString(scriptValue)->value(exec); |
| 86 | RETURN_IF_EXCEPTION(scope, JSValue()); |
| 87 | |
| 88 | NakedPtr<Exception> exception; |
| 89 | JSObject* scopeExtension = exec->argument(1).getObject(); |
| 90 | JSValue result = impl().evaluateWithScopeExtension(script, scopeExtension, exception); |
| 91 | if (exception) |
| 92 | throwException(exec, scope, exception); |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | static 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 | |
| 116 | static JSValue valueForScopeLocation(ExecState* exec, const DebuggerLocation& location) |
| 117 | { |
| 118 | if (location.sourceID == noSourceID) |
| 119 | return jsNull(); |
| 120 | |
| 121 | // Debugger.Location protocol object. |
| 122 | VM& vm = exec->vm(); |
| 123 | JSObject* result = constructEmptyObject(exec); |
| 124 | result->putDirect(vm, Identifier::fromString(exec, "scriptId" ), jsString(exec, String::number(location.sourceID))); |
| 125 | result->putDirect(vm, Identifier::fromString(exec, "lineNumber" ), jsNumber(location.line)); |
| 126 | result->putDirect(vm, Identifier::fromString(exec, "columnNumber" ), jsNumber(location.column)); |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | JSValue JSJavaScriptCallFrame::scopeDescriptions(ExecState* exec) |
| 131 | { |
| 132 | VM& vm = exec->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(exec, 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(exec); |
| 146 | description->putDirect(vm, Identifier::fromString(exec, "type" ), valueForScopeType(scope)); |
| 147 | description->putDirect(vm, Identifier::fromString(exec, "name" ), jsString(exec, scope->name())); |
| 148 | description->putDirect(vm, Identifier::fromString(exec, "location" ), valueForScopeLocation(exec, scope->location())); |
| 149 | array->putDirectIndex(exec, index++, description); |
| 150 | RETURN_IF_EXCEPTION(throwScope, JSValue()); |
| 151 | } |
| 152 | |
| 153 | return array; |
| 154 | } |
| 155 | |
| 156 | JSValue JSJavaScriptCallFrame::caller(ExecState* exec) const |
| 157 | { |
| 158 | return toJS(exec, globalObject(exec->vm()), impl().caller()); |
| 159 | } |
| 160 | |
| 161 | JSValue JSJavaScriptCallFrame::sourceID(ExecState*) const |
| 162 | { |
| 163 | return jsNumber(impl().sourceID()); |
| 164 | } |
| 165 | |
| 166 | JSValue JSJavaScriptCallFrame::line(ExecState*) const |
| 167 | { |
| 168 | return jsNumber(impl().line()); |
| 169 | } |
| 170 | |
| 171 | JSValue JSJavaScriptCallFrame::column(ExecState*) const |
| 172 | { |
| 173 | return jsNumber(impl().column()); |
| 174 | } |
| 175 | |
| 176 | JSValue JSJavaScriptCallFrame::functionName(ExecState* exec) const |
| 177 | { |
| 178 | return jsString(exec, impl().functionName()); |
| 179 | } |
| 180 | |
| 181 | JSValue JSJavaScriptCallFrame::scopeChain(ExecState* exec) const |
| 182 | { |
| 183 | VM& vm = exec->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(exec, scope); |
| 203 | return { }; |
| 204 | } |
| 205 | |
| 206 | return constructArray(exec, nullptr, globalObject(vm), list); |
| 207 | } |
| 208 | |
| 209 | JSValue JSJavaScriptCallFrame::thisObject(ExecState*) const |
| 210 | { |
| 211 | return impl().thisValue(); |
| 212 | } |
| 213 | |
| 214 | JSValue JSJavaScriptCallFrame::isTailDeleted(JSC::ExecState*) const |
| 215 | { |
| 216 | return jsBoolean(impl().isTailDeleted()); |
| 217 | } |
| 218 | |
| 219 | JSValue JSJavaScriptCallFrame::type(ExecState* exec) const |
| 220 | { |
| 221 | switch (impl().type()) { |
| 222 | case DebuggerCallFrame::FunctionType: |
| 223 | return jsNontrivialString(exec, "function"_s ); |
| 224 | case DebuggerCallFrame::ProgramType: |
| 225 | return jsNontrivialString(exec, "program"_s ); |
| 226 | } |
| 227 | |
| 228 | ASSERT_NOT_REACHED(); |
| 229 | return jsNull(); |
| 230 | } |
| 231 | |
| 232 | JSValue toJS(ExecState* exec, JSGlobalObject* globalObject, JavaScriptCallFrame* impl) |
| 233 | { |
| 234 | if (!impl) |
| 235 | return jsNull(); |
| 236 | |
| 237 | VM& vm = exec->vm(); |
| 238 | JSObject* prototype = JSJavaScriptCallFrame::createPrototype(vm, globalObject); |
| 239 | Structure* structure = JSJavaScriptCallFrame::createStructure(vm, globalObject, prototype); |
| 240 | JSJavaScriptCallFrame* javaScriptCallFrame = JSJavaScriptCallFrame::create(vm, structure, *impl); |
| 241 | |
| 242 | return javaScriptCallFrame; |
| 243 | } |
| 244 | |
| 245 | } // namespace Inspector |
| 246 | |
| 247 | |