2021-02-20 13:06:03 +01:00
|
|
|
/*
|
2023-03-03 18:04:58 +00:00
|
|
|
* Copyright (c) 2021-2023, the SerenityOS developers.
|
2021-02-20 13:06:03 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-20 13:06:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-13 23:48:25 +04:30
|
|
|
#include <AK/Optional.h>
|
2021-02-20 13:06:03 +01:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-02-20 13:06:03 +01:00
|
|
|
|
|
|
|
namespace Web::Bindings {
|
|
|
|
|
|
|
|
template<typename>
|
2021-04-10 18:29:06 +04:30
|
|
|
constexpr bool IsExceptionOr = false;
|
2021-02-20 13:06:03 +01:00
|
|
|
|
|
|
|
template<typename T>
|
2022-09-25 17:03:42 +01:00
|
|
|
constexpr bool IsExceptionOr<WebIDL::ExceptionOr<T>> = true;
|
2021-02-20 13:06:03 +01:00
|
|
|
|
2022-03-09 14:31:51 +01:00
|
|
|
template<typename>
|
|
|
|
constexpr bool IsThrowCompletionOr = false;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
constexpr bool IsThrowCompletionOr<JS::ThrowCompletionOr<T>> = true;
|
|
|
|
|
2021-04-13 23:48:25 +04:30
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ExtractExceptionOrValueType {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2022-09-25 17:03:42 +01:00
|
|
|
struct ExtractExceptionOrValueType<WebIDL::ExceptionOr<T>> {
|
2021-04-13 23:48:25 +04:30
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
2022-03-09 14:31:51 +01:00
|
|
|
template<typename T>
|
|
|
|
struct ExtractExceptionOrValueType<JS::ThrowCompletionOr<T>> {
|
|
|
|
using Type = T;
|
|
|
|
};
|
|
|
|
|
2021-04-13 23:48:25 +04:30
|
|
|
template<>
|
|
|
|
struct ExtractExceptionOrValueType<void> {
|
|
|
|
using Type = JS::Value;
|
|
|
|
};
|
|
|
|
|
2021-06-27 00:17:13 +04:30
|
|
|
template<>
|
2022-09-25 17:03:42 +01:00
|
|
|
struct ExtractExceptionOrValueType<WebIDL::ExceptionOr<Empty>> {
|
2021-06-27 00:17:13 +04:30
|
|
|
using Type = JS::Value;
|
|
|
|
};
|
|
|
|
|
2021-04-13 23:48:25 +04:30
|
|
|
template<>
|
2022-09-25 17:03:42 +01:00
|
|
|
struct ExtractExceptionOrValueType<WebIDL::ExceptionOr<void>> {
|
2021-04-13 23:48:25 +04:30
|
|
|
using Type = JS::Value;
|
|
|
|
};
|
|
|
|
|
2023-06-15 18:48:01 +12:00
|
|
|
}
|
|
|
|
|
2024-11-04 14:37:27 +01:00
|
|
|
ALWAYS_INLINE JS::Completion exception_to_throw_completion(JS::VM& vm, auto&& exception)
|
2021-10-31 13:07:52 -04:00
|
|
|
{
|
|
|
|
return exception.visit(
|
2022-09-25 17:03:42 +01:00
|
|
|
[&](WebIDL::SimpleException const& exception) {
|
2023-03-03 18:04:58 +00:00
|
|
|
auto message = exception.message.visit([](auto const& s) -> StringView { return s; });
|
2021-10-31 13:07:52 -04:00
|
|
|
switch (exception.type) {
|
2022-09-25 17:03:42 +01:00
|
|
|
#define E(x) \
|
|
|
|
case WebIDL::SimpleExceptionType::x: \
|
2023-03-03 18:04:58 +00:00
|
|
|
return vm.template throw_completion<JS::x>(message);
|
2021-10-31 13:07:52 -04:00
|
|
|
|
|
|
|
ENUMERATE_SIMPLE_WEBIDL_EXCEPTION_TYPES(E)
|
|
|
|
|
|
|
|
#undef E
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
},
|
2024-11-15 04:01:23 +13:00
|
|
|
[&](GC::Ref<WebIDL::DOMException> const& exception) {
|
2022-09-04 16:56:15 +02:00
|
|
|
return throw_completion(exception);
|
2022-09-25 17:52:27 +01:00
|
|
|
},
|
|
|
|
[&](JS::Completion const& completion) {
|
|
|
|
return completion;
|
2021-10-31 13:07:52 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-13 23:48:25 +04:30
|
|
|
template<typename T>
|
|
|
|
using ExtractExceptionOrValueType = typename Detail::ExtractExceptionOrValueType<T>::Type;
|
|
|
|
|
|
|
|
// Return type depends on the return type of 'fn' (when invoked with no args):
|
2021-10-31 13:07:52 -04:00
|
|
|
// void or ExceptionOr<void>: JS::ThrowCompletionOr<JS::Value>, always returns JS::js_undefined()
|
|
|
|
// ExceptionOr<T>: JS::ThrowCompletionOr<T>
|
|
|
|
// T: JS::ThrowCompletionOr<T>
|
2022-03-09 14:31:51 +01:00
|
|
|
template<typename F, typename T = decltype(declval<F>()()), typename Ret = Conditional<!IsExceptionOr<T> && !IsVoid<T> && !IsThrowCompletionOr<T>, T, ExtractExceptionOrValueType<T>>>
|
2022-10-27 23:23:03 +01:00
|
|
|
JS::ThrowCompletionOr<Ret> throw_dom_exception_if_needed(JS::VM& vm, F&& fn)
|
2021-02-20 13:06:03 +01:00
|
|
|
{
|
2021-04-10 18:29:06 +04:30
|
|
|
if constexpr (IsExceptionOr<T>) {
|
2021-02-20 13:06:03 +01:00
|
|
|
auto&& result = fn();
|
2021-10-31 13:07:52 -04:00
|
|
|
|
|
|
|
if (result.is_exception())
|
2024-11-04 14:37:27 +01:00
|
|
|
return exception_to_throw_completion(vm, result.exception());
|
2021-10-31 13:07:52 -04:00
|
|
|
|
2021-02-20 13:06:03 +01:00
|
|
|
if constexpr (requires(T v) { v.value(); })
|
|
|
|
return result.value();
|
2021-04-13 23:48:25 +04:30
|
|
|
else
|
|
|
|
return JS::js_undefined();
|
2021-04-10 18:29:06 +04:30
|
|
|
} else if constexpr (IsVoid<T>) {
|
2021-02-20 13:06:03 +01:00
|
|
|
fn();
|
|
|
|
return JS::js_undefined();
|
|
|
|
} else {
|
|
|
|
return fn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|