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