WebCore/bindings/js/ScriptCallStack.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (c) 2008, 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 "ScriptCallStack.h"
       
    33 
       
    34 #include <interpreter/CallFrame.h>
       
    35 #include <interpreter/Interpreter.h>
       
    36 #include <runtime/JSFunction.h>
       
    37 #include <runtime/JSValue.h>
       
    38 #include <runtime/UString.h>
       
    39 #include <runtime/JSGlobalData.h>
       
    40 
       
    41 using namespace JSC;
       
    42 
       
    43 namespace WebCore {
       
    44 
       
    45 ScriptCallStack::ScriptCallStack(ExecState* exec, unsigned skipArgumentCount)
       
    46     : m_initialized(false)
       
    47     , m_exec(exec)
       
    48     , m_caller(0)
       
    49 {
       
    50     int signedLineNumber;
       
    51     intptr_t sourceID;
       
    52     UString urlString;
       
    53     JSValue function;
       
    54 
       
    55     exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, urlString, function);
       
    56 
       
    57     unsigned lineNumber = signedLineNumber >= 0 ? signedLineNumber : 0;
       
    58 
       
    59     if (function) {
       
    60         m_caller = asFunction(function);
       
    61         m_frames.append(ScriptCallFrame(m_caller->name(m_exec), urlString, lineNumber, m_exec, skipArgumentCount));
       
    62     } else {
       
    63         // Caller is unknown, but we should still add the frame, because
       
    64         // something called us, and gave us arguments.
       
    65         m_frames.append(ScriptCallFrame(UString(), urlString, lineNumber, m_exec, skipArgumentCount));
       
    66     }
       
    67 }
       
    68 
       
    69 ScriptCallStack::~ScriptCallStack()
       
    70 {
       
    71 }
       
    72 
       
    73 const ScriptCallFrame &ScriptCallStack::at(unsigned index)
       
    74 {
       
    75     // First frame is pre-populated in constructor, so don't trigger
       
    76     // initialization unless looking beyond the first frame.
       
    77     if (index > 0)
       
    78         initialize();
       
    79     ASSERT(m_frames.size() > index);
       
    80     return m_frames[index];
       
    81 }
       
    82 
       
    83 unsigned ScriptCallStack::size()
       
    84 {
       
    85     initialize();
       
    86     return m_frames.size();
       
    87 }
       
    88 
       
    89 void ScriptCallStack::initialize()
       
    90 {
       
    91     if (!m_caller || m_initialized)
       
    92         return;
       
    93 
       
    94     int signedLineNumber;
       
    95     intptr_t sourceID;
       
    96     UString urlString;
       
    97     JSValue function;
       
    98     // callFrame must exist if m_caller is not null.
       
    99     CallFrame* callFrame = m_exec->callerFrame();
       
   100     while (true) {
       
   101         ASSERT(callFrame);
       
   102         m_exec->interpreter()->retrieveLastCaller(callFrame, signedLineNumber, sourceID, urlString, function);
       
   103         if (!function)
       
   104             break;
       
   105         JSFunction* jsFunction = asFunction(function);
       
   106         unsigned lineNumber = signedLineNumber >= 0 ? signedLineNumber : 0;
       
   107         m_frames.append(ScriptCallFrame(jsFunction->name(m_exec), urlString, lineNumber, m_exec, 0));
       
   108         callFrame = callFrame->callerFrame();
       
   109     }
       
   110     m_initialized = true;
       
   111 }
       
   112 
       
   113 bool ScriptCallStack::stackTrace(int, const RefPtr<InspectorArray>&)
       
   114 {
       
   115     return false;
       
   116 }
       
   117 
       
   118 } // namespace WebCore