LibWeb: Turn IDB internal Key into a GC type

This commit is contained in:
stelar7 2025-01-08 23:09:56 +01:00 committed by Jelle Raaijmakers
commit 47b8a015a7
Notes: github-actions[bot] 2025-01-14 22:47:44 +00:00
4 changed files with 52 additions and 30 deletions

View file

@ -10,11 +10,25 @@
#include <AK/String.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibGC/Ptr.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::IndexedDB {
// https://w3c.github.io/IndexedDB/#key-construct
class Key {
class Key : public JS::Cell {
GC_CELL(Key, JS::Cell);
GC_DECLARE_ALLOCATOR(Key);
// A key also has an associated value, which will be either:
// * an unrestricted double if type is number or date,
// * a DOMString if type is string,
// * a byte sequence if type is binary,
// * a list of other keys if type is array.
using KeyValue = Variant<double, AK::String, ByteBuffer, Vector<GC::Root<Key>>>;
// A key has an associated type which is one of: number, date, string, binary, or array.
enum KeyType {
Number,
@ -24,24 +38,23 @@ class Key {
Array,
};
// A key also has an associated value, which will be either:
// * an unrestricted double if type is number or date,
// * a DOMString if type is string,
// * a byte sequence if type is binary,
// * a list of other keys if type is array.
using KeyValue = Variant<double, AK::String, ByteBuffer, Vector<Key>>;
public:
[[nodiscard]] static GC::Ref<Key> create(JS::Realm&, KeyType, KeyValue);
virtual ~Key();
[[nodiscard]] KeyType type() { return m_type; }
[[nodiscard]] KeyValue value() { return m_value; }
[[nodiscard]] static Key create_number(double value) { return Key(Number, value); }
[[nodiscard]] static Key create_date(double value) { return Key(Date, value); }
[[nodiscard]] static Key create_string(AK::String const& value) { return Key(String, value); }
[[nodiscard]] static Key create_binary(ByteBuffer const& value) { return Key(Binary, value); }
[[nodiscard]] static Key create_array(Vector<Key> const& value) { return Key(Array, value); }
[[nodiscard]] static GC::Ref<Key> create_number(JS::Realm& realm, double value) { return create(realm, Number, value); }
[[nodiscard]] static GC::Ref<Key> create_date(JS::Realm& realm, double value) { return create(realm, Date, value); }
[[nodiscard]] static GC::Ref<Key> create_string(JS::Realm& realm, AK::String const& value) { return create(realm, String, value); }
[[nodiscard]] static GC::Ref<Key> create_binary(JS::Realm& realm, ByteBuffer const& value) { return create(realm, Binary, value); }
[[nodiscard]] static GC::Ref<Key> create_array(JS::Realm& realm, Vector<GC::Root<Key>> const& value) { return create(realm, Array, value); }
[[nodiscard]] static i8 compare_two_keys(Key a, Key b);
[[nodiscard]] static i8 compare_two_keys(GC::Ref<Key> a, GC::Ref<Key> b);
[[nodiscard]] static bool equals(GC::Ref<Key> a, GC::Ref<Key> b) { return compare_two_keys(a, b) == 0; }
[[nodiscard]] static bool less_than(GC::Ref<Key> a, GC::Ref<Key> b) { return compare_two_keys(a, b) < 0; }
[[nodiscard]] static bool greater_than(GC::Ref<Key> a, GC::Ref<Key> b) { return compare_two_keys(a, b) > 0; }
private:
Key(KeyType type, KeyValue value)