Everywhere: Use Optional<T>::ensure() where useful

No functional changes.
This commit is contained in:
Jelle Raaijmakers 2025-09-17 15:48:22 +02:00 committed by Tim Flynn
commit c31eff6a47
Notes: github-actions[bot] 2025-09-17 16:02:17 +00:00
14 changed files with 53 additions and 87 deletions

View file

@ -360,13 +360,11 @@ FLATTEN SignedBigInteger SignedBigInteger::negated_value() const
u32 SignedBigInteger::hash() const u32 SignedBigInteger::hash() const
{ {
if (m_hash.has_value()) return m_hash.ensure([&] {
return *m_hash; auto buffer = MUST(ByteBuffer::create_zeroed(byte_length()));
auto result = export_data(buffer);
auto buffer = MUST(ByteBuffer::create_zeroed(byte_length())); return string_hash(reinterpret_cast<char const*>(result.data()), result.size());
auto result = export_data(buffer); });
m_hash = string_hash(reinterpret_cast<char const*>(result.data()), result.size());
return *m_hash;
} }
bool SignedBigInteger::operator==(SignedBigInteger const& other) const bool SignedBigInteger::operator==(SignedBigInteger const& other) const

View file

@ -359,13 +359,11 @@ FLATTEN UnsignedBigInteger UnsignedBigInteger::lcm(UnsignedBigInteger const& oth
u32 UnsignedBigInteger::hash() const u32 UnsignedBigInteger::hash() const
{ {
if (m_hash.has_value()) return m_hash.ensure([&] {
return *m_hash; auto buffer = MUST(ByteBuffer::create_zeroed(byte_length()));
auto result = export_data(buffer);
auto buffer = MUST(ByteBuffer::create_zeroed(byte_length())); return string_hash(reinterpret_cast<char const*>(result.data()), result.size());
auto result = export_data(buffer); });
m_hash = string_hash(reinterpret_cast<char const*>(result.data()), result.size());
return *m_hash;
} }
bool UnsignedBigInteger::operator==(UnsignedBigInteger const& other) const bool UnsignedBigInteger::operator==(UnsignedBigInteger const& other) const

View file

@ -114,12 +114,11 @@ void TypefaceSkia::populate_glyph_page(GlyphPage& glyph_page, size_t page_index)
FlyString const& TypefaceSkia::family() const FlyString const& TypefaceSkia::family() const
{ {
if (!m_family.has_value()) { return m_family.ensure([&] {
SkString family_name; SkString family_name;
impl().skia_typeface->getFamilyName(&family_name); impl().skia_typeface->getFamilyName(&family_name);
m_family = FlyString::from_utf8_without_validation(ReadonlyBytes { family_name.c_str(), family_name.size() }); return FlyString::from_utf8_without_validation(ReadonlyBytes { family_name.c_str(), family_name.size() });
} });
return m_family.value();
} }
u16 TypefaceSkia::weight() const u16 TypefaceSkia::weight() const

View file

@ -38,9 +38,7 @@ Locale::Locale(Object& prototype)
Unicode::LocaleID const& Locale::locale_id() const Unicode::LocaleID const& Locale::locale_id() const
{ {
if (!m_cached_locale_id.has_value()) return m_cached_locale_id.ensure([&] { return Unicode::parse_unicode_locale_id(locale()); });
m_cached_locale_id = Unicode::parse_unicode_locale_id(locale());
return *m_cached_locale_id;
} }
// 15.5.5 GetLocaleVariants ( locale ), https://tc39.es/ecma402/#sec-getlocalevariants // 15.5.5 GetLocaleVariants ( locale ), https://tc39.es/ecma402/#sec-getlocalevariants

View file

@ -53,9 +53,7 @@ public:
Vector<Test::Suite>& ensure_suites() Vector<Test::Suite>& ensure_suites()
{ {
if (!m_suites.has_value()) return m_suites.ensure([] { return Vector<Suite> {}; });
m_suites = Vector<Suite> {};
return *m_suites;
} }
protected: protected:

View file

@ -315,41 +315,29 @@ static FDFlags fd_flags_of(struct stat const& buf);
Vector<AK::String> const& Implementation::arguments() const Vector<AK::String> const& Implementation::arguments() const
{ {
if (!cache.cached_arguments.has_value()) { return cache.cached_arguments.ensure([&] {
cache.cached_arguments.lazy_emplace([&] { if (provide_arguments)
if (provide_arguments) return provide_arguments();
return provide_arguments(); return Vector<AK::String> {};
return Vector<AK::String> {}; });
});
}
return *cache.cached_arguments;
} }
Vector<AK::String> const& Implementation::environment() const Vector<AK::String> const& Implementation::environment() const
{ {
if (!cache.cached_environment.has_value()) { return cache.cached_environment.ensure([&] {
cache.cached_environment.lazy_emplace([&] { if (provide_environment)
if (provide_environment) return provide_environment();
return provide_environment(); return Vector<AK::String> {};
return Vector<AK::String> {}; });
});
}
return *cache.cached_environment;
} }
Vector<Implementation::MappedPath> const& Implementation::preopened_directories() const Vector<Implementation::MappedPath> const& Implementation::preopened_directories() const
{ {
if (!cache.cached_preopened_directories.has_value()) { return cache.cached_preopened_directories.ensure([&] {
cache.cached_preopened_directories.lazy_emplace([&] { if (provide_preopened_directories)
if (provide_preopened_directories) return provide_preopened_directories();
return provide_preopened_directories(); return Vector<MappedPath> {};
return Vector<MappedPath> {}; });
});
}
return *cache.cached_preopened_directories;
} }
Implementation::Descriptor Implementation::map_fd(FD fd) Implementation::Descriptor Implementation::map_fd(FD fd)

View file

@ -72,7 +72,7 @@ void CSSRule::clear_caches()
m_cached_layer_name.clear(); m_cached_layer_name.clear();
} }
FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const FlyString CSSRule::parent_layer_internal_qualified_name_slow_case() const
{ {
Vector<FlyString> layer_names; Vector<FlyString> layer_names;
for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) { for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) {
@ -105,8 +105,7 @@ FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const
} }
} }
m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse())); return MUST(String::join('.', layer_names.in_reverse()));
return m_cached_layer_name.value();
} }
} }

View file

@ -70,12 +70,10 @@ protected:
[[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const [[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const
{ {
if (!m_cached_layer_name.has_value()) return m_cached_layer_name.ensure([&] { return parent_layer_internal_qualified_name_slow_case(); });
return parent_layer_internal_qualified_name_slow_case();
return m_cached_layer_name.value();
} }
[[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const; [[nodiscard]] FlyString parent_layer_internal_qualified_name_slow_case() const;
Type m_type; Type m_type;
GC::Ptr<CSSRule> m_parent_rule; GC::Ptr<CSSRule> m_parent_rule;

View file

@ -4053,9 +4053,7 @@ GC::Ptr<NamedNodeMap const> Element::attributes() const
FlyString const& Element::html_uppercased_qualified_name() const FlyString const& Element::html_uppercased_qualified_name() const
{ {
if (!m_html_uppercased_qualified_name.has_value()) return m_html_uppercased_qualified_name.ensure([&] { return make_html_uppercased_qualified_name(); });
m_html_uppercased_qualified_name = make_html_uppercased_qualified_name();
return m_html_uppercased_qualified_name.value();
} }
void Element::play_or_cancel_animations_after_display_property_change() void Element::play_or_cancel_animations_after_display_property_change()

View file

@ -213,9 +213,7 @@ void HTMLTextAreaElement::set_raw_value(Utf16String value)
Utf16String HTMLTextAreaElement::api_value() const Utf16String HTMLTextAreaElement::api_value() const
{ {
// The algorithm for obtaining the element's API value is to return the element's raw value, with newlines normalized. // The algorithm for obtaining the element's API value is to return the element's raw value, with newlines normalized.
if (!m_api_value.has_value()) return m_api_value.ensure([&] { return Infra::normalize_newlines(m_raw_value); });
m_api_value = Infra::normalize_newlines(m_raw_value);
return *m_api_value;
} }
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value

View file

@ -21,15 +21,14 @@ bool ListOfAvailableImages::Key::operator==(Key const& other) const
u32 ListOfAvailableImages::Key::hash() const u32 ListOfAvailableImages::Key::hash() const
{ {
if (!cached_hash.has_value()) { return cached_hash.ensure([&] {
u32 url_hash = url.hash(); u32 url_hash = url.hash();
u32 mode_hash = static_cast<u32>(mode); u32 mode_hash = static_cast<u32>(mode);
u32 origin_hash = 0; u32 origin_hash = 0;
if (origin.has_value()) if (origin.has_value())
origin_hash = Traits<URL::Origin>::hash(origin.value()); origin_hash = Traits<URL::Origin>::hash(origin.value());
cached_hash = pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash)); return pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash));
} });
return cached_hash.value();
} }
void ListOfAvailableImages::visit_edges(JS::Cell::Visitor& visitor) void ListOfAvailableImages::visit_edges(JS::Cell::Visitor& visitor)

View file

@ -45,11 +45,9 @@ void ImageBox::prepare_for_replaced_layout()
set_natural_height(0); set_natural_height(0);
} else { } else {
auto font = Platform::FontPlugin::the().default_font(12); auto font = Platform::FontPlugin::the().default_font(12);
CSSPixels alt_text_width = 0; CSSPixels alt_text_width = m_cached_alt_text_width.ensure([&] {
if (!m_cached_alt_text_width.has_value()) return CSSPixels::nearest_value_for(font->width(alt));
m_cached_alt_text_width = CSSPixels::nearest_value_for(font->width(alt)); });
alt_text_width = m_cached_alt_text_width.value();
set_natural_width(alt_text_width + 16); set_natural_width(alt_text_width + 16);
set_natural_height(CSSPixels::nearest_value_for(font->pixel_size()) + 16); set_natural_height(CSSPixels::nearest_value_for(font->pixel_size()) + 16);
} }

View file

@ -68,14 +68,12 @@ CSS::Display Paintable::display() const
PaintableBox* Paintable::containing_block() const PaintableBox* Paintable::containing_block() const
{ {
if (!m_containing_block.has_value()) { return m_containing_block.ensure([&] -> GC::Ptr<PaintableBox> {
auto containing_layout_box = m_layout_node->containing_block(); auto containing_layout_box = m_layout_node->containing_block();
if (containing_layout_box) if (!containing_layout_box)
m_containing_block = const_cast<PaintableBox*>(containing_layout_box->paintable_box()); return nullptr;
else return const_cast<PaintableBox*>(containing_layout_box->paintable_box());
m_containing_block = nullptr; });
}
return *m_containing_block;
} }
CSS::ImmutableComputedValues const& Paintable::computed_values() const CSS::ImmutableComputedValues const& Paintable::computed_values() const

View file

@ -24,13 +24,12 @@ public:
CSSPixelPoint cumulative_offset() const CSSPixelPoint cumulative_offset() const
{ {
if (!m_cached_cumulative_offset.has_value()) { return m_cached_cumulative_offset.ensure([&] {
m_cached_cumulative_offset = m_own_offset; auto offset = m_own_offset;
if (m_parent) { if (m_parent)
m_cached_cumulative_offset.value() += m_parent->cumulative_offset(); offset += m_parent->cumulative_offset();
} return offset;
} });
return m_cached_cumulative_offset.value();
} }
CSSPixelPoint own_offset() const { return m_own_offset; } CSSPixelPoint own_offset() const { return m_own_offset; }