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.call_stack().size());
  18. for (auto* call_frame : vm.call_stack()) {
  19. auto function_name = call_frame->function_name;
  20. if (function_name.is_empty())
  21. function_name = "<anonymous>";
  22. m_traceback.prepend({
  23. .function_name = move(function_name),
  24. // We might not have an AST node associated with the call frame, e.g. in promise
  25. // reaction jobs (which aren't called anywhere from the source code).
  26. // They're not going to generate any _unhandled_ exceptions though, so a meaningless
  27. // source range is fine.
  28. .source_range = call_frame->current_node ? call_frame->current_node->source_range() : SourceRange {},
  29. });
  30. }
  31. }
  32. void Exception::visit_edges(Visitor& visitor)
  33. {
  34. Cell::visit_edges(visitor);
  35. visitor.visit(m_value);
  36. }
  37. }