/src/serenity/Userland/Libraries/LibJS/Runtime/Set.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibJS/Runtime/GlobalObject.h> |
10 | | #include <LibJS/Runtime/Map.h> |
11 | | #include <LibJS/Runtime/Object.h> |
12 | | #include <LibJS/Runtime/Value.h> |
13 | | |
14 | | namespace JS { |
15 | | |
16 | | class Set : public Object { |
17 | | JS_OBJECT(Set, Object); |
18 | | JS_DECLARE_ALLOCATOR(Set); |
19 | | |
20 | | public: |
21 | | static NonnullGCPtr<Set> create(Realm&); |
22 | | |
23 | | virtual void initialize(Realm&) override; |
24 | | virtual ~Set() override = default; |
25 | | |
26 | | // NOTE: Unlike what the spec says, we implement Sets using an underlying map, |
27 | | // so all the functions below do not directly implement the operations as |
28 | | // defined by the specification. |
29 | | |
30 | 0 | void set_clear() { m_values->map_clear(); } |
31 | 0 | bool set_remove(Value const& value) { return m_values->map_remove(value); } |
32 | 0 | bool set_has(Value const& key) const { return m_values->map_has(key); } |
33 | 0 | void set_add(Value const& key) { m_values->map_set(key, js_undefined()); } |
34 | 0 | size_t set_size() const { return m_values->map_size(); } |
35 | | |
36 | 0 | auto begin() const { return const_cast<Map const&>(*m_values).begin(); } |
37 | 0 | auto begin() { return m_values->begin(); } |
38 | 0 | auto end() const { return m_values->end(); } |
39 | | |
40 | | NonnullGCPtr<Set> copy() const; |
41 | | |
42 | | private: |
43 | | explicit Set(Object& prototype); |
44 | | |
45 | | virtual void visit_edges(Visitor& visitor) override; |
46 | | |
47 | | GCPtr<Map> m_values; |
48 | | }; |
49 | | |
50 | | // 24.2.1.1 Set Records, https://tc39.es/ecma262/#sec-set-records |
51 | | struct SetRecord { |
52 | | NonnullGCPtr<Object const> set_object; // [[SetObject]] |
53 | | double size { 0 }; // [[Size] |
54 | | NonnullGCPtr<FunctionObject> has; // [[Has]] |
55 | | NonnullGCPtr<FunctionObject> keys; // [[Keys]] |
56 | | }; |
57 | | |
58 | | ThrowCompletionOr<SetRecord> get_set_record(VM&, Value); |
59 | | bool set_data_has(NonnullGCPtr<Set>, Value); |
60 | | |
61 | | } |