2020-03-24 18:03:50 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-04-24 15:16:31 -03:00
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
2020-03-24 18:03:50 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-24 18:03:50 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-09-27 15:18:30 -03:00
|
|
|
#include <AK/String.h>
|
2020-12-28 14:15:22 -03:00
|
|
|
#include <LibJS/AST.h>
|
2021-03-21 08:18:56 -03:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-24 18:03:50 -03:00
|
|
|
#include <LibJS/Runtime/Exception.h>
|
2020-09-27 15:18:30 -03:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-03-24 18:03:50 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
Exception::Exception(Value value)
|
|
|
|
|
: m_value(value)
|
|
|
|
|
{
|
2021-03-21 08:18:56 -03:00
|
|
|
auto& vm = this->vm();
|
2021-06-24 14:17:45 -03:00
|
|
|
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;
|
2020-06-02 10:11:59 -03:00
|
|
|
if (function_name.is_empty())
|
|
|
|
|
function_name = "<anonymous>";
|
2021-06-03 07:43:38 -03:00
|
|
|
m_traceback.empend(
|
|
|
|
|
move(function_name),
|
2021-06-24 14:17:45 -03:00
|
|
|
// We might not have an AST node associated with the execution context, e.g. in promise
|
2021-04-25 16:45:23 -03:00
|
|
|
// 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.
|
2021-06-24 14:17:45 -03:00
|
|
|
context->current_node ? context->current_node->source_range() : SourceRange {});
|
2020-06-02 10:11:59 -03:00
|
|
|
}
|
2020-03-24 18:03:50 -03:00
|
|
|
}
|
|
|
|
|
|
2020-11-28 10:33:36 -03:00
|
|
|
void Exception::visit_edges(Visitor& visitor)
|
2020-03-24 18:03:50 -03:00
|
|
|
{
|
2020-11-28 10:33:36 -03:00
|
|
|
Cell::visit_edges(visitor);
|
2020-03-24 18:03:50 -03:00
|
|
|
visitor.visit(m_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|