1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:17:44 +00:00

AK: Convert AK::Format formatting helpers to returning ErrorOr<void>

This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
This commit is contained in:
Andreas Kling 2021-11-16 01:15:21 +01:00
parent 008355c222
commit 216e21a1fa
87 changed files with 450 additions and 448 deletions

View file

@ -28,7 +28,7 @@ private:
template<>
struct AK::Formatter<JS::Bytecode::Label> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, JS::Bytecode::Label const& value)
ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Label const& value)
{
return AK::Formatter<FormatString>::format(builder, "@{}", value.block().name());
}

View file

@ -42,7 +42,7 @@ private:
template<>
struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, JS::Bytecode::Register const& value)
ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value)
{
if (value.index() == JS::Bytecode::Register::accumulator_index)
return AK::Formatter<FormatString>::format(builder, "acc");

View file

@ -75,11 +75,11 @@ private:
template<>
struct AK::Formatter<JS::Cell> : AK::Formatter<FormatString> {
void format(FormatBuilder& builder, const JS::Cell* cell)
ErrorOr<void> format(FormatBuilder& builder, JS::Cell const* cell)
{
if (!cell)
Formatter<FormatString>::format(builder, "Cell{nullptr}");
return Formatter<FormatString>::format(builder, "Cell{nullptr}");
else
Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
return Formatter<FormatString>::format(builder, "{}({})", cell->class_name(), cell);
}
};

View file

@ -75,13 +75,13 @@ namespace AK {
template<>
struct Formatter<JS::PropertyAttributes> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyAttributes const& property_attributes)
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyAttributes const& property_attributes)
{
Vector<String> parts;
parts.append(String::formatted("[[Writable]]: {}", property_attributes.is_writable()));
parts.append(String::formatted("[[Enumerable]]: {}", property_attributes.is_enumerable()));
parts.append(String::formatted("[[Configurable]]: {}", property_attributes.is_configurable()));
Formatter<StringView>::format(builder, String::formatted("PropertyAttributes {{ {} }}", String::join(", ", parts)));
return Formatter<StringView>::format(builder, String::formatted("PropertyAttributes {{ {} }}", String::join(", ", parts)));
}
};

View file

@ -47,7 +47,7 @@ namespace AK {
template<>
struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyDescriptor const& property_descriptor)
{
Vector<String> parts;
if (property_descriptor.value.has_value())
@ -62,7 +62,7 @@ struct Formatter<JS::PropertyDescriptor> : Formatter<StringView> {
parts.append(String::formatted("[[Enumerable]]: {}", *property_descriptor.enumerable));
if (property_descriptor.configurable.has_value())
parts.append(String::formatted("[[Configurable]]: {}", *property_descriptor.configurable));
Formatter<StringView>::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts)));
return Formatter<StringView>::format(builder, String::formatted("PropertyDescriptor {{ {} }}", String::join(", ", parts)));
}
};

View file

@ -225,14 +225,13 @@ struct Traits<JS::PropertyKey> : public GenericTraits<JS::PropertyKey> {
template<>
struct Formatter<JS::PropertyKey> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyKey const& property_name)
ErrorOr<void> format(FormatBuilder& builder, JS::PropertyKey const& property_name)
{
if (!property_name.is_valid())
Formatter<StringView>::format(builder, "<invalid PropertyKey>");
else if (property_name.is_number())
Formatter<StringView>::format(builder, String::number(property_name.as_number()));
else
Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
return Formatter<StringView>::format(builder, "<invalid PropertyKey>");
if (property_name.is_number())
return Formatter<StringView>::format(builder, String::number(property_name.as_number()));
return Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
}
};

View file

@ -458,9 +458,9 @@ namespace AK {
template<>
struct Formatter<JS::Value> : Formatter<StringView> {
void format(FormatBuilder& builder, const JS::Value& value)
ErrorOr<void> format(FormatBuilder& builder, JS::Value value)
{
Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
return Formatter<StringView>::format(builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
}
};