Set.cpp 997 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. NonnullGCPtr<Set> Set::create(Realm& realm)
  9. {
  10. return realm.heap().allocate<Set>(realm, realm.intrinsics().set_prototype());
  11. }
  12. Set::Set(Object& prototype)
  13. : Object(ConstructWithPrototypeTag::Tag, prototype)
  14. {
  15. }
  16. void Set::initialize(Realm& realm)
  17. {
  18. m_values = Map::create(realm);
  19. }
  20. NonnullGCPtr<Set> Set::copy() const
  21. {
  22. auto& vm = this->vm();
  23. auto& realm = *vm.current_realm();
  24. // FIXME: This is very inefficient, but there's no better way to do this at the moment, as the underlying Map
  25. // implementation of m_values uses a non-copyable RedBlackTree.
  26. auto result = Set::create(realm);
  27. for (auto const& entry : *this)
  28. result->set_add(entry.key);
  29. return *result;
  30. }
  31. void Set::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_values);
  35. }
  36. }