1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:47:45 +00:00

LibJS: Add a traceback to Error

Since we have the traceback we now also generate the stack string on
demand instead of immediately on construction.
This commit is contained in:
davidot 2022-02-07 14:48:09 +01:00 committed by Linus Groh
parent f324d91106
commit e160f508a8
3 changed files with 39 additions and 18 deletions

View file

@ -54,29 +54,46 @@ ThrowCompletionOr<void> Error::install_error_cause(Value options)
void Error::populate_stack()
{
AK::StringBuilder stack_string_builder {};
auto& vm = this->vm();
m_traceback.ensure_capacity(vm.execution_context_stack().size());
for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {
auto* context = vm.execution_context_stack()[i];
auto function_name = context->function_name;
if (function_name.is_empty())
function_name = "<unknown>"sv;
m_traceback.empend(
move(function_name),
// We might not have an AST node associated with the execution context, e.g. in promise
// reaction jobs (which aren't called anywhere from the source code).
// They're not going to generate any _unhandled_ exceptions though, so a meaningless
// source range is fine.
context->current_node ? context->current_node->source_range() : SourceRange {});
}
}
String Error::stack_string() const
{
StringBuilder stack_string_builder;
// Note: We roughly follow V8's formatting
// Note: The error's name and message get prepended by ErrorPrototype::stack
// Note: We don't want to capture the global exectution context, so we omit the last frame
// FIXME: We generate a stack-frame for the Errors constructor, other engines do not
for (size_t i = vm().execution_context_stack().size() - 1; i > 0; --i) {
auto const* frame = vm().execution_context_stack()[i];
for (size_t i = 0; i < m_traceback.size() - 1; ++i) {
auto const& frame = m_traceback[i];
auto function_name = frame.function_name;
// Note: Since we don't know whether we have a valid SourceRange here we just check for some default values.
if (!frame.source_range.filename.is_null() || frame.source_range.start.offset != 0 || frame.source_range.end.offset != 0) {
auto function_name = frame->function_name;
if (auto const* current_node = frame->current_node) {
auto const& source_range = current_node->source_range();
if (function_name.is_empty())
stack_string_builder.appendff(" at {}:{}:{}\n", source_range.filename, source_range.start.line, source_range.start.column);
if (function_name == "<unknown>"sv)
stack_string_builder.appendff(" at {}:{}:{}\n", frame.source_range.filename, frame.source_range.start.line, frame.source_range.start.column);
else
stack_string_builder.appendff(" at {} ({}:{}:{})\n", function_name, source_range.filename, source_range.start.line, source_range.start.column);
stack_string_builder.appendff(" at {} ({}:{}:{})\n", function_name, frame.source_range.filename, frame.source_range.start.line, frame.source_range.start.column);
} else {
stack_string_builder.appendff(" at {}\n", function_name.is_empty() ? "<unknown>"sv : function_name.view());
}
}
m_stack_string = stack_string_builder.build();
return stack_string_builder.build();
}
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \

View file

@ -10,9 +10,15 @@
#include <AK/FlyString.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/SourceRange.h>
namespace JS {
struct TracebackFrame {
FlyString function_name;
SourceRange source_range;
};
class Error : public Object {
JS_OBJECT(Error, Object);
@ -23,13 +29,15 @@ public:
explicit Error(Object& prototype);
virtual ~Error() override = default;
String const& stack_string() const { return m_stack_string; }
[[nodiscard]] String stack_string() const;
ThrowCompletionOr<void> install_error_cause(Value options);
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
private:
void populate_stack();
String m_stack_string {};
Vector<TracebackFrame, 32> m_traceback;
};
// NOTE: Making these inherit from Error is not required by the spec but

View file

@ -10,16 +10,12 @@
#include <AK/FlyString.h>
#include <AK/Vector.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/SourceRange.h>
namespace JS {
struct TracebackFrame {
FlyString function_name;
SourceRange source_range;
};
class Exception : public Cell {
public:
explicit Exception(Value);