Set.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Set.h>
  7. namespace JS {
  8. JS_DEFINE_ALLOCATOR(Set);
  9. NonnullGCPtr<Set> Set::create(Realm& realm)
  10. {
  11. return realm.heap().allocate<Set>(realm, realm.intrinsics().set_prototype());
  12. }
  13. Set::Set(Object& prototype)
  14. : Object(ConstructWithPrototypeTag::Tag, prototype)
  15. {
  16. }
  17. void Set::initialize(Realm& realm)
  18. {
  19. m_values = Map::create(realm);
  20. }
  21. NonnullGCPtr<Set> Set::copy() const
  22. {
  23. auto& vm = this->vm();
  24. auto& realm = *vm.current_realm();
  25. // FIXME: This is very inefficient, but there's no better way to do this at the moment, as the underlying Map
  26. // implementation of m_values uses a non-copyable RedBlackTree.
  27. auto result = Set::create(realm);
  28. for (auto const& entry : *this)
  29. result->set_add(entry.key);
  30. return *result;
  31. }
  32. void Set::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_values);
  36. }
  37. }