LibWeb/IDB: Implement IDBCursorWithValue interface

This commit is contained in:
stelar7 2025-05-13 11:04:55 +02:00 committed by Andrew Kaster
commit 296d9d74d8
Notes: github-actions[bot] 2025-05-13 16:49:47 +00:00
10 changed files with 74 additions and 4 deletions

View file

@ -624,6 +624,7 @@ class Performance;
namespace Web::IndexedDB {
class Database;
class IDBCursor;
class IDBCursorWithValue;
class IDBDatabase;
class IDBFactory;
class IDBIndex;

View file

@ -8,6 +8,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBCursor.h>
#include <LibWeb/IndexedDB/IDBCursorWithValue.h>
#include <LibWeb/IndexedDB/Internal/Algorithms.h>
namespace Web::IndexedDB {
@ -18,11 +19,11 @@ IDBCursor::~IDBCursor() = default;
IDBCursor::IDBCursor(JS::Realm& realm, CursorSourceHandle source_handle, GC::Ptr<Key> position, Bindings::IDBCursorDirection direction, GotValue got_value, GC::Ptr<Key> key, JS::Value value, GC::Ref<IDBKeyRange> range, KeyOnly key_only)
: PlatformObject(realm)
, m_value(value)
, m_position(position)
, m_direction(direction)
, m_got_value(got_value == GotValue::Yes)
, m_key(key)
, m_value(value)
, m_source_handle(source_handle)
, m_range(range)
, m_key_only(key_only == KeyOnly::Yes)
@ -31,6 +32,10 @@ IDBCursor::IDBCursor(JS::Realm& realm, CursorSourceHandle source_handle, GC::Ptr
GC::Ref<IDBCursor> IDBCursor::create(JS::Realm& realm, CursorSourceHandle source_handle, GC::Ptr<Key> position, Bindings::IDBCursorDirection direction, GotValue got_value, GC::Ptr<Key> key, JS::Value value, GC::Ref<IDBKeyRange> range, KeyOnly key_only)
{
// A cursor that has its key only flag set to false implements the IDBCursorWithValue interface as well.
if (key_only == KeyOnly::No)
return realm.create<IDBCursorWithValue>(realm, source_handle, position, direction, got_value, key, value, range, key_only);
return realm.create<IDBCursor>(realm, source_handle, position, direction, got_value, key, value, range, key_only);
}
@ -54,6 +59,26 @@ void IDBCursor::visit_edges(Visitor& visitor)
});
}
GC_DEFINE_ALLOCATOR(IDBCursorWithValue);
IDBCursorWithValue::~IDBCursorWithValue() = default;
IDBCursorWithValue::IDBCursorWithValue(JS::Realm& realm, CursorSourceHandle source_handle, GC::Ptr<Key> position, Bindings::IDBCursorDirection direction, GotValue got_value, GC::Ptr<Key> key, JS::Value value, GC::Ref<IDBKeyRange> range, KeyOnly key_only)
: IDBCursor(realm, source_handle, position, direction, got_value, key, value, range, key_only)
{
}
void IDBCursorWithValue::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(IDBCursorWithValue);
Base::initialize(realm);
}
void IDBCursorWithValue::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
}
// https://w3c.github.io/IndexedDB/#cursor-transaction
GC::Ref<IDBTransaction> IDBCursor::transaction()
{

View file

@ -8,6 +8,7 @@
#include <LibGC/Heap.h>
#include <LibWeb/Bindings/IDBCursorPrototype.h>
#include <LibWeb/Bindings/IDBCursorWithValuePrototype.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/IndexedDB/IDBIndex.h>
#include <LibWeb/IndexedDB/IDBKeyRange.h>
@ -52,7 +53,6 @@ public:
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> update(JS::Value);
WebIDL::ExceptionOr<GC::Ref<IDBRequest>> delete_();
[[nodiscard]] JS::Value value() { return m_value.value_or(JS::js_undefined()); }
[[nodiscard]] GC::Ref<IDBKeyRange> range() { return m_range; }
[[nodiscard]] GC::Ptr<Key> position() { return m_position; }
[[nodiscard]] GC::Ptr<Key> object_store_position() { return m_object_store_position; }
@ -75,6 +75,9 @@ protected:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor& visitor) override;
// A cursor has a value which represent the value of the last iterated record.
Optional<JS::Value> m_value;
private:
// A cursor has a position within its range.
GC::Ptr<Key> m_position;
@ -88,9 +91,8 @@ private:
// A cursor has a got value flag.
bool m_got_value { false };
// A cursor has a key and a value which represent the key and the value of the last iterated record.
// A cursor has a key which represent the key the last iterated record.
GC::Ptr<Key> m_key;
Optional<JS::Value> m_value;
// A cursor has a source handle, which is the index handle or the object store handle that opened the cursor.
CursorSourceHandle m_source_handle;
@ -104,4 +106,5 @@ private:
// A cursor also has a key only flag, that indicates whether the cursors value is exposed via the API.
bool m_key_only { false };
};
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2025, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/IndexedDB/IDBCursor.h>
namespace Web::IndexedDB {
// https://w3c.github.io/IndexedDB/#idbcursorwithvalue
class IDBCursorWithValue : public IDBCursor {
WEB_PLATFORM_OBJECT(IDBCursorWithValue, IDBCursor);
GC_DECLARE_ALLOCATOR(IDBCursorWithValue);
public:
virtual ~IDBCursorWithValue() override;
// https://w3c.github.io/IndexedDB/#dom-idbcursorwithvalue-value
[[nodiscard]] JS::Value value() { return m_value.value_or(JS::js_undefined()); }
private:
explicit IDBCursorWithValue(JS::Realm&, CursorSourceHandle, GC::Ptr<Key>, Bindings::IDBCursorDirection, GotValue, GC::Ptr<Key>, JS::Value, GC::Ref<IDBKeyRange>, KeyOnly);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor& visitor) override;
};
}

View file

@ -0,0 +1,6 @@
#import <IndexedDB/IDBCursor.idl>
[Exposed=(Window,Worker)]
interface IDBCursorWithValue : IDBCursor {
readonly attribute any value;
};

View file

@ -9,6 +9,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBCursor.h>
#include <LibWeb/IndexedDB/IDBCursorWithValue.h>
#include <LibWeb/IndexedDB/IDBIndex.h>
namespace Web::IndexedDB {

View file

@ -10,6 +10,7 @@
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/IndexedDB/IDBCursor.h>
#include <LibWeb/IndexedDB/IDBCursorWithValue.h>
#include <LibWeb/IndexedDB/IDBIndex.h>
#include <LibWeb/IndexedDB/IDBObjectStore.h>

View file

@ -260,6 +260,7 @@ libweb_js_bindings(HTML/WorkletGlobalScope)
libweb_js_bindings(HTML/XMLSerializer)
libweb_js_bindings(HighResolutionTime/Performance)
libweb_js_bindings(IndexedDB/IDBCursor)
libweb_js_bindings(IndexedDB/IDBCursorWithValue)
libweb_js_bindings(IndexedDB/IDBDatabase)
libweb_js_bindings(IndexedDB/IDBFactory)
libweb_js_bindings(IndexedDB/IDBIndex)

View file

@ -66,6 +66,7 @@ static bool is_platform_object(Type const& type)
"FormData"sv,
"HTMLCollection"sv,
"IDBCursor"sv,
"IDBCursorWithValue"sv,
"IDBIndex"sv,
"IDBKeyRange"sv,
"IDBObjectStore"sv,

View file

@ -213,6 +213,7 @@ HashChangeEvent
Headers
History
IDBCursor
IDBCursorWithValue
IDBDatabase
IDBFactory
IDBIndex