Set.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 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. private:
  30. explicit Set(Object& prototype);
  31. virtual void visit_edges(Visitor& visitor) override;
  32. GCPtr<Map> m_values;
  33. };
  34. }