Error.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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(GlobalObject&);
  21. static Error* create(GlobalObject&, String const& message);
  22. explicit Error(Object& prototype);
  23. virtual ~Error() override = default;
  24. [[nodiscard]] String stack_string() const;
  25. ThrowCompletionOr<void> install_error_cause(Value options);
  26. Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
  27. private:
  28. void populate_stack();
  29. Vector<TracebackFrame, 32> m_traceback;
  30. };
  31. // NOTE: Making these inherit from Error is not required by the spec but
  32. // our way of implementing the [[ErrorData]] internal slot, which is
  33. // used in Object.prototype.toString().
  34. #define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
  35. class ClassName final : public Error { \
  36. JS_OBJECT(ClassName, Error); \
  37. \
  38. public: \
  39. static ClassName* create(GlobalObject&); \
  40. static ClassName* create(GlobalObject&, String const& message); \
  41. \
  42. explicit ClassName(Object& prototype); \
  43. virtual ~ClassName() override = default; \
  44. };
  45. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  46. DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName)
  47. JS_ENUMERATE_NATIVE_ERRORS
  48. #undef __JS_ENUMERATE
  49. }