LibWeb/Bindings: Support returning nullable types in dictionaries

We were previously assuming that dictionary members were always
required when being returned.

This is a bit of a weird case, because unlike _input_ dictionaries
which the spec marks as required, 'result' dictionaries do not seem to
be marked in spec IDL as required. This is still fine from the POV that
the spec is written as it states that we should only be putting the
values into the dictionary if the value exists.

We could do this through some metaprogramming constexpr type checks.
For example, if the type in our C++ representation was not an
Optional, we can skip the has_value check.

Instead of doing that, change the IDL of the result dictionaries to
annotate these members so that the IDL generator knows this
information up front. While all current cases have every single
member returned or not returned, it is conceivable that the spec
could have a situation that one member is always returned (and
should get marked as required), while the others are optionally
returned. Therefore, this new GenerateAsRequired attribute is
applied for each individual member.
This commit is contained in:
Shannon Booth 2025-01-14 00:08:27 +13:00 committed by Tim Ledbetter
commit f3ec727555
Notes: github-actions[bot] 2025-02-10 17:06:24 +00:00
6 changed files with 37 additions and 30 deletions

View file

@ -42,20 +42,20 @@ dictionary URLPatternOptions {
// https://urlpattern.spec.whatwg.org/#dictdef-urlpatternresult
dictionary URLPatternResult {
sequence<URLPatternInput> inputs;
[GenerateAsRequired] sequence<URLPatternInput> inputs;
URLPatternComponentResult protocol;
URLPatternComponentResult username;
URLPatternComponentResult password;
URLPatternComponentResult hostname;
URLPatternComponentResult port;
URLPatternComponentResult pathname;
URLPatternComponentResult search;
URLPatternComponentResult hash;
[GenerateAsRequired] URLPatternComponentResult protocol;
[GenerateAsRequired] URLPatternComponentResult username;
[GenerateAsRequired] URLPatternComponentResult password;
[GenerateAsRequired] URLPatternComponentResult hostname;
[GenerateAsRequired] URLPatternComponentResult port;
[GenerateAsRequired] URLPatternComponentResult pathname;
[GenerateAsRequired] URLPatternComponentResult search;
[GenerateAsRequired] URLPatternComponentResult hash;
};
// https://urlpattern.spec.whatwg.org/#dictdef-urlpatterncomponentresult
dictionary URLPatternComponentResult {
USVString input;
record<USVString, (USVString or undefined)> groups;
[GenerateAsRequired] USVString input;
[GenerateAsRequired] record<USVString, (USVString or undefined)> groups;
};