2021-06-09 00:08:47 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2025-07-19 13:49:30 -07:00
|
|
|
#include <LibJS/Export.h>
|
2021-06-09 00:08:47 +03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2022-02-09 18:34:16 +03:30
|
|
|
#include <LibJS/Runtime/Map.h>
|
2021-06-09 00:08:47 +03:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
#include <LibJS/Runtime/Value.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2025-07-19 13:49:30 -07:00
|
|
|
class JS_API Set : public Object {
|
2021-06-09 00:08:47 +03:00
|
|
|
JS_OBJECT(Set, Object);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(Set);
|
2021-06-09 00:08:47 +03:00
|
|
|
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
static GC::Ref<Set> create(Realm&);
|
2021-06-09 00:08:47 +03:00
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
virtual void initialize(Realm&) override;
|
2022-03-14 10:25:06 -06:00
|
|
|
virtual ~Set() override = default;
|
2021-06-09 00:08:47 +03:00
|
|
|
|
2025-09-25 18:12:16 +02:00
|
|
|
virtual bool is_set_object() const final { return true; }
|
|
|
|
|
2022-02-09 18:34:16 +03:30
|
|
|
// NOTE: Unlike what the spec says, we implement Sets using an underlying map,
|
|
|
|
// so all the functions below do not directly implement the operations as
|
|
|
|
// defined by the specification.
|
|
|
|
|
2022-11-30 09:18:27 -05:00
|
|
|
void set_clear() { m_values->map_clear(); }
|
|
|
|
bool set_remove(Value const& value) { return m_values->map_remove(value); }
|
|
|
|
bool set_has(Value const& key) const { return m_values->map_has(key); }
|
|
|
|
void set_add(Value const& key) { m_values->map_set(key, js_undefined()); }
|
|
|
|
size_t set_size() const { return m_values->map_size(); }
|
2022-02-09 18:34:16 +03:30
|
|
|
|
2022-11-30 09:18:27 -05:00
|
|
|
auto begin() const { return const_cast<Map const&>(*m_values).begin(); }
|
|
|
|
auto begin() { return m_values->begin(); }
|
|
|
|
auto end() const { return m_values->end(); }
|
2021-06-09 00:08:47 +03:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Set> copy() const;
|
2022-12-01 23:04:30 +02:00
|
|
|
|
2021-06-09 00:08:47 +03:00
|
|
|
private:
|
2022-08-28 23:51:28 +02:00
|
|
|
explicit Set(Object& prototype);
|
|
|
|
|
2021-06-09 18:01:06 +03:00
|
|
|
virtual void visit_edges(Visitor& visitor) override;
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Map> m_values;
|
2021-06-09 00:08:47 +03:00
|
|
|
};
|
|
|
|
|
2024-07-09 17:18:09 -04:00
|
|
|
// 24.2.1.1 Set Records, https://tc39.es/ecma262/#sec-set-records
|
|
|
|
struct SetRecord {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<Object const> set_object; // [[SetObject]]
|
|
|
|
double size { 0 }; // [[Size]
|
|
|
|
GC::Ref<FunctionObject> has; // [[Has]]
|
|
|
|
GC::Ref<FunctionObject> keys; // [[Keys]]
|
2024-07-09 17:18:09 -04:00
|
|
|
};
|
|
|
|
|
2025-07-19 10:41:08 -07:00
|
|
|
ThrowCompletionOr<SetRecord> get_set_record(VM&, Value);
|
|
|
|
bool set_data_has(GC::Ref<Set>, Value);
|
2024-07-09 17:18:09 -04:00
|
|
|
|
2025-09-25 18:12:16 +02:00
|
|
|
template<>
|
|
|
|
inline bool Object::fast_is<Set>() const { return is_set_object(); }
|
|
|
|
|
2021-06-09 00:08:47 +03:00
|
|
|
}
|