Exception.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/String.h>
  8. #include <LibJS/AST.h>
  9. #include <LibJS/Interpreter.h>
  10. #include <LibJS/Runtime/Exception.h>
  11. #include <LibJS/Runtime/VM.h>
  12. namespace JS {
  13. Exception::Exception(Value value)
  14. : m_value(value)
  15. {
  16. auto& vm = this->vm();
  17. m_traceback.ensure_capacity(vm.execution_context_stack().size());
  18. for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {
  19. auto* context = vm.execution_context_stack()[i];
  20. auto function_name = context->function_name;
  21. if (function_name.is_empty())
  22. function_name = "<anonymous>";
  23. m_traceback.empend(
  24. move(function_name),
  25. // We might not have an AST node associated with the execution context, e.g. in promise
  26. // reaction jobs (which aren't called anywhere from the source code).
  27. // They're not going to generate any _unhandled_ exceptions though, so a meaningless
  28. // source range is fine.
  29. context->current_node ? context->current_node->source_range() : SourceRange {});
  30. }
  31. }
  32. void Exception::visit_edges(Visitor& visitor)
  33. {
  34. Cell::visit_edges(visitor);
  35. visitor.visit(m_value);
  36. }
  37. }