Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (c) 2010, Google 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 are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "JavaScriptCallFrame.h"
     33 
     34 #if ENABLE(JAVASCRIPT_DEBUGGER)
     35 
     36 #include "V8Binding.h"
     37 
     38 namespace WebCore {
     39 
     40 JavaScriptCallFrame::JavaScriptCallFrame(v8::Handle<v8::Context> debuggerContext, v8::Handle<v8::Object> callFrame)
     41     : m_debuggerContext(debuggerContext)
     42     , m_callFrame(callFrame)
     43 {
     44 }
     45 
     46 JavaScriptCallFrame::~JavaScriptCallFrame()
     47 {
     48 }
     49 
     50 JavaScriptCallFrame* JavaScriptCallFrame::caller()
     51 {
     52     if (!m_caller) {
     53         v8::HandleScope handleScope;
     54         v8::Context::Scope contextScope(m_debuggerContext.get());
     55         v8::Handle<v8::Value> callerFrame = m_callFrame.get()->Get(v8String("caller"));
     56         if (!callerFrame->IsObject())
     57             return 0;
     58         m_caller = JavaScriptCallFrame::create(m_debuggerContext.get(), v8::Handle<v8::Object>::Cast(callerFrame));
     59     }
     60     return m_caller.get();
     61 }
     62 
     63 int JavaScriptCallFrame::sourceID() const
     64 {
     65     v8::HandleScope handleScope;
     66     v8::Context::Scope contextScope(m_debuggerContext.get());
     67     v8::Handle<v8::Value> result = m_callFrame.get()->Get(v8String("sourceID"));
     68     if (result->IsInt32())
     69         return result->Int32Value();
     70     return 0;
     71 }
     72 
     73 int JavaScriptCallFrame::line() const
     74 {
     75     v8::HandleScope handleScope;
     76     v8::Context::Scope contextScope(m_debuggerContext.get());
     77     v8::Handle<v8::Value> result = m_callFrame.get()->Get(v8String("line"));
     78     if (result->IsInt32())
     79         return result->Int32Value();
     80     return 0;
     81 }
     82 
     83 int JavaScriptCallFrame::column() const
     84 {
     85     v8::HandleScope handleScope;
     86     v8::Context::Scope contextScope(m_debuggerContext.get());
     87     v8::Handle<v8::Value> result = m_callFrame.get()->Get(v8String("column"));
     88     if (result->IsInt32())
     89         return result->Int32Value();
     90     return 0;
     91 }
     92 
     93 String JavaScriptCallFrame::functionName() const
     94 {
     95     v8::HandleScope handleScope;
     96     v8::Context::Scope contextScope(m_debuggerContext.get());
     97     v8::Handle<v8::Value> result = m_callFrame.get()->Get(v8String("functionName"));
     98     return toWebCoreStringWithNullOrUndefinedCheck(result);
     99 }
    100 
    101 v8::Handle<v8::Value> JavaScriptCallFrame::scopeChain() const
    102 {
    103     v8::Handle<v8::Array> scopeChain = v8::Handle<v8::Array>::Cast(m_callFrame.get()->Get(v8String("scopeChain")));
    104     v8::Handle<v8::Array> result = v8::Array::New(scopeChain->Length());
    105     for (uint32_t i = 0; i < scopeChain->Length(); i++)
    106         result->Set(i, scopeChain->Get(i));
    107     return result;
    108 }
    109 
    110 int JavaScriptCallFrame::scopeType(int scopeIndex) const
    111 {
    112     v8::Handle<v8::Array> scopeType = v8::Handle<v8::Array>::Cast(m_callFrame.get()->Get(v8String("scopeType")));
    113     return scopeType->Get(scopeIndex)->Int32Value();
    114 }
    115 
    116 v8::Handle<v8::Value> JavaScriptCallFrame::thisObject() const
    117 {
    118     return m_callFrame.get()->Get(v8String("thisObject"));
    119 }
    120 
    121 v8::Handle<v8::Value> JavaScriptCallFrame::evaluate(const String& expression)
    122 {
    123     v8::Handle<v8::Function> evalFunction = v8::Handle<v8::Function>::Cast(m_callFrame.get()->Get(v8String("evaluate")));
    124     v8::Handle<v8::Value> argv[] = { v8String(expression) };
    125     return evalFunction->Call(m_callFrame.get(), 1, argv);
    126 }
    127 
    128 } // namespace WebCore
    129 
    130 #endif // ENABLE(JAVASCRIPT_DEBUGGER)
    131