1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

LibJS: Consolidate exception function names and source ranges

Instead of storing the function names (in a badly named Vector<String>)
and source ranges separately, consolidate them into a new struct:
TracebackFrame. This makes it both easier to use now and easier to
extend in the future.
Unlike before we now keep each call frame's current node source range
in the traceback frame next to the function name, meaning we can display
line and column numbers outside of the VM and after the call stack is
emptied.
This commit is contained in:
Linus Groh 2021-04-24 18:15:02 +02:00 committed by Andreas Kling
parent 0cf04d07aa
commit 97d49cb92b
5 changed files with 39 additions and 33 deletions

View file

@ -489,24 +489,27 @@ static bool parse_and_run(JS::Interpreter& interpreter, const StringView& source
vm->clear_exception();
out("Uncaught exception: ");
print(exception->value());
auto& trace = exception->trace();
if (trace.size() > 1) {
auto& traceback = exception->traceback();
if (traceback.size() > 1) {
unsigned repetitions = 0;
for (size_t i = 0; i < trace.size(); ++i) {
auto& function_name = trace[i];
if (i + 1 < trace.size() && trace[i + 1] == function_name) {
repetitions++;
continue;
for (size_t i = 0; i < traceback.size(); ++i) {
auto& traceback_frame = traceback[i];
if (i + 1 < traceback.size()) {
auto& next_traceback_frame = traceback[i + 1];
if (next_traceback_frame.function_name == traceback_frame.function_name) {
repetitions++;
continue;
}
}
if (repetitions > 4) {
// If more than 5 (1 + >4) consecutive function calls with the same name, print
// the name only once and show the number of repetitions instead. This prevents
// printing ridiculously large call stacks of recursive functions.
outln(" -> {}", function_name);
outln(" -> {}", traceback_frame.function_name);
outln(" {} more calls", repetitions);
} else {
for (size_t j = 0; j < repetitions + 1; ++j)
outln(" -> {}", function_name);
outln(" -> {}", traceback_frame.function_name);
}
repetitions = 0;
}