LibWeb: Implement optional function IDL arguments

This allows us to run some more view transitions WPT tests, one of
which has been imported.
This commit is contained in:
Psychpsyo 2025-09-09 16:02:49 +02:00 committed by Tim Flynn
commit fa3c45d0b4
Notes: github-actions[bot] 2025-09-10 13:51:13 +00:00
2 changed files with 7 additions and 3 deletions

View file

@ -6679,7 +6679,6 @@ void Document::set_onvisibilitychange(WebIDL::CallbackType* value)
}
// https://drafts.csswg.org/css-view-transitions-1/#dom-document-startviewtransition
// FIXME: Calling document.startViewTransition() without arguments throws TypeError instead of calling this.
GC::Ptr<ViewTransition::ViewTransition> Document::start_view_transition(ViewTransition::ViewTransitionUpdateCallback update_callback)
{
// The method steps for startViewTransition(updateCallback) are as follows:

View file

@ -1089,17 +1089,22 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
// 1. If the result of calling IsCallable(V) is false and the conversion to an IDL value is not being performed due to V being assigned to an attribute whose type is a nullable callback function that is annotated with [LegacyTreatNonObjectAsNull], then throw a TypeError.
if (!parameter.type->is_nullable() && !callback_function.is_legacy_treat_non_object_as_null) {
callback_function_generator.append(R"~~~(
if (!@js_name@@js_suffix@.is_function())
if (!@js_name@@js_suffix@.is_function()
)~~~");
if (optional)
callback_function_generator.append("&& !@js_name@@js_suffix@.is_undefined()");
callback_function_generator.append(R"~~~()
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, @js_name@@js_suffix@.to_string_without_side_effects());
)~~~");
}
// 2. Return the IDL callback function type value that represents a reference to the same object that V represents, with the incumbent realm as the callback context.
if (parameter.type->is_nullable() || callback_function.is_legacy_treat_non_object_as_null) {
if (optional || parameter.type->is_nullable() || callback_function.is_legacy_treat_non_object_as_null) {
callback_function_generator.append(R"~~~(
GC::Ptr<WebIDL::CallbackType> @cpp_name@;
if (@js_name@@js_suffix@.is_object())
@cpp_name@ = vm.heap().allocate<WebIDL::CallbackType>(@js_name@@js_suffix@.as_object(), HTML::incumbent_realm(), @operation_returns_promise@);
)~~~");
// FIXME: Handle default value for optional parameter here.
} else {
callback_function_generator.append(R"~~~(
auto @cpp_name@ = vm.heap().allocate<WebIDL::CallbackType>(@js_name@@js_suffix@.as_object(), HTML::incumbent_realm(), @operation_returns_promise@);