Set.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Map.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibJS/Runtime/Value.h>
  11. namespace JS {
  12. class Set : public Object {
  13. JS_OBJECT(Set, Object);
  14. public:
  15. static NonnullGCPtr<Set> create(Realm&);
  16. virtual void initialize(Realm&) override;
  17. virtual ~Set() override = default;
  18. // NOTE: Unlike what the spec says, we implement Sets using an underlying map,
  19. // so all the functions below do not directly implement the operations as
  20. // defined by the specification.
  21. void set_clear() { m_values->map_clear(); }
  22. bool set_remove(Value const& value) { return m_values->map_remove(value); }
  23. bool set_has(Value const& key) const { return m_values->map_has(key); }
  24. void set_add(Value const& key) { m_values->map_set(key, js_undefined()); }
  25. size_t set_size() const { return m_values->map_size(); }
  26. auto begin() const { return const_cast<Map const&>(*m_values).begin(); }
  27. auto begin() { return m_values->begin(); }
  28. auto end() const { return m_values->end(); }
  29. NonnullGCPtr<Set> copy() const;
  30. private:
  31. explicit Set(Object& prototype);
  32. virtual void visit_edges(Visitor& visitor) override;
  33. GCPtr<Map> m_values;
  34. };
  35. }