From de2cad02aa9f33cd51e166e3e1bfca5e86e7cdad Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 21 Apr 2024 13:03:55 +1200 Subject: [PATCH] LibWeb: Factor out a function to generate IDL enumerations The prototype header generation was getting a bit long. This is also a step towards generating code for IDL files only containing an enum definition without any interface. In that case we can't put the enum definitions alongside the prototype - there is no prototype to speak of. --- .../BindingsGenerator/IDLGenerators.cpp | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index d6051224c69..857f0a31839 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -2545,6 +2545,50 @@ JS::ThrowCompletionOr> @constructor_class@::constru } } +static void generate_enumerations(HashMap const& enumerations, StringBuilder& builder) +{ + SourceGenerator generator { builder }; + + for (auto const& it : enumerations) { + if (!it.value.is_original_definition) + continue; + auto enum_generator = generator.fork(); + enum_generator.set("enum.type.name", it.key); + enum_generator.append(R"~~~( +enum class @enum.type.name@ { +)~~~"); + for (auto const& entry : it.value.translated_cpp_names) { + enum_generator.set("enum.entry", entry.value); + enum_generator.append(R"~~~( + @enum.entry@, +)~~~"); + } + + enum_generator.append(R"~~~( +}; +)~~~"); + + enum_generator.append(R"~~~( +inline String idl_enum_to_string(@enum.type.name@ value) +{ + switch (value) { +)~~~"); + for (auto const& entry : it.value.translated_cpp_names) { + enum_generator.set("enum.entry", entry.value); + enum_generator.set("enum.string", entry.key); + enum_generator.append(R"~~~( + case @enum.type.name@::@enum.entry@: + return "@enum.string@"_string; +)~~~"); + } + enum_generator.append(R"~~~( + } + VERIFY_NOT_REACHED(); +} +)~~~"); + } +} + static void generate_prototype_or_global_mixin_declarations(IDL::Interface const& interface, StringBuilder& builder) { SourceGenerator generator { builder }; @@ -2602,44 +2646,7 @@ static void generate_prototype_or_global_mixin_declarations(IDL::Interface const )~~~"); - for (auto& it : interface.enumerations) { - if (!it.value.is_original_definition) - continue; - auto enum_generator = generator.fork(); - enum_generator.set("enum.type.name", it.key); - enum_generator.append(R"~~~( -enum class @enum.type.name@ { -)~~~"); - for (auto& entry : it.value.translated_cpp_names) { - enum_generator.set("enum.entry", entry.value); - enum_generator.append(R"~~~( - @enum.entry@, -)~~~"); - } - - enum_generator.append(R"~~~( -}; -)~~~"); - - enum_generator.append(R"~~~( -inline String idl_enum_to_string(@enum.type.name@ value) -{ - switch (value) { -)~~~"); - for (auto& entry : it.value.translated_cpp_names) { - enum_generator.set("enum.entry", entry.value); - enum_generator.set("enum.string", entry.key); - enum_generator.append(R"~~~( - case @enum.type.name@::@enum.entry@: - return "@enum.string@"_string; -)~~~"); - } - enum_generator.append(R"~~~( - } - VERIFY_NOT_REACHED(); -} -)~~~"); - } + generate_enumerations(interface.enumerations, builder); } // https://webidl.spec.whatwg.org/#create-an-inheritance-stack