Home | History | Annotate | Download | only in js
      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/InternalFunction.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, const ArgList& args, 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 = asInternalFunction(function);
     61         m_frames.append(ScriptCallFrame(m_caller->name(m_exec), urlString, lineNumber, args, 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, args, 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     JSValue func = m_exec->interpreter()->retrieveCaller(m_exec, m_caller);
     95     while (!func.isNull()) {
     96         InternalFunction* internalFunction = asInternalFunction(func);
     97         ArgList emptyArgList;
     98         m_frames.append(ScriptCallFrame(internalFunction->name(m_exec), UString(), 0, emptyArgList, 0));
     99         func = m_exec->interpreter()->retrieveCaller(m_exec, internalFunction);
    100     }
    101     m_initialized = true;
    102 }
    103 
    104 } // namespace WebCore
    105