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