Browse Source

LibWeb: Allow returning JS::ThrowCompletionOr<T> from wrapped functions

In some cases, we need more nuance than what DOM::ExceptionOr<T> offers.
Andreas Kling 3 years ago
parent
commit
ba87f23f22
1 changed files with 12 additions and 1 deletions
  1. 12 1
      Userland/Libraries/LibWeb/Bindings/ExceptionOrUtils.h

+ 12 - 1
Userland/Libraries/LibWeb/Bindings/ExceptionOrUtils.h

@@ -20,6 +20,12 @@ constexpr bool IsExceptionOr = false;
 template<typename T>
 constexpr bool IsExceptionOr<DOM::ExceptionOr<T>> = true;
 
+template<typename>
+constexpr bool IsThrowCompletionOr = false;
+
+template<typename T>
+constexpr bool IsThrowCompletionOr<JS::ThrowCompletionOr<T>> = true;
+
 namespace Detail {
 
 template<typename T>
@@ -32,6 +38,11 @@ struct ExtractExceptionOrValueType<DOM::ExceptionOr<T>> {
     using Type = T;
 };
 
+template<typename T>
+struct ExtractExceptionOrValueType<JS::ThrowCompletionOr<T>> {
+    using Type = T;
+};
+
 template<>
 struct ExtractExceptionOrValueType<void> {
     using Type = JS::Value;
@@ -79,7 +90,7 @@ using ExtractExceptionOrValueType = typename Detail::ExtractExceptionOrValueType
 // void or ExceptionOr<void>: JS::ThrowCompletionOr<JS::Value>, always returns JS::js_undefined()
 // ExceptionOr<T>: JS::ThrowCompletionOr<T>
 // T: JS::ThrowCompletionOr<T>
-template<typename F, typename T = decltype(declval<F>()()), typename Ret = Conditional<!IsExceptionOr<T> && !IsVoid<T>, T, ExtractExceptionOrValueType<T>>>
+template<typename F, typename T = decltype(declval<F>()()), typename Ret = Conditional<!IsExceptionOr<T> && !IsVoid<T> && !IsThrowCompletionOr<T>, T, ExtractExceptionOrValueType<T>>>
 JS::ThrowCompletionOr<Ret> throw_dom_exception_if_needed(auto&& global_object, F&& fn)
 {
     if constexpr (IsExceptionOr<T>) {