From 637f35c0eb7e607ba3306be8a72925f10657ebce Mon Sep 17 00:00:00 2001 From: stelar7 Date: Thu, 8 May 2025 10:04:43 +0200 Subject: [PATCH] LibWeb/IDB: Implement clear_an_object_store --- .../LibWeb/IndexedDB/Internal/Algorithms.cpp | 15 +++++++++++++++ Libraries/LibWeb/IndexedDB/Internal/Algorithms.h | 1 + Libraries/LibWeb/IndexedDB/Internal/Index.cpp | 5 +++++ Libraries/LibWeb/IndexedDB/Internal/Index.h | 1 + .../LibWeb/IndexedDB/Internal/ObjectStore.cpp | 5 +++++ Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h | 1 + 6 files changed, 28 insertions(+) diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index 9bfb97cd01c..486d07dcfbd 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -1840,4 +1840,19 @@ GC::Ptr iterate_a_cursor(JS::Realm& realm, GC::Ref cursor, return cursor; } +// https://w3c.github.io/IndexedDB/#clear-an-object-store +JS::Value clear_an_object_store(GC::Ref store) +{ + // 1. Remove all records from store. + store->clear_records(); + + // 2. In all indexes which reference store, remove all records. + for (auto const& [name, index] : store->index_set()) { + index->clear_records(); + } + + // 3. Return undefined. + return JS::js_undefined(); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index 54fb23e5385..76fe5828d39 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -45,5 +45,6 @@ WebIDL::ExceptionOr> convert_a_value_to_a_key_range(JS::Rea JS::Value count_the_records_in_a_range(GC::Ref, GC::Ref); WebIDL::ExceptionOr retrieve_a_value_from_an_object_store(JS::Realm&, GC::Ref, GC::Ref); GC::Ptr iterate_a_cursor(JS::Realm&, GC::Ref, GC::Ptr = nullptr, GC::Ptr = nullptr, u64 = 1); +JS::Value clear_an_object_store(GC::Ref); } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Index.cpp b/Libraries/LibWeb/IndexedDB/Internal/Index.cpp index 04becff6d3a..b71fc0f5a3c 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Index.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Index.cpp @@ -71,4 +71,9 @@ HTML::SerializationRecord Index::referenced_value(IndexRecord const& index_recor .value; } +void Index::clear_records() +{ + m_records.clear(); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Index.h b/Libraries/LibWeb/IndexedDB/Internal/Index.h index 83903e89eb9..bad62d0cd47 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Index.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Index.h @@ -40,6 +40,7 @@ public: [[nodiscard]] KeyPath const& key_path() const { return m_key_path; } [[nodiscard]] bool has_record_with_key(GC::Ref key); + void clear_records(); HTML::SerializationRecord referenced_value(IndexRecord const& index_record) const; diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp index d11e82a7890..d2d114f9b2a 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp @@ -84,4 +84,9 @@ Optional ObjectStore::first_in_range(GC::Ref range) }); } +void ObjectStore::clear_records() +{ + m_records.clear(); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h index 1222d5c3886..3f8ad31da91 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h @@ -55,6 +55,7 @@ public: void store_a_record(Record const& record); u64 count_records_in_range(GC::Ref range); Optional first_in_range(GC::Ref range); + void clear_records(); protected: virtual void visit_edges(Visitor&) override;