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