[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 | |||||
Chris Watkins | 756035a | 2017-12-01 03:03:27 | [diff] [blame] | 28 | TryCatch::~TryCatch() = default; |
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 29 | |
30 | bool TryCatch::HasCaught() { | ||||
31 | return try_catch_.HasCaught(); | ||||
32 | } | ||||
33 | |||||
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 34 | std::string TryCatch::GetStackTrace() { |
[email protected] | bf3dd3c | 2013-12-06 06:55:25 | [diff] [blame] | 35 | if (!HasCaught()) { |
36 | return ""; | ||||
37 | } | ||||
38 | |||||
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 39 | std::stringstream ss; |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 40 | v8::Local<v8::Message> message = try_catch_.Message(); |
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 41 | ss << V8ToString(isolate_, message->Get()) << std::endl |
42 | << V8ToString(isolate_, GetSourceLine(isolate_, message)) << std::endl; | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 43 | |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 44 | v8::Local<v8::StackTrace> trace = message->GetStackTrace(); |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 45 | if (trace.IsEmpty()) |
46 | return ss.str(); | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 47 | |
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 48 | int len = trace->GetFrameCount(); |
49 | for (int i = 0; i < len; ++i) { | ||||
Dan Elphick | 38a50805 | 2018-07-23 22:19:53 | [diff] [blame] | 50 | v8::Local<v8::StackFrame> frame = trace->GetFrame(isolate_, i); |
51 | ss << V8ToString(isolate_, frame->GetScriptName()) << ":" | ||||
52 | << frame->GetLineNumber() << ":" << frame->GetColumn() << ": " | ||||
53 | << V8ToString(isolate_, frame->GetFunctionName()) << std::endl; | ||||
[email protected] | 2f70342 | 2013-11-25 21:26:15 | [diff] [blame] | 54 | } |
55 | return ss.str(); | ||||
[email protected] | 855ab43 | 2013-11-18 17:09:36 | [diff] [blame] | 56 | } |
57 | |||||
58 | } // namespace gin |