Skip to content

Commit 1fc0dc0

Browse files
committed
feat(vm): compressing identical traces when displaying the stacktrace of an error
1 parent 3568c18 commit 1fc0dc0

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

include/Ark/TypeChecker.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <ostream>
1818
#include <sstream>
1919

20+
#include <Ark/Exceptions.hpp>
2021
#include <Ark/VM/Value.hpp>
2122

2223
namespace Ark::types

src/arkreactor/VM/VM.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,27 +1661,44 @@ namespace Ark
16611661
fmt::println(os, "");
16621662
}
16631663

1664-
if (const uint16_t original_frame_count = context.fc; original_frame_count > 1)
1664+
if (context.fc > 1)
16651665
{
16661666
// display call stack trace
16671667
const ScopeView old_scope = context.locals.back();
16681668

1669+
std::string previous_trace;
1670+
std::size_t displayed_traces = 0;
1671+
std::size_t consecutive_similar_traces = 0;
1672+
16691673
while (context.fc != 0)
16701674
{
16711675
const auto maybe_call_loc = findSourceLocation(context.ip, context.pp);
16721676
const auto loc_as_text = maybe_call_loc ? fmt::format(" ({}:{})", m_state.m_filenames[maybe_call_loc->filename_id], maybe_call_loc->line + 1) : "";
16731677

1674-
fmt::print(os, "[{}] ", fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()));
16751678
if (context.pp != 0)
16761679
{
16771680
const uint16_t id = findNearestVariableIdWithValue(
16781681
Value(static_cast<PageAddr_t>(context.pp)),
16791682
context);
1683+
const auto func_name = (id < m_state.m_symbols.size()) ? m_state.m_symbols[id] : "???";
16801684

1681-
if (id < m_state.m_symbols.size())
1682-
fmt::println(os, "In function `{}'{}", fmt::styled(m_state.m_symbols[id], colorize ? fmt::fg(fmt::color::green) : fmt::text_style()), loc_as_text);
1683-
else // should never happen
1684-
fmt::println(os, "In function `{}'{}", fmt::styled("???", colorize ? fmt::fg(fmt::color::gold) : fmt::text_style()), loc_as_text);
1685+
if (func_name + loc_as_text != previous_trace)
1686+
{
1687+
fmt::println(
1688+
os,
1689+
"[{:4}] In function `{}'{}",
1690+
fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()),
1691+
fmt::styled(func_name, colorize ? fmt::fg(fmt::color::green) : fmt::text_style()),
1692+
loc_as_text);
1693+
previous_trace = func_name + loc_as_text;
1694+
++displayed_traces;
1695+
consecutive_similar_traces = 0;
1696+
}
1697+
else if (consecutive_similar_traces == 0)
1698+
{
1699+
fmt::println("{0:^{1}}", "...", 21 + func_name.size() + loc_as_text.size());
1700+
++consecutive_similar_traces;
1701+
}
16851702

16861703
Value* ip;
16871704
do
@@ -1695,11 +1712,11 @@ namespace Ark
16951712
}
16961713
else
16971714
{
1698-
fmt::println(os, "In global scope{}", loc_as_text);
1715+
fmt::println(os, "[{:4}] In global scope{}", fmt::styled(context.fc, colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style()), loc_as_text);
16991716
break;
17001717
}
17011718

1702-
if (original_frame_count - context.fc > 7)
1719+
if (displayed_traces > 7)
17031720
{
17041721
fmt::println(os, "...");
17051722
break;

tests/unittests/resources/DiagnosticsSuite/runtime/not_enough_args.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ In file tests/unittests/resources/DiagnosticsSuite/runtime/not_enough_args.ark
66
2 | (foo 1)
77
3 |
88

9-
[2] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/not_enough_args.ark:1)
10-
[1] In global scope (tests/unittests/resources/DiagnosticsSuite/runtime/not_enough_args.ark:2)
9+
[ 2] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/not_enough_args.ark:1)
10+
[ 1] In global scope (tests/unittests/resources/DiagnosticsSuite/runtime/not_enough_args.ark:2)
1111

1212
Current scope variables values:
1313
foo = Function @ 1

tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.expected

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@ In file tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark
99
5 | }))
1010

1111
[2047] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
12-
[2046] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
13-
[2045] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
14-
[2044] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
15-
[2043] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
16-
[2042] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
17-
[2041] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
18-
[2040] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:3)
19-
...
12+
[ 1] In global scope (tests/unittests/resources/DiagnosticsSuite/runtime/recursion_depth.ark:7)
2013

2114
Current scope variables values:
2215
foo = Function @ 1

tests/unittests/resources/DiagnosticsSuite/runtime/too_many_args.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ In file tests/unittests/resources/DiagnosticsSuite/runtime/too_many_args.ark
66
2 | (foo 1 2 3)
77
3 |
88

9-
[2] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/too_many_args.ark:1)
10-
[1] In global scope (tests/unittests/resources/DiagnosticsSuite/runtime/too_many_args.ark:2)
9+
[ 2] In function `foo' (tests/unittests/resources/DiagnosticsSuite/runtime/too_many_args.ark:1)
10+
[ 1] In global scope (tests/unittests/resources/DiagnosticsSuite/runtime/too_many_args.ark:2)
1111

1212
Current scope variables values:
1313
foo = Function @ 1

0 commit comments

Comments
 (0)