This patch combine three patch which is related to "--gcov" flag.
[platform/framework/web/chromium-efl.git] / gin / try_catch.cc
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gin/try_catch.h"
6
7 #include <sstream>
8
9 #include "gin/converter.h"
10 #include "v8/include/v8-message.h"
11
12 namespace {
13
14 v8::Local<v8::String> GetSourceLine(v8::Isolate* isolate,
15                                     v8::Local<v8::Message> message) {
16   auto maybe = message->GetSourceLine(isolate->GetCurrentContext());
17   v8::Local<v8::String> source_line;
18   return maybe.ToLocal(&source_line) ? source_line : v8::String::Empty(isolate);
19 }
20
21 }  // namespace
22
23 namespace gin {
24
25 TryCatch::TryCatch(v8::Isolate* isolate)
26     : isolate_(isolate), try_catch_(isolate) {
27 }
28
29 TryCatch::~TryCatch() = default;
30
31 bool TryCatch::HasCaught() {
32   return try_catch_.HasCaught();
33 }
34
35 std::string TryCatch::GetStackTrace() {
36   if (!HasCaught()) {
37     return "";
38   }
39
40   std::stringstream ss;
41   v8::Local<v8::Message> message = try_catch_.Message();
42   ss << V8ToString(isolate_, message->Get()) << std::endl
43      << V8ToString(isolate_, GetSourceLine(isolate_, message)) << std::endl;
44
45   v8::Local<v8::StackTrace> trace = message->GetStackTrace();
46   if (trace.IsEmpty())
47     return ss.str();
48
49   int len = trace->GetFrameCount();
50   for (int i = 0; i < len; ++i) {
51     v8::Local<v8::StackFrame> frame = trace->GetFrame(isolate_, i);
52     ss << V8ToString(isolate_, frame->GetScriptName()) << ":"
53        << frame->GetLineNumber() << ":" << frame->GetColumn() << ": "
54        << V8ToString(isolate_, frame->GetFunctionName()) << std::endl;
55   }
56   return ss.str();
57 }
58
59 }  // namespace gin