[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #include "gin/try_catch.h" | ||||
6 | |||||
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 7 | #include <sstream> |
8 | |||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 9 | #include "gin/converter.h" |
10 | |||||
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame^] | 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 | |||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 22 | namespace gin { |
23 | |||||
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame^] | 24 | TryCatch::TryCatch(v8::Isolate* isolate) |
25 | : isolate_(isolate), try_catch_(isolate) { | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 26 | } |
27 | |||||
28 | TryCatch::~TryCatch() { | ||||
29 | } | ||||
30 | |||||
31 | bool TryCatch::HasCaught() { | ||||
32 | return try_catch_.HasCaught(); | ||||
33 | } | ||||
34 | |||||
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 35 | std::string TryCatch::GetStackTrace() { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 36 | if (!HasCaught()) { |
37 | return ""; | ||||
38 | } | ||||
39 | |||||
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 40 | std::stringstream ss; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 41 | v8::Local<v8::Message> message = try_catch_.Message(); |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 42 | ss << V8ToString(message->Get()) << std::endl |
bashi | dbd2ef9bb | 2015-06-02 01:39:32 | [diff] [blame^] | 43 | << V8ToString(GetSourceLine(isolate_, message)) << std::endl; |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 44 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 45 | v8::Local<v8::StackTrace> trace = message->GetStackTrace(); |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 46 | if (trace.IsEmpty()) |
47 | return ss.str(); | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 48 | |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 49 | int len = trace->GetFrameCount(); |
50 | for (int i = 0; i < len; ++i) { | ||||
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 51 | v8::Local<v8::StackFrame> frame = trace->GetFrame(i); |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 52 | ss << V8ToString(frame->GetScriptName()) << ":" |
53 | << frame->GetLineNumber() << ":" | ||||
54 | << frame->GetColumn() << ": " | ||||
55 | << V8ToString(frame->GetFunctionName()) | ||||
56 | << std::endl; | ||||
57 | } | ||||
58 | return ss.str(); | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 59 | } |
60 | |||||
61 | } // namespace gin |