Browse Source

IDLGenerators: Support nullable dictionary members with default values

When wrapping dictionary members, generate_wrap_statement was called
with the pattern "auto {} = ...", where "..." was determined based on
the variable's type. However, in generate_wrap_statement, if a type is
nullable it generates an if statement, so this would end up generating
something along the lines of

    if (!retval.member.has_value()) {
    	auto wrapped_member0_value = JS::js_null();
    } else {
    	auto wrapped_member0_value = JS::Value(...);
    }

...which makes the declaration inaccessible. It now generates the same
code, but the "auto" declaration (now an explicit JS::Value declaration)
is outside of the if-statement.
Matthew Olsson 1 year ago
parent
commit
e03e710d1b

+ 5 - 1
Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

@@ -1770,7 +1770,11 @@ static void generate_wrap_statement(SourceGenerator& generator, DeprecatedString
 
                 auto wrapped_value_name = DeprecatedString::formatted("wrapped_{}", member_value_js_name);
                 dictionary_generator.set("wrapped_value_name", wrapped_value_name);
-                generate_wrap_statement(dictionary_generator, DeprecatedString::formatted("{}.{}", value, member.name.to_snakecase()), member.type, interface, DeprecatedString::formatted("auto {} =", wrapped_value_name), WrappingReference::No, recursion_depth + 1);
+
+                dictionary_generator.append(R"~~~(
+    JS::Value @wrapped_value_name@;
+)~~~");
+                generate_wrap_statement(dictionary_generator, DeprecatedString::formatted("{}.{}", value, member.name.to_snakecase()), member.type, interface, DeprecatedString::formatted("{} =", wrapped_value_name), WrappingReference::No, recursion_depth + 1);
 
                 dictionary_generator.append(R"~~~(
     MUST(dictionary_object@recursion_depth@->create_data_property("@member_key@", @wrapped_value_name@));