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 "bindings/v8/ScriptCallStackFactory.h"
     33 
     34 #include "bindings/v8/ScriptScope.h"
     35 #include "bindings/v8/ScriptValue.h"
     36 #include "bindings/v8/V8Binding.h"
     37 #include "bindings/v8/V8Utilities.h"
     38 #include "core/inspector/InspectorInstrumentation.h"
     39 #include "core/inspector/ScriptArguments.h"
     40 #include "core/inspector/ScriptCallFrame.h"
     41 #include "core/inspector/ScriptCallStack.h"
     42 #include "core/platform/JSONValues.h"
     43 
     44 #include <v8-debug.h>
     45 
     46 namespace WebCore {
     47 
     48 class ScriptExecutionContext;
     49 
     50 static ScriptCallFrame toScriptCallFrame(v8::Handle<v8::StackFrame> frame)
     51 {
     52     String sourceName;
     53     v8::Local<v8::String> sourceNameValue(frame->GetScriptNameOrSourceURL());
     54     if (!sourceNameValue.IsEmpty())
     55         sourceName = toWebCoreString(sourceNameValue);
     56 
     57     String functionName;
     58     v8::Local<v8::String> functionNameValue(frame->GetFunctionName());
     59     if (!functionNameValue.IsEmpty())
     60         functionName = toWebCoreString(functionNameValue);
     61 
     62     int sourceLineNumber = frame->GetLineNumber();
     63     int sourceColumn = frame->GetColumn();
     64     return ScriptCallFrame(functionName, sourceName, sourceLineNumber, sourceColumn);
     65 }
     66 
     67 static void toScriptCallFramesVector(v8::Handle<v8::StackTrace> stackTrace, Vector<ScriptCallFrame>& scriptCallFrames, size_t maxStackSize, bool emptyStackIsAllowed)
     68 {
     69     ASSERT(v8::Context::InContext());
     70     int frameCount = stackTrace->GetFrameCount();
     71     if (frameCount > static_cast<int>(maxStackSize))
     72         frameCount = maxStackSize;
     73     for (int i = 0; i < frameCount; i++) {
     74         v8::Local<v8::StackFrame> stackFrame = stackTrace->GetFrame(i);
     75         scriptCallFrames.append(toScriptCallFrame(stackFrame));
     76     }
     77     if (!frameCount && !emptyStackIsAllowed) {
     78         // Successfully grabbed stack trace, but there are no frames. It may happen in case
     79         // when a bound function is called from native code for example.
     80         // Fallback to setting lineNumber to 0, and source and function name to "undefined".
     81         scriptCallFrames.append(ScriptCallFrame("undefined", "undefined", 0));
     82     }
     83 }
     84 
     85 static PassRefPtr<ScriptCallStack> createScriptCallStack(v8::Handle<v8::StackTrace> stackTrace, size_t maxStackSize, bool emptyStackIsAllowed)
     86 {
     87     ASSERT(v8::Context::InContext());
     88     v8::HandleScope scope;
     89     Vector<ScriptCallFrame> scriptCallFrames;
     90     toScriptCallFramesVector(stackTrace, scriptCallFrames, maxStackSize, emptyStackIsAllowed);
     91     return ScriptCallStack::create(scriptCallFrames);
     92 }
     93 
     94 PassRefPtr<ScriptCallStack> createScriptCallStack(v8::Handle<v8::StackTrace> stackTrace, size_t maxStackSize)
     95 {
     96     return createScriptCallStack(stackTrace, maxStackSize, true);
     97 }
     98 
     99 PassRefPtr<ScriptCallStack> createScriptCallStack(size_t maxStackSize, bool emptyStackIsAllowed)
    100 {
    101     if (!v8::Context::InContext())
    102         return 0;
    103     v8::HandleScope handleScope;
    104     v8::Handle<v8::StackTrace> stackTrace(v8::StackTrace::CurrentStackTrace(maxStackSize, stackTraceOptions));
    105     return createScriptCallStack(stackTrace, maxStackSize, emptyStackIsAllowed);
    106 }
    107 
    108 PassRefPtr<ScriptCallStack> createScriptCallStackForConsole(size_t maxStackSize)
    109 {
    110     size_t stackSize = 1;
    111     if (InspectorInstrumentation::hasFrontends()) {
    112         ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext();
    113         if (InspectorInstrumentation::consoleAgentEnabled(scriptExecutionContext))
    114             stackSize = maxStackSize;
    115     }
    116     return createScriptCallStack(stackSize);
    117 }
    118 
    119 PassRefPtr<ScriptCallStack> createScriptCallStackForConsole(ScriptState*)
    120 {
    121     return createScriptCallStackForConsole();
    122 }
    123 
    124 PassRefPtr<ScriptCallStack> createScriptCallStack(ScriptState*, size_t maxStackSize)
    125 {
    126     return createScriptCallStackForConsole(maxStackSize);
    127 }
    128 
    129 PassRefPtr<ScriptArguments> createScriptArguments(const v8::FunctionCallbackInfo<v8::Value>& v8arguments, unsigned skipArgumentCount)
    130 {
    131     v8::HandleScope scope;
    132     v8::Local<v8::Context> context = v8::Context::GetCurrent();
    133     ScriptState* state = ScriptState::forContext(context);
    134 
    135     Vector<ScriptValue> arguments;
    136     for (int i = skipArgumentCount; i < v8arguments.Length(); ++i)
    137         arguments.append(ScriptValue(v8arguments[i]));
    138 
    139     return ScriptArguments::create(state, arguments);
    140 }
    141 
    142 } // namespace WebCore
    143