Set.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 Set* create(Realm&);
  16. virtual ~Set() override = default;
  17. // NOTE: Unlike what the spec says, we implement Sets using an underlying map,
  18. // so all the functions below do not directly implement the operations as
  19. // defined by the specification.
  20. void set_clear() { m_values.map_clear(); }
  21. bool set_remove(Value const& value) { return m_values.map_remove(value); }
  22. bool set_has(Value const& key) const { return m_values.map_has(key); }
  23. void set_add(Value const& key) { m_values.map_set(key, js_undefined()); }
  24. size_t set_size() const { return m_values.map_size(); }
  25. auto begin() const { return m_values.begin(); }
  26. auto begin() { return m_values.begin(); }
  27. auto end() const { return m_values.end(); }
  28. private:
  29. explicit Set(Object& prototype);
  30. virtual void visit_edges(Visitor& visitor) override;
  31. Map m_values;
  32. };
  33. }