LibWeb: Support nullable attributes in generated toJSON()

Any optional or nullable attribute will end up in an `if/else` branch
when we collect the attribute values, and this is inherently
incompatibly with an `auto {attr}_wrapped = ...` expression. Define the
variable as an `JS::Value` before generating the wrap statement so we
can properly support `toJSON()` for an attribute like:

  readonly attribute double? altitude;
This commit is contained in:
Jelle Raaijmakers 2025-06-19 13:44:49 +02:00 committed by Jelle Raaijmakers
commit 8f3fe0c39e
Notes: github-actions[bot] 2025-06-21 08:01:47 +00:00

View file

@ -3161,7 +3161,10 @@ static void collect_attribute_values_of_an_inheritance_stack(SourceGenerator& fu
)~~~");
}
generate_wrap_statement(attribute_generator, return_value_name, attribute.type, interface_in_chain, ByteString::formatted("auto {}_wrapped =", return_value_name));
attribute_generator.append(R"~~~(
JS::Value @attribute.return_value_name@_wrapped;
)~~~");
generate_wrap_statement(attribute_generator, return_value_name, attribute.type, interface_in_chain, ByteString::formatted("{}_wrapped =", return_value_name));
attribute_generator.append(R"~~~(
MUST(result->create_data_property("@attribute.name@"_fly_string, @attribute.return_value_name@_wrapped));