LibWeb/IDB: Sort IDBObjectStore according to the IDL

This commit is contained in:
stelar7 2025-05-08 23:55:30 +02:00 committed by Shannon Booth
parent 7250aa0b6b
commit a11efe3139
Notes: github-actions[bot] 2025-05-12 20:28:54 +00:00
2 changed files with 65 additions and 44 deletions

View file

@ -50,21 +50,11 @@ void IDBObjectStore::visit_edges(Visitor& visitor)
visitor.visit(m_indexes);
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-keypath
JS::Value IDBObjectStore::key_path() const
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-name
String IDBObjectStore::name() const
{
if (!m_store->key_path().has_value())
return JS::js_null();
return m_store->key_path().value().visit(
[&](String const& value) -> JS::Value {
return JS::PrimitiveString::create(realm().vm(), value);
},
[&](Vector<String> const& value) -> JS::Value {
return JS::Array::create_from<String>(realm(), value.span(), [&](auto const& entry) -> JS::Value {
return JS::PrimitiveString::create(realm().vm(), entry);
});
});
// The name getter steps are to return thiss name.
return m_name;
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-name
@ -108,6 +98,49 @@ WebIDL::ExceptionOr<void> IDBObjectStore::set_name(String const& value)
return {};
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-keypath
JS::Value IDBObjectStore::key_path() const
{
if (!m_store->key_path().has_value())
return JS::js_null();
return m_store->key_path().value().visit(
[&](String const& value) -> JS::Value {
return JS::PrimitiveString::create(realm().vm(), value);
},
[&](Vector<String> const& value) -> JS::Value {
return JS::Array::create_from<String>(realm(), value.span(), [&](auto const& entry) -> JS::Value {
return JS::PrimitiveString::create(realm().vm(), entry);
});
});
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-indexnames
GC::Ref<HTML::DOMStringList> IDBObjectStore::index_names()
{
// 1. Let names be a list of the names of the indexes in this's index set.
Vector<String> names;
for (auto const& [name, index] : m_indexes)
names.append(name);
// 2. Return the result (a DOMStringList) of creating a sorted name list with names.
return create_a_sorted_name_list(realm(), names);
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-transaction
GC::Ref<IDBTransaction> IDBObjectStore::transaction() const
{
// The transaction getter steps are to return thiss transaction.
return m_transaction;
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-autoincrement
bool IDBObjectStore::auto_increment() const
{
// The autoIncrement getter steps are to return true if thiss object store has a key generator, and false otherwise.
return m_store->uses_a_key_generator();
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-createindex
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::create_index(String const& name, KeyPath key_path, IDBIndexParameters options)
{
@ -158,18 +191,6 @@ WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::create_index(String const
return IDBIndex::create(realm, index, *this);
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-indexnames
GC::Ref<HTML::DOMStringList> IDBObjectStore::index_names()
{
// 1. Let names be a list of the names of the indexes in this's index set.
Vector<String> names;
for (auto const& [name, index] : m_indexes)
names.append(name);
// 2. Return the result (a DOMStringList) of creating a sorted name list with names.
return create_a_sorted_name_list(realm(), names);
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-index
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> IDBObjectStore::index(String const& name)
{

View file

@ -31,33 +31,33 @@ public:
virtual ~IDBObjectStore() override;
[[nodiscard]] static GC::Ref<IDBObjectStore> create(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>);
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-autoincrement
// The autoIncrement getter steps are to return true if thiss object store has a key generator, and false otherwise.
bool auto_increment() const { return m_store->uses_a_key_generator(); }
JS::Value key_path() const;
String name() const { return m_name; }
String name() const;
WebIDL::ExceptionOr<void> set_name(String const& value);
GC::Ref<IDBTransaction> transaction() const { return m_transaction; }
GC::Ref<ObjectStore> store() const { return m_store; }
AK::HashMap<String, GC::Ref<Index>>& index_set() { return m_indexes; }
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> create_index(String const&, KeyPath, IDBIndexParameters options);
JS::Value key_path() const;
[[nodiscard]] GC::Ref<HTML::DOMStringList> index_names();
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> index(String const&);
WebIDL::ExceptionOr<void> delete_index(String const&);
GC::Ref<IDBTransaction> transaction() const;
bool auto_increment() const;
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> add_or_put(GC::Ref<IDBObjectStore>, JS::Value, Optional<JS::Value> const&, bool);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> add(JS::Value value, Optional<JS::Value> const& key);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> put(JS::Value value, Optional<JS::Value> const& key);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> count(Optional<JS::Value>);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> add(JS::Value value, Optional<JS::Value> const& key);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> delete_(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> clear();
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_key(JS::Value);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_all(Optional<JS::Value>, Optional<WebIDL::UnsignedLong>);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_key_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> get_all_keys(Optional<JS::Value>, Optional<WebIDL::UnsignedLong>);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> count(Optional<JS::Value>);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<IDBRequest>> open_key_cursor(JS::Value, Bindings::IDBCursorDirection = Bindings::IDBCursorDirection::Next);
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> index(String const&);
WebIDL::ExceptionOr<GC::Ref<IDBIndex>> create_index(String const&, KeyPath, IDBIndexParameters options);
WebIDL::ExceptionOr<void> delete_index(String const&);
AK::HashMap<String, GC::Ref<Index>>& index_set() { return m_indexes; }
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> add_or_put(GC::Ref<IDBObjectStore>, JS::Value, Optional<JS::Value> const&, bool);
GC::Ref<ObjectStore> store() const { return m_store; }
protected:
explicit IDBObjectStore(JS::Realm&, GC::Ref<ObjectStore>, GC::Ref<IDBTransaction>);