X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..refs/heads/master:/debugger/DebuggerCallFrame.cpp diff --git a/debugger/DebuggerCallFrame.cpp b/debugger/DebuggerCallFrame.cpp index 27b824c..663fc03 100644 --- a/debugger/DebuggerCallFrame.cpp +++ b/debugger/DebuggerCallFrame.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008, 2013, 2014 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,7 +10,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * @@ -29,53 +29,229 @@ #include "config.h" #include "DebuggerCallFrame.h" -#include "JSFunction.h" #include "CodeBlock.h" +#include "DebuggerEvalEnabler.h" +#include "DebuggerScope.h" #include "Interpreter.h" +#include "JSFunction.h" +#include "JSLexicalEnvironment.h" +#include "JSCInlines.h" #include "Parser.h" +#include "StackVisitor.h" +#include "StrongInlines.h" namespace JSC { -const UString* DebuggerCallFrame::functionName() const +class LineAndColumnFunctor { +public: + StackVisitor::Status operator()(StackVisitor& visitor) + { + visitor->computeLineAndColumn(m_line, m_column); + return StackVisitor::Done; + } + + unsigned line() const { return m_line; } + unsigned column() const { return m_column; } + +private: + unsigned m_line; + unsigned m_column; +}; + +class FindCallerMidStackFunctor { +public: + FindCallerMidStackFunctor(CallFrame* callFrame) + : m_callFrame(callFrame) + , m_callerFrame(nullptr) + { } + + StackVisitor::Status operator()(StackVisitor& visitor) + { + if (visitor->callFrame() == m_callFrame) { + m_callerFrame = visitor->callerFrame(); + return StackVisitor::Done; + } + return StackVisitor::Continue; + } + + CallFrame* getCallerFrame() const { return m_callerFrame; } + +private: + CallFrame* m_callFrame; + CallFrame* m_callerFrame; +}; + +DebuggerCallFrame::DebuggerCallFrame(CallFrame* callFrame) + : m_callFrame(callFrame) +{ + m_position = positionForCallFrame(m_callFrame); +} + +RefPtr DebuggerCallFrame::callerFrame() { - if (!m_callFrame->codeBlock()) + ASSERT(isValid()); + if (!isValid()) return 0; - JSFunction* function = static_cast(m_callFrame->callee()); + if (m_caller) + return m_caller; + + FindCallerMidStackFunctor functor(m_callFrame); + m_callFrame->vm().topCallFrame->iterate(functor); + + CallFrame* callerFrame = functor.getCallerFrame(); + if (!callerFrame) + return nullptr; + + m_caller = DebuggerCallFrame::create(callerFrame); + return m_caller; +} + +JSC::JSGlobalObject* DebuggerCallFrame::vmEntryGlobalObject() const +{ + ASSERT(isValid()); + if (!isValid()) + return 0; + return m_callFrame->vmEntryGlobalObject(); +} + +SourceID DebuggerCallFrame::sourceID() const +{ + ASSERT(isValid()); + if (!isValid()) + return noSourceID; + return sourceIDForCallFrame(m_callFrame); +} + +String DebuggerCallFrame::functionName() const +{ + ASSERT(isValid()); + if (!isValid()) + return String(); + JSFunction* function = jsDynamicCast(m_callFrame->callee()); if (!function) + return String(); + + return getCalculatedDisplayName(m_callFrame, function); +} + +DebuggerScope* DebuggerCallFrame::scope() +{ + ASSERT(isValid()); + if (!isValid()) return 0; - return &function->name(&m_callFrame->globalData()); + + if (!m_scope) { + VM& vm = m_callFrame->vm(); + JSScope* scope; + CodeBlock* codeBlock = m_callFrame->codeBlock(); + if (codeBlock && codeBlock->scopeRegister().isValid()) + scope = m_callFrame->scope(codeBlock->scopeRegister().offset()); + else if (JSCallee* callee = jsDynamicCast(m_callFrame->callee())) + scope = callee->scope(); + else + scope = m_callFrame->lexicalGlobalObject(); + + m_scope.set(vm, DebuggerScope::create(vm, scope)); + } + return m_scope.get(); } DebuggerCallFrame::Type DebuggerCallFrame::type() const { - if (m_callFrame->callee()) + ASSERT(isValid()); + if (!isValid()) + return ProgramType; + + if (jsDynamicCast(m_callFrame->callee())) return FunctionType; return ProgramType; } -JSObject* DebuggerCallFrame::thisObject() const +JSValue DebuggerCallFrame::thisValue() const { - if (!m_callFrame->codeBlock()) - return 0; + ASSERT(isValid()); + return thisValueForCallFrame(m_callFrame); +} + +// Evaluate some JavaScript code in the scope of this frame. +JSValue DebuggerCallFrame::evaluate(const String& script, NakedPtr& exception) +{ + ASSERT(isValid()); + CallFrame* callFrame = m_callFrame; + if (!callFrame) + return jsNull(); - return asObject(m_callFrame->thisValue()); + JSLockHolder lock(callFrame); + + if (!callFrame->codeBlock()) + return JSValue(); + + DebuggerEvalEnabler evalEnabler(callFrame); + VM& vm = callFrame->vm(); + auto& codeBlock = *callFrame->codeBlock(); + ThisTDZMode thisTDZMode = codeBlock.unlinkedCodeBlock()->constructorKind() == ConstructorKind::Derived ? ThisTDZMode::AlwaysCheck : ThisTDZMode::CheckIfNeeded; + EvalExecutable* eval = EvalExecutable::create(callFrame, makeSource(script), codeBlock.isStrictMode(), thisTDZMode); + if (vm.exception()) { + exception = vm.exception(); + vm.clearException(); + return jsUndefined(); + } + + JSValue thisValue = thisValueForCallFrame(callFrame); + JSValue result = vm.interpreter->execute(eval, callFrame, thisValue, scope()->jsScope()); + if (vm.exception()) { + exception = vm.exception(); + vm.clearException(); + } + ASSERT(result); + return result; +} + +void DebuggerCallFrame::invalidate() +{ + RefPtr frame = this; + while (frame) { + frame->m_callFrame = nullptr; + if (frame->m_scope) { + frame->m_scope->invalidateChain(); + frame->m_scope.clear(); + } + frame = frame->m_caller.release(); + } } -JSValuePtr DebuggerCallFrame::evaluate(const UString& script, JSValuePtr& exception) const +TextPosition DebuggerCallFrame::positionForCallFrame(CallFrame* callFrame) { - if (!m_callFrame->codeBlock()) - return noValue(); + if (!callFrame) + return TextPosition(); - int errLine; - UString errMsg; - SourceCode source = makeSource(script); - RefPtr evalNode = m_callFrame->scopeChain()->globalData->parser->parse(m_callFrame, m_callFrame->dynamicGlobalObject()->debugger(), source, &errLine, &errMsg); - if (!evalNode) - return Error::create(m_callFrame, SyntaxError, errMsg, errLine, source.provider()->asID(), source.provider()->url()); + LineAndColumnFunctor functor; + callFrame->iterate(functor); + return TextPosition(OrdinalNumber::fromOneBasedInt(functor.line()), OrdinalNumber::fromOneBasedInt(functor.column())); +} + +SourceID DebuggerCallFrame::sourceIDForCallFrame(CallFrame* callFrame) +{ + ASSERT(callFrame); + CodeBlock* codeBlock = callFrame->codeBlock(); + if (!codeBlock) + return noSourceID; + return codeBlock->ownerExecutable()->sourceID(); +} + +JSValue DebuggerCallFrame::thisValueForCallFrame(CallFrame* callFrame) +{ + if (!callFrame) + return jsNull(); - return m_callFrame->scopeChain()->globalData->interpreter->execute(evalNode.get(), m_callFrame, thisObject(), m_callFrame->scopeChain(), &exception); + ECMAMode ecmaMode = NotStrictMode; + CodeBlock* codeBlock = callFrame->codeBlock(); + if (codeBlock && codeBlock->isStrictMode()) + ecmaMode = StrictMode; + JSValue thisValue = callFrame->thisValue().toThis(callFrame, ecmaMode); + return thisValue; } } // namespace JSC