From 3845982d3821322fcb0fde1c1bfa1e28ec1c97ff Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sat, 2 Jul 2022 17:07:41 +0100 Subject: [PATCH] LibWeb/IDL: Add support for optional sequences --- .../LibWeb/WrapperGenerator/IDLGenerators.cpp | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp index 994f9a06224..ab54ab21e3f 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/IDLGenerators.cpp @@ -707,6 +707,41 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter // 3. If method is undefined, throw a TypeError. // 4. Return the result of creating a sequence from V and method. + if (optional) { + auto sequence_cpp_type = idl_type_name_to_cpp_type(parameterized_type.parameters.first(), interface); + sequence_generator.set("sequence.type", sequence_cpp_type.name); + sequence_generator.set("sequence.storage_type", sequence_storage_type_to_cpp_storage_type_name(sequence_cpp_type.sequence_storage_type)); + + if (!optional_default_value.has_value()) { + if (sequence_cpp_type.sequence_storage_type == IDL::SequenceStorageType::Vector) { + sequence_generator.append(R"~~~( + Optional<@sequence.storage_type@<@sequence.type@>> @cpp_name@; +)~~~"); + } else { + sequence_generator.append(R"~~~( + Optional<@sequence.storage_type@> @cpp_name@; +)~~~"); + } + } else { + if (optional_default_value != "[]") + TODO(); + + if (sequence_cpp_type.sequence_storage_type == IDL::SequenceStorageType::Vector) { + sequence_generator.append(R"~~~( + @sequence.storage_type@<@sequence.type@> @cpp_name@; +)~~~"); + } else { + sequence_generator.append(R"~~~( + @sequence.storage_type@ @cpp_name@ { global_object.heap() }; +)~~~"); + } + } + + sequence_generator.append(R"~~~( + if (!@js_name@@js_suffix@.is_undefined()) { +)~~~"); + } + sequence_generator.append(R"~~~( if (!@js_name@@js_suffix@.is_object()) return vm.throw_completion(global_object, JS::ErrorType::NotAnObject, @js_name@@js_suffix@.to_string_without_side_effects()); @@ -716,7 +751,14 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter return vm.throw_completion(global_object, JS::ErrorType::NotIterable, @js_name@@js_suffix@.to_string_without_side_effects()); )~~~"); - parameterized_type.generate_sequence_from_iterable(sequence_generator, acceptable_cpp_name, String::formatted("{}{}", js_name, js_suffix), String::formatted("iterator_method{}", recursion_depth), interface, recursion_depth + 1); + parameterized_type.generate_sequence_from_iterable(sequence_generator, String::formatted("{}{}", acceptable_cpp_name, optional ? "_non_optional" : ""), String::formatted("{}{}", js_name, js_suffix), String::formatted("iterator_method{}", recursion_depth), interface, recursion_depth + 1); + + if (optional) { + sequence_generator.append(R"~~~( + @cpp_name@ = move(@cpp_name@_non_optional); + } +)~~~"); + } } else if (parameter.type->name == "record") { // https://webidl.spec.whatwg.org/#es-record