DOMException.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <LibJS/Runtime/VM.h>
  9. #include <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/HTML/Scripting/Environments.h>
  11. namespace Web::WebIDL {
  12. #define TRY_OR_RETURN_OOM(realm, expression) \
  13. ({ \
  14. auto _temporary_result = (expression); \
  15. if (_temporary_result.is_error()) { \
  16. VERIFY(_temporary_result.error().code() == ENOMEM); \
  17. return WebIDL::UnknownError::create(realm, "Out of memory."sv); \
  18. } \
  19. _temporary_result.release_value(); \
  20. })
  21. // The following have a legacy code value but *don't* produce it as
  22. // DOMException.code value when used as name (and are therefore omitted here):
  23. // - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)
  24. // - NoDataAllowedError (NO_DATA_ALLOWED_ERR = 6)
  25. // - ValidationError (VALIDATION_ERR = 16)
  26. #define ENUMERATE_DOM_EXCEPTION_LEGACY_CODES \
  27. __ENUMERATE(IndexSizeError, 1) \
  28. __ENUMERATE(HierarchyRequestError, 3) \
  29. __ENUMERATE(WrongDocumentError, 4) \
  30. __ENUMERATE(InvalidCharacterError, 5) \
  31. __ENUMERATE(NoModificationAllowedError, 7) \
  32. __ENUMERATE(NotFoundError, 8) \
  33. __ENUMERATE(NotSupportedError, 9) \
  34. __ENUMERATE(InUseAttributeError, 10) \
  35. __ENUMERATE(InvalidStateError, 11) \
  36. __ENUMERATE(SyntaxError, 12) \
  37. __ENUMERATE(InvalidModificationError, 13) \
  38. __ENUMERATE(NamespaceError, 14) \
  39. __ENUMERATE(InvalidAccessError, 15) \
  40. __ENUMERATE(TypeMismatchError, 17) \
  41. __ENUMERATE(SecurityError, 18) \
  42. __ENUMERATE(NetworkError, 19) \
  43. __ENUMERATE(AbortError, 20) \
  44. __ENUMERATE(URLMismatchError, 21) \
  45. __ENUMERATE(QuotaExceededError, 22) \
  46. __ENUMERATE(TimeoutError, 23) \
  47. __ENUMERATE(InvalidNodeTypeError, 24) \
  48. __ENUMERATE(DataCloneError, 25)
  49. // https://webidl.spec.whatwg.org/#idl-DOMException-error-names
  50. // Same order as in the spec document, also matches the legacy codes order above.
  51. #define ENUMERATE_DOM_EXCEPTION_ERROR_NAMES \
  52. __ENUMERATE(IndexSizeError) /* Deprecated */ \
  53. __ENUMERATE(HierarchyRequestError) \
  54. __ENUMERATE(WrongDocumentError) \
  55. __ENUMERATE(InvalidCharacterError) \
  56. __ENUMERATE(NoModificationAllowedError) \
  57. __ENUMERATE(NotFoundError) \
  58. __ENUMERATE(NotSupportedError) \
  59. __ENUMERATE(InUseAttributeError) \
  60. __ENUMERATE(InvalidStateError) \
  61. __ENUMERATE(SyntaxError) \
  62. __ENUMERATE(InvalidModificationError) \
  63. __ENUMERATE(NamespaceError) \
  64. __ENUMERATE(InvalidAccessError) /* Deprecated */ \
  65. __ENUMERATE(TypeMismatchError) /* Deprecated */ \
  66. __ENUMERATE(SecurityError) \
  67. __ENUMERATE(NetworkError) \
  68. __ENUMERATE(AbortError) \
  69. __ENUMERATE(URLMismatchError) \
  70. __ENUMERATE(QuotaExceededError) \
  71. __ENUMERATE(TimeoutError) \
  72. __ENUMERATE(InvalidNodeTypeError) \
  73. __ENUMERATE(DataCloneError) \
  74. __ENUMERATE(EncodingError) \
  75. __ENUMERATE(NotReadableError) \
  76. __ENUMERATE(UnknownError) \
  77. __ENUMERATE(ConstraintError) \
  78. __ENUMERATE(DataError) \
  79. __ENUMERATE(TransactionInactiveError) \
  80. __ENUMERATE(ReadOnlyError) \
  81. __ENUMERATE(VersionError) \
  82. __ENUMERATE(OperationError) \
  83. __ENUMERATE(NotAllowedError)
  84. static u16 get_legacy_code_for_name(FlyString const& name)
  85. {
  86. #define __ENUMERATE(ErrorName, code) \
  87. if (name == #ErrorName) \
  88. return code;
  89. ENUMERATE_DOM_EXCEPTION_LEGACY_CODES
  90. #undef __ENUMERATE
  91. return 0;
  92. }
  93. // https://webidl.spec.whatwg.org/#idl-DOMException
  94. class DOMException final : public Bindings::PlatformObject {
  95. WEB_PLATFORM_OBJECT(DOMException, Bindings::PlatformObject);
  96. public:
  97. static JS::NonnullGCPtr<DOMException> create(JS::Realm& realm, FlyString const& name, FlyString const& message);
  98. // JS constructor has message first, name second
  99. // FIXME: This is a completely pointless footgun, let's use the same order for both factories.
  100. static JS::NonnullGCPtr<DOMException> construct_impl(JS::Realm& realm, FlyString const& message, FlyString const& name);
  101. virtual ~DOMException() override;
  102. FlyString const& name() const { return m_name; }
  103. FlyString const& message() const { return m_message; }
  104. u16 code() const { return get_legacy_code_for_name(m_name); }
  105. protected:
  106. DOMException(JS::Realm&, FlyString const& name, FlyString const& message);
  107. private:
  108. FlyString m_name;
  109. FlyString m_message;
  110. };
  111. #define __ENUMERATE(ErrorName) \
  112. class ErrorName final { \
  113. public: \
  114. static JS::NonnullGCPtr<DOMException> create(JS::Realm& realm, FlyString const& message) \
  115. { \
  116. return DOMException::create(realm, #ErrorName, message); \
  117. } \
  118. };
  119. ENUMERATE_DOM_EXCEPTION_ERROR_NAMES
  120. #undef __ENUMERATE
  121. }
  122. namespace Web {
  123. inline JS::Completion throw_completion(JS::NonnullGCPtr<WebIDL::DOMException> exception)
  124. {
  125. return JS::throw_completion(JS::Value(static_cast<JS::Object*>(exception.ptr())));
  126. }
  127. }