LibWeb/IDL: Add support for returning nullable sequence types

This commit is contained in:
Luke Wilde 2022-06-04 03:58:30 +01:00 committed by Linus Groh
parent 633ac53c0c
commit 85c617fb1c
Notes: sideshowbarker 2024-07-17 14:33:07 +09:00

View file

@ -1319,6 +1319,12 @@ static void generate_wrap_statement(SourceGenerator& generator, String const& va
if (@value@.is_null()) {
@result_expression@ JS::js_null();
} else {
)~~~");
} else if (type.name == "sequence") {
scoped_generator.append(R"~~~(
if (!@value@.has_value()) {
@result_expression@ JS::js_null();
} else {
)~~~");
} else {
scoped_generator.append(R"~~~(
@ -1339,10 +1345,20 @@ static void generate_wrap_statement(SourceGenerator& generator, String const& va
scoped_generator.append(R"~~~(
auto* new_array@recursion_depth@ = MUST(JS::Array::create(global_object, 0));
)~~~");
if (!type.nullable) {
scoped_generator.append(R"~~~(
for (size_t i@recursion_depth@ = 0; i@recursion_depth@ < @value@.size(); ++i@recursion_depth@) {
auto& element@recursion_depth@ = @value@.at(i@recursion_depth@);
)~~~");
} else {
scoped_generator.append(R"~~~(
auto& @value@_non_optional = @value@.value();
for (size_t i@recursion_depth@ = 0; i@recursion_depth@ < @value@_non_optional.size(); ++i@recursion_depth@) {
auto& element@recursion_depth@ = @value@_non_optional.at(i@recursion_depth@);
)~~~");
}
generate_wrap_statement(scoped_generator, String::formatted("element{}", recursion_depth), sequence_generic_type.parameters.first(), interface, String::formatted("auto wrapped_element{} =", recursion_depth), WrappingReference::Yes, recursion_depth + 1);