Skip to content

Truly independent web browser

LibJS: Shift the remaining indices when taking a sparse array slot

sideshowbarker221bead6e74c
diff --git a/Libraries/LibJS/Rust/src/runtime/indexed_properties.rs b/Libraries/LibJS/Rust/src/runtime/indexed_properties.rs
index 92a413564d4..3fd21ec0734 100644
--- a/Libraries/LibJS/Rust/src/runtime/indexed_properties.rs
+++ b/Libraries/LibJS/Rust/src/runtime/indexed_properties.rs
@@ -82,14 +82,13 @@ impl GenericIndexedPropertyStorage {
assert!(self.array_size > 0);
self.array_size -= 1;
- let first_index = *self
+ let first = self.sparse_elements.remove(&0).unwrap_or_default();
+ self.sparse_elements = self
.sparse_elements
- .keys()
- .min()
- .expect("an array with a size has an element");
- self.sparse_elements
- .remove(&first_index)
- .expect("the first index is in the map")
+ .drain()
+ .map(|(index, element)| (index - 1, element))
+ .collect();
+ first
}
pub fn take_last(&mut self) -> ValueAndAttributes {
diff --git a/Tests/LibJS/TestVMThreads.cpp b/Tests/LibJS/TestVMThreads.cpp
index d8cd2912977..43ec7e1bcf6 100644
--- a/Tests/LibJS/TestVMThreads.cpp
+++ b/Tests/LibJS/TestVMThreads.cpp
@@ -718,3 +718,24 @@ TEST_CASE(dictionary_elements_distinguish_holes_from_undefined)
EXPECT(object->has_own_property(2u).release_value());
});
}
+
+TEST_CASE(taking_the_first_dictionary_element_shifts_the_remaining_indices)
+{
+ run_on_vm_threads(1, [] {
+ auto vm = JS::VM::create();
+ auto context = JS::create_simple_execution_context<JS::GlobalObject>(*vm);
+ auto object = JS::Object::create(*context->realm, nullptr);
+ object->define_direct_property(0u, JS::js_undefined(), 0);
+ object->define_direct_property(2u, JS::js_undefined(), 0);
+
+ object->indexed_take_first();
+ EXPECT_EQ(object->indexed_array_like_size(), 2u);
+ EXPECT(!object->has_own_property(0u).release_value());
+ EXPECT(object->has_own_property(1u).release_value());
+ EXPECT(!object->has_own_property(2u).release_value());
+
+ object->indexed_take_first();
+ EXPECT_EQ(object->indexed_array_like_size(), 1u);
+ EXPECT(object->has_own_property(0u).release_value());
+ });
+}