LibWeb: Implement caching of reflected element array attributes
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

For attributes like Element.ariaControlsElements, which are a reflection
of FrozenArray<Element>, we must return the same JS::Array object every
time the attribute is invoked - until its contents have changed. This
patch implements caching of the reflected array in accordance with the
spec.
This commit is contained in:
Timothy Flynn 2025-04-25 13:47:08 -04:00 committed by Tim Flynn
commit ac1c2a956a
Notes: github-actions[bot] 2025-04-26 21:30:33 +00:00
7 changed files with 93 additions and 14 deletions

View file

@ -528,4 +528,25 @@ template JS::ThrowCompletionOr<UnsignedLong> convert_to_int(JS::VM& vm, JS::Valu
template JS::ThrowCompletionOr<LongLong> convert_to_int(JS::VM& vm, JS::Value, EnforceRange, Clamp);
template JS::ThrowCompletionOr<UnsignedLongLong> convert_to_int(JS::VM& vm, JS::Value, EnforceRange, Clamp);
// AD-HOC: For same-object caching purposes, this can be used to compare a cached JS array of DOM::Elements with another
// list. Either list can be null, in which case they are considered the same only if they are both null.
bool lists_contain_same_elements(GC::Ptr<JS::Array> array, Optional<GC::RootVector<GC::Ref<DOM::Element>>> const& elements)
{
if (!array || !elements.has_value())
return !array && !elements.has_value();
bool is_equivalent = array->indexed_properties().array_like_size() == elements->size();
for (size_t i = 0; is_equivalent && i < elements->size(); ++i) {
auto cached_value = array->get_without_side_effects(JS::PropertyKey { i });
auto const& cached_element = as<DOM::Element>(cached_value.as_object());
auto it = elements->find_if([&](auto const& element) { return element.ptr() == &cached_element; });
if (it == elements->end())
is_equivalent = false;
}
return is_equivalent;
}
}