LibWeb: Generate code for throwing DOMExceptions based on ExceptionOr<T>

There's a little bit of template magic involved here to make it work,
but this seems alright. Very cool! :^)

Co-authored-by: AnotherTest <ali.mpfard@gmail.com>
This commit is contained in:
Linus Groh 2021-02-19 19:10:44 +01:00 committed by Andreas Kling
parent e708f1c3a2
commit 3da2b51d74
Notes: sideshowbarker 2024-07-18 22:08:22 +09:00

View file

@ -1091,6 +1091,7 @@ void generate_prototype_implementation(const IDL::Interface& interface)
generator.append(R"~~~(
#include <AK/Function.h>
#include <AK/StdLibExtras.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Function.h>
@ -1100,6 +1101,7 @@ void generate_prototype_implementation(const IDL::Interface& interface)
#include <LibWeb/Bindings/@wrapper_class@.h>
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
#include <LibWeb/Bindings/CommentWrapper.h>
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
#include <LibWeb/Bindings/DOMImplementationWrapper.h>
#include <LibWeb/Bindings/DocumentFragmentWrapper.h>
#include <LibWeb/Bindings/DocumentTypeWrapper.h>
@ -1115,6 +1117,7 @@ void generate_prototype_implementation(const IDL::Interface& interface)
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/EventListener.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/HTMLElement.h>
@ -1407,6 +1410,55 @@ JS_DEFINE_NATIVE_SETTER(@prototype_class@::@attribute.setter_callback@)
function_generator.set("function.name:snakecase", snake_name(function.name));
function_generator.append(R"~~~(
#ifndef EXCEPTION_OR_TEMPLATES
#define EXCEPTION_OR_TEMPLATES
template<typename>
struct IsExceptionOr : AK::FalseType {
};
template<typename T>
struct IsExceptionOr<ExceptionOr<T>> : AK::TrueType {
};
template<typename T>
ALWAYS_INLINE bool throw_dom_exception(JS::VM& vm, JS::GlobalObject& global_object, ExceptionOr<T>& result)
{
if (result.is_exception()) {
vm.throw_exception(global_object, DOMExceptionWrapper::create(global_object, const_cast<DOM::DOMException&>(result.exception())));
return true;
}
return false;
}
template<typename F, typename T = decltype(declval<F>()()), typename Ret = typename Conditional<!IsExceptionOr<T>::value && !IsVoid<T>::value, T, JS::Value>::Type>
Ret throw_dom_exception_if_needed(auto&& vm, auto&& global_object, F&& fn) {
if constexpr (IsExceptionOr<T>::value) {
auto&& result = fn();
if (throw_dom_exception(vm, global_object, result))
return JS::Value();
if constexpr (requires(T v) { v.value(); })
return result.value();
return JS::Value();
} else if constexpr (IsVoid<T>::value) {
fn();
return JS::js_undefined();
} else {
return fn();
}
}
template <typename T>
bool should_return_empty(T&& value)
{
if constexpr (IsSame<JS::Value, T>::value)
return value.is_empty();
return false;
}
#endif
JS_DEFINE_NATIVE_FUNCTION(@prototype_class@::@function.name:snakecase@)
{
auto* impl = impl_from(vm, global_object);
@ -1420,15 +1472,11 @@ JS_DEFINE_NATIVE_FUNCTION(@prototype_class@::@function.name:snakecase@)
generate_arguments(generator, function.parameters, arguments_builder);
function_generator.set(".arguments", arguments_builder.string_view());
if (function.return_type.name != "undefined") {
function_generator.append(R"~~~(
auto retval = impl->@function.name:snakecase@(@.arguments@);
function_generator.append(R"~~~(
auto retval = throw_dom_exception_if_needed(vm, global_object, [&] { return impl->@function.name:snakecase@(@.arguments@); });
if (should_return_empty(retval))
return JS::Value();
)~~~");
} else {
function_generator.append(R"~~~(
impl->@function.name:snakecase@(@.arguments@);
)~~~");
}
generate_return_statement(function.return_type);