Error.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/Object.h>
  11. #include <LibJS/SourceRange.h>
  12. namespace JS {
  13. struct TracebackFrame {
  14. FlyString function_name;
  15. SourceRange source_range;
  16. };
  17. class Error : public Object {
  18. JS_OBJECT(Error, Object);
  19. public:
  20. static Error* create(Realm&);
  21. static Error* create(Realm&, String const& message);
  22. virtual ~Error() override = default;
  23. [[nodiscard]] String stack_string() const;
  24. ThrowCompletionOr<void> install_error_cause(Value options);
  25. Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
  26. protected:
  27. explicit Error(Object& prototype);
  28. private:
  29. void populate_stack();
  30. Vector<TracebackFrame, 32> m_traceback;
  31. };
  32. // NOTE: Making these inherit from Error is not required by the spec but
  33. // our way of implementing the [[ErrorData]] internal slot, which is
  34. // used in Object.prototype.toString().
  35. #define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
  36. class ClassName final : public Error { \
  37. JS_OBJECT(ClassName, Error); \
  38. \
  39. public: \
  40. static ClassName* create(Realm&); \
  41. static ClassName* create(Realm&, String const& message); \
  42. \
  43. explicit ClassName(Object& prototype); \
  44. virtual ~ClassName() override = default; \
  45. };
  46. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  47. DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName)
  48. JS_ENUMERATE_NATIVE_ERRORS
  49. #undef __JS_ENUMERATE
  50. }