/* * Copyright (c) 2024, stelar7 * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include namespace Web::IndexedDB { // https://w3c.github.io/IndexedDB/#key-construct 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>>; // A key has an associated type which is one of: number, date, string, binary, or array. enum KeyType { Number, Date, String, Binary, Array, }; public: [[nodiscard]] static GC::Ref create(JS::Realm&, KeyType, KeyValue); virtual ~Key(); [[nodiscard]] KeyType type() { return m_type; } [[nodiscard]] KeyValue value() { return m_value; } [[nodiscard]] double value_as_double() { return m_value.get(); } [[nodiscard]] AK::String value_as_string() { return m_value.get(); } [[nodiscard]] ByteBuffer value_as_byte_buffer() { return m_value.get(); } [[nodiscard]] Vector> value_as_vector() { return m_value.get>>(); } [[nodiscard]] Vector> subkeys() { VERIFY(m_type == Array); return value_as_vector(); } [[nodiscard]] static GC::Ref create_number(JS::Realm& realm, double value) { return create(realm, Number, value); } [[nodiscard]] static GC::Ref create_date(JS::Realm& realm, double value) { return create(realm, Date, value); } [[nodiscard]] static GC::Ref create_string(JS::Realm& realm, AK::String const& value) { return create(realm, String, value); } [[nodiscard]] static GC::Ref create_binary(JS::Realm& realm, ByteBuffer const& value) { return create(realm, Binary, value); } [[nodiscard]] static GC::Ref create_array(JS::Realm& realm, Vector> const& value) { return create(realm, Array, value); } [[nodiscard]] static i8 compare_two_keys(GC::Ref a, GC::Ref b); [[nodiscard]] static bool equals(GC::Ref a, GC::Ref b) { return compare_two_keys(a, b) == 0; } [[nodiscard]] static bool less_than(GC::Ref a, GC::Ref b) { return compare_two_keys(a, b) < 0; } [[nodiscard]] static bool greater_than(GC::Ref a, GC::Ref b) { return compare_two_keys(a, b) > 0; } private: Key(KeyType type, KeyValue value) : m_type(type) , m_value(value) { } KeyType m_type; KeyValue m_value; }; }