| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |  * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/NonnullRefPtr.h>
 | 
					
						
							|  |  |  | #include <AK/Optional.h>
 | 
					
						
							|  |  |  | #include <AK/RefPtr.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/DOMException.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | namespace Web::WebIDL { | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  | #define ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E) \
 | 
					
						
							|  |  |  |     E(EvalError)                                   \ | 
					
						
							|  |  |  |     E(RangeError)                                  \ | 
					
						
							|  |  |  |     E(ReferenceError)                              \ | 
					
						
							|  |  |  |     E(TypeError)                                   \ | 
					
						
							|  |  |  |     E(URIError) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define E(x) x,
 | 
					
						
							|  |  |  | enum class SimpleExceptionType { | 
					
						
							|  |  |  |     ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E) | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | #undef E
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct SimpleException { | 
					
						
							|  |  |  |     SimpleExceptionType type; | 
					
						
							|  |  |  |     String message; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | template<typename ValueType> | 
					
						
							|  |  |  | class ExceptionOr { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-03-22 07:44:33 -04:00
										 |  |  |     ExceptionOr() requires(IsSame<ValueType, Empty>) | 
					
						
							|  |  |  |         : m_result(Empty {}) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     ExceptionOr(ValueType const& result) | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |         : m_result(result) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ExceptionOr(ValueType&& result) | 
					
						
							|  |  |  |         : m_result(move(result)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     ExceptionOr(JS::NonnullGCPtr<DOM::DOMException> exception) | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  |         : m_exception(move(exception)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ExceptionOr(SimpleException exception) | 
					
						
							|  |  |  |         : m_exception(move(exception)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     ExceptionOr(Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> exception) | 
					
						
							|  |  |  |         : m_exception(move(exception).template downcast<Empty, SimpleException, JS::NonnullGCPtr<DOM::DOMException>>()) | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ExceptionOr(ExceptionOr&& other) = default; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     ExceptionOr(ExceptionOr const& other) = default; | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     ~ExceptionOr() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  |     ValueType& value() requires(!IsSame<ValueType, Empty>) | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         return m_result.value(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 07:44:33 -04:00
										 |  |  |     ValueType release_value() | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         return m_result.release_value(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> exception() const | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |         return m_exception.template downcast<SimpleException, JS::NonnullGCPtr<DOM::DOMException>>(); | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     bool is_exception() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  |         return !m_exception.template has<Empty>(); | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 07:44:33 -04:00
										 |  |  |     // These are for compatibility with the TRY() macro in AK.
 | 
					
						
							|  |  |  |     [[nodiscard]] bool is_error() const { return is_exception(); } | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     Variant<SimpleException, JS::NonnullGCPtr<DOM::DOMException>> release_error() { return exception(); } | 
					
						
							| 
									
										
										
										
											2022-03-22 07:44:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | private: | 
					
						
							|  |  |  |     Optional<ValueType> m_result; | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 13:15:16 +01:00
										 |  |  |     // https://webidl.spec.whatwg.org/#idl-exceptions
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     Variant<Empty, SimpleException, JS::NonnullGCPtr<DOM::DOMException>> m_exception {}; | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  | class ExceptionOr<void> : public ExceptionOr<Empty> { | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-06-27 00:17:13 +04:30
										 |  |  |     using ExceptionOr<Empty>::ExceptionOr; | 
					
						
							| 
									
										
										
										
											2021-02-19 19:08:00 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |