IDLGenerators: Translate boolean? silent = null to Optional<bool>

It was prevously translating it to just `bool` instead of
`Optional<bool>`.

Required elements in dictionary (such as in Notification.idl
for the NotificationAction) were treated as Optional<T> like the other
non-required elements. Now it's just the type without the Optional.

Co-authored-by: Zaggy1024 <gregory.bertilson@gmail.com>
This commit is contained in:
Niccolo Antonelli Dziri
2025-10-06 22:42:24 +02:00
committed by Sam Atkins
parent 7d60d0bfb7
commit 94ca0dcbef
Notes: github-actions[bot] 2026-02-13 16:49:24 +00:00

View File

@@ -580,7 +580,7 @@ static void generate_from_integral(SourceGenerator& scoped_generator, IDL::Type
}
template<typename ParameterType>
static void generate_to_integral(SourceGenerator& scoped_generator, ParameterType const& parameter, bool optional, Optional<ByteString> const& optional_default_value)
static void generate_to_integral(SourceGenerator& scoped_generator, ParameterType const& parameter, bool optional, Optional<ByteString> optional_default_value)
{
struct TypeMap {
StringView idl_type;
@@ -607,7 +607,10 @@ static void generate_to_integral(SourceGenerator& scoped_generator, ParameterTyp
scoped_generator.set("enforce_range", parameter.extended_attributes.contains("EnforceRange") ? "Yes" : "No");
scoped_generator.set("clamp", parameter.extended_attributes.contains("Clamp") ? "Yes" : "No");
if ((!optional && !parameter.type->is_nullable()) || (optional_default_value.has_value() && optional_default_value != "null"sv)) {
if (optional_default_value.has_value() && optional_default_value.value() == "null"sv)
optional_default_value = {};
if ((!optional && !parameter.type->is_nullable()) || optional_default_value.has_value()) {
scoped_generator.append(R"~~~(
@cpp_type@ @cpp_name@;
)~~~");
@@ -637,9 +640,7 @@ static void generate_to_integral(SourceGenerator& scoped_generator, ParameterTyp
)~~~");
}
// FIXME: The Optional<foo> defaults to empty, and can't be explicitly set to `null`.
// So, just skip the assignment.
if (optional_default_value.has_value() && optional_default_value != "null"sv) {
if (optional_default_value.has_value()) {
scoped_generator.append(R"~~~(
else
@cpp_name@ = static_cast<@cpp_type@>(@parameter.optional_default_value@);
@@ -2216,7 +2217,7 @@ static void generate_wrap_statement(SourceGenerator& generator, ByteString const
// the generated code to do some metaprogramming to inspect the type of the member in the C++ struct to
// determine whether the type is present or not (e.g through a has_value() on an Optional<T>, or a null
// check on a GC::Ptr<T>). So to save some complexity in the generator, give ourselves a hint of what to do.
bool is_optional = !member.extended_attributes.contains("GenerateAsRequired") && !member.default_value.has_value();
bool is_optional = !member.required && !member.extended_attributes.contains("GenerateAsRequired") && !member.default_value.has_value();
if (is_optional) {
dictionary_generator.append(R"~~~(
Optional<JS::Value> @wrapped_value_name@;