DOMException.h 6.5 KB

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