blob: 821719294cf46b36ad529fcd0305e91f8dec4e06 [file] [log] [blame]
[email protected]855ab432013-11-18 17:09:361// 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]2f703422013-11-25 21:26:157#include <sstream>
8
[email protected]855ab432013-11-18 17:09:369#include "gin/converter.h"
10
bashidbd2ef9bb2015-06-02 01:39:3211namespace {
12
13v8::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]855ab432013-11-18 17:09:3622namespace gin {
23
bashidbd2ef9bb2015-06-02 01:39:3224TryCatch::TryCatch(v8::Isolate* isolate)
25 : isolate_(isolate), try_catch_(isolate) {
[email protected]855ab432013-11-18 17:09:3626}
27
Chris Watkins756035a2017-12-01 03:03:2728TryCatch::~TryCatch() = default;
[email protected]855ab432013-11-18 17:09:3629
30bool TryCatch::HasCaught() {
31 return try_catch_.HasCaught();
32}
33
[email protected]2f703422013-11-25 21:26:1534std::string TryCatch::GetStackTrace() {
[email protected]bf3dd3c2013-12-06 06:55:2535 if (!HasCaught()) {
36 return "";
37 }
38
[email protected]2f703422013-11-25 21:26:1539 std::stringstream ss;
deepak.sfaaa1b62015-04-30 07:30:4840 v8::Local<v8::Message> message = try_catch_.Message();
Dan Elphick38a508052018-07-23 22:19:5341 ss << V8ToString(isolate_, message->Get()) << std::endl
42 << V8ToString(isolate_, GetSourceLine(isolate_, message)) << std::endl;
[email protected]855ab432013-11-18 17:09:3643
deepak.sfaaa1b62015-04-30 07:30:4844 v8::Local<v8::StackTrace> trace = message->GetStackTrace();
[email protected]2f703422013-11-25 21:26:1545 if (trace.IsEmpty())
46 return ss.str();
[email protected]855ab432013-11-18 17:09:3647
[email protected]2f703422013-11-25 21:26:1548 int len = trace->GetFrameCount();
49 for (int i = 0; i < len; ++i) {
Dan Elphick38a508052018-07-23 22:19:5350 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]2f703422013-11-25 21:26:1554 }
55 return ss.str();
[email protected]855ab432013-11-18 17:09:3656}
57
58} // namespace gin