1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:47:35 +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

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,23 +17,16 @@ Exception::Exception(Value value)
: m_value(value)
{
auto& vm = this->vm();
auto& call_stack = vm.call_stack();
for (ssize_t i = call_stack.size() - 1; i >= 0; --i) {
String function_name = call_stack[i]->function_name;
m_traceback.ensure_capacity(vm.call_stack().size());
for (auto* call_frame : vm.call_stack()) {
auto function_name = call_frame->function_name;
if (function_name.is_empty())
function_name = "<anonymous>";
m_trace.append(function_name);
m_traceback.prepend({
.function_name = move(function_name),
.source_range = call_frame->current_node->source_range(),
});
}
if (auto* interpreter = vm.interpreter_if_exists()) {
for (auto* node_chain = interpreter->executing_ast_node_chain(); node_chain; node_chain = node_chain->previous) {
m_source_ranges.append(node_chain->node.source_range());
}
}
}
Exception::~Exception()
{
}
void Exception::visit_edges(Visitor& visitor)

View file

@ -1,11 +1,13 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/Cell.h>
#include <LibJS/Runtime/Value.h>
@ -13,22 +15,25 @@
namespace JS {
struct TracebackFrame {
FlyString function_name;
SourceRange source_range;
};
class Exception : public Cell {
public:
explicit Exception(Value);
virtual ~Exception() override;
virtual ~Exception() override = default;
Value value() const { return m_value; }
const Vector<String>& trace() const { return m_trace; }
const Vector<SourceRange>& source_ranges() const { return m_source_ranges; }
const Vector<TracebackFrame>& traceback() const { return m_traceback; }
private:
virtual const char* class_name() const override { return "Exception"; }
virtual void visit_edges(Visitor&) override;
Value m_value;
Vector<String> m_trace;
Vector<SourceRange> m_source_ranges;
Vector<TracebackFrame> m_traceback;
};
}