1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:37:34 +00:00

LibWeb: Move DOMException from DOM/ to WebIDL/

This commit is contained in:
Linus Groh 2022-09-25 17:28:46 +01:00
parent ad04d7ac9b
commit bbaa05fcf9
49 changed files with 206 additions and 203 deletions

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebIDL/DOMException.h>
namespace Web::WebIDL {
JS::NonnullGCPtr<DOMException> DOMException::create(JS::Object& global_object, FlyString const& name, FlyString const& message)
{
auto& window = verify_cast<HTML::Window>(global_object);
return *window.heap().allocate<DOMException>(window.realm(), window, name, message);
}
JS::NonnullGCPtr<DOMException> DOMException::create_with_global_object(JS::Object& global_object, FlyString const& message, FlyString const& name)
{
auto& window = verify_cast<HTML::Window>(global_object);
return *window.heap().allocate<DOMException>(window.realm(), window, name, message);
}
DOMException::DOMException(HTML::Window& window, FlyString const& name, FlyString const& message)
: PlatformObject(window.realm())
, m_name(name)
, m_message(message)
{
set_prototype(&window.cached_web_prototype("DOMException"));
}
DOMException::~DOMException() = default;
}

View file

@ -0,0 +1,147 @@
/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::WebIDL {
#define TRY_OR_RETURN_OOM(global_object, expression) \
({ \
auto _temporary_result = (expression); \
if (_temporary_result.is_error()) { \
VERIFY(_temporary_result.error().code() == ENOMEM); \
return WebIDL::UnknownError::create(global_object, "Out of memory."sv); \
} \
_temporary_result.release_value(); \
})
// The following have a legacy code value but *don't* produce it as
// DOMException.code value when used as name (and are therefore omitted here):
// - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)
// - NoDataAllowedError (NO_DATA_ALLOWED_ERR = 6)
// - ValidationError (VALIDATION_ERR = 16)
#define ENUMERATE_DOM_EXCEPTION_LEGACY_CODES \
__ENUMERATE(IndexSizeError, 1) \
__ENUMERATE(HierarchyRequestError, 3) \
__ENUMERATE(WrongDocumentError, 4) \
__ENUMERATE(InvalidCharacterError, 5) \
__ENUMERATE(NoModificationAllowedError, 7) \
__ENUMERATE(NotFoundError, 8) \
__ENUMERATE(NotSupportedError, 9) \
__ENUMERATE(InUseAttributeError, 10) \
__ENUMERATE(InvalidStateError, 11) \
__ENUMERATE(SyntaxError, 12) \
__ENUMERATE(InvalidModificationError, 13) \
__ENUMERATE(NamespaceError, 14) \
__ENUMERATE(InvalidAccessError, 15) \
__ENUMERATE(TypeMismatchError, 17) \
__ENUMERATE(SecurityError, 18) \
__ENUMERATE(NetworkError, 19) \
__ENUMERATE(AbortError, 20) \
__ENUMERATE(URLMismatchError, 21) \
__ENUMERATE(QuotaExceededError, 22) \
__ENUMERATE(TimeoutError, 23) \
__ENUMERATE(InvalidNodeTypeError, 24) \
__ENUMERATE(DataCloneError, 25)
// https://webidl.spec.whatwg.org/#idl-DOMException-error-names
// Same order as in the spec document, also matches the legacy codes order above.
#define ENUMERATE_DOM_EXCEPTION_ERROR_NAMES \
__ENUMERATE(IndexSizeError) /* Deprecated */ \
__ENUMERATE(HierarchyRequestError) \
__ENUMERATE(WrongDocumentError) \
__ENUMERATE(InvalidCharacterError) \
__ENUMERATE(NoModificationAllowedError) \
__ENUMERATE(NotFoundError) \
__ENUMERATE(NotSupportedError) \
__ENUMERATE(InUseAttributeError) \
__ENUMERATE(InvalidStateError) \
__ENUMERATE(SyntaxError) \
__ENUMERATE(InvalidModificationError) \
__ENUMERATE(NamespaceError) \
__ENUMERATE(InvalidAccessError) /* Deprecated */ \
__ENUMERATE(TypeMismatchError) /* Deprecated */ \
__ENUMERATE(SecurityError) \
__ENUMERATE(NetworkError) \
__ENUMERATE(AbortError) \
__ENUMERATE(URLMismatchError) \
__ENUMERATE(QuotaExceededError) \
__ENUMERATE(TimeoutError) \
__ENUMERATE(InvalidNodeTypeError) \
__ENUMERATE(DataCloneError) \
__ENUMERATE(EncodingError) \
__ENUMERATE(NotReadableError) \
__ENUMERATE(UnknownError) \
__ENUMERATE(ConstraintError) \
__ENUMERATE(DataError) \
__ENUMERATE(TransactionInactiveError) \
__ENUMERATE(ReadOnlyError) \
__ENUMERATE(VersionError) \
__ENUMERATE(OperationError) \
__ENUMERATE(NotAllowedError)
static u16 get_legacy_code_for_name(FlyString const& name)
{
#define __ENUMERATE(ErrorName, code) \
if (name == #ErrorName) \
return code;
ENUMERATE_DOM_EXCEPTION_LEGACY_CODES
#undef __ENUMERATE
return 0;
}
// https://webidl.spec.whatwg.org/#idl-DOMException
class DOMException final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(DOMException, Bindings::PlatformObject);
public:
static JS::NonnullGCPtr<DOMException> create(JS::Object& global_object, FlyString const& name, FlyString const& message);
// JS constructor has message first, name second
// FIXME: This is a completely pointless footgun, let's use the same order for both factories.
static JS::NonnullGCPtr<DOMException> create_with_global_object(JS::Object& global_object, FlyString const& message, FlyString const& name);
static JS::NonnullGCPtr<DOMException> create(JS::Realm& realm, FlyString const& message);
virtual ~DOMException() override;
FlyString const& name() const { return m_name; }
FlyString const& message() const { return m_message; }
u16 code() const { return get_legacy_code_for_name(m_name); }
protected:
DOMException(HTML::Window&, FlyString const& name, FlyString const& message);
private:
FlyString m_name;
FlyString m_message;
};
#define __ENUMERATE(ErrorName) \
class ErrorName final { \
public: \
static JS::NonnullGCPtr<DOMException> create(JS::Object& global_object, FlyString const& message) \
{ \
return DOMException::create(global_object, #ErrorName, message); \
} \
};
ENUMERATE_DOM_EXCEPTION_ERROR_NAMES
#undef __ENUMERATE
}
namespace Web {
inline JS::Completion throw_completion(JS::NonnullGCPtr<WebIDL::DOMException> exception)
{
return JS::throw_completion(JS::Value(static_cast<JS::Object*>(exception.ptr())));
}
}

View file

@ -0,0 +1,35 @@
interface DOMException {
constructor(optional DOMString message = "", optional DOMString name = "Error");
readonly attribute DOMString name;
readonly attribute DOMString message;
readonly attribute unsigned short code;
const unsigned short INDEX_SIZE_ERR = 1;
const unsigned short DOMSTRING_SIZE_ERR = 2;
const unsigned short HIERARCHY_REQUEST_ERR = 3;
const unsigned short WRONG_DOCUMENT_ERR = 4;
const unsigned short INVALID_CHARACTER_ERR = 5;
const unsigned short NO_DATA_ALLOWED_ERR = 6;
const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7;
const unsigned short NOT_FOUND_ERR = 8;
const unsigned short NOT_SUPPORTED_ERR = 9;
const unsigned short INUSE_ATTRIBUTE_ERR = 10;
const unsigned short INVALID_STATE_ERR = 11;
const unsigned short SYNTAX_ERR = 12;
const unsigned short INVALID_MODIFICATION_ERR = 13;
const unsigned short NAMESPACE_ERR = 14;
const unsigned short INVALID_ACCESS_ERR = 15;
const unsigned short VALIDATION_ERR = 16;
const unsigned short TYPE_MISMATCH_ERR = 17;
const unsigned short SECURITY_ERR = 18;
const unsigned short NETWORK_ERR = 19;
const unsigned short ABORT_ERR = 20;
const unsigned short URL_MISMATCH_ERR = 21;
const unsigned short QUOTA_EXCEEDED_ERR = 22;
const unsigned short TIMEOUT_ERR = 23;
const unsigned short INVALID_NODE_TYPE_ERR = 24;
const unsigned short DATA_CLONE_ERR = 25;
};

View file

@ -9,7 +9,7 @@
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/RefPtr.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/WebIDL/DOMException.h>
namespace Web::WebIDL {
@ -49,7 +49,7 @@ public:
{
}
ExceptionOr(JS::NonnullGCPtr<DOM::DOMException> exception)
ExceptionOr(JS::NonnullGCPtr<DOMException> exception)
: m_exception(move(exception))
{
}
@ -59,8 +59,8 @@ public:
{
}
ExceptionOr(Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> exception)
: m_exception(move(exception).template downcast<Empty, SimpleException, JS::NonnullGCPtr<DOM::DOMException>>())
ExceptionOr(Variant<SimpleException, JS::NonnullGCPtr<DOMException>> exception)
: m_exception(move(exception).template downcast<Empty, SimpleException, JS::NonnullGCPtr<DOMException>>())
{
}
@ -78,9 +78,9 @@ public:
return m_result.release_value();
}
Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> exception() const
Variant<SimpleException, JS::NonnullGCPtr<DOMException>> exception() const
{
return m_exception.template downcast<SimpleException, JS::NonnullGCPtr<DOM::DOMException>>();
return m_exception.template downcast<SimpleException, JS::NonnullGCPtr<DOMException>>();
}
bool is_exception() const
@ -90,13 +90,13 @@ public:
// These are for compatibility with the TRY() macro in AK.
[[nodiscard]] bool is_error() const { return is_exception(); }
Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> release_error() { return exception(); }
Variant<SimpleException, JS::NonnullGCPtr<DOMException>> release_error() { return exception(); }
private:
Optional<ValueType> m_result;
// https://webidl.spec.whatwg.org/#idl-exceptions
Variant<Empty, SimpleException, JS::NonnullGCPtr<DOM::DOMException>> m_exception {};
Variant<Empty, SimpleException, JS::NonnullGCPtr<DOMException>> m_exception {};
};
template<>