PrivateEnvironment.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2021, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/StringView.h>
  9. #include <AK/Vector.h>
  10. #include <LibJS/Heap/Cell.h>
  11. #include <LibJS/Heap/CellAllocator.h>
  12. namespace JS {
  13. struct PrivateName {
  14. PrivateName() = default;
  15. PrivateName(u64 unique_id, DeprecatedFlyString description)
  16. : unique_id(unique_id)
  17. , description(move(description))
  18. {
  19. }
  20. u64 unique_id { 0 };
  21. DeprecatedFlyString description;
  22. bool operator==(PrivateName const& rhs) const;
  23. };
  24. class PrivateEnvironment : public Cell {
  25. JS_CELL(PrivateEnvironment, Cell);
  26. JS_DECLARE_ALLOCATOR(PrivateEnvironment);
  27. public:
  28. PrivateName resolve_private_identifier(DeprecatedFlyString const& identifier) const;
  29. void add_private_name(Badge<ClassExpression>, DeprecatedFlyString description);
  30. private:
  31. explicit PrivateEnvironment(PrivateEnvironment* parent);
  32. virtual void visit_edges(Visitor&) override;
  33. auto find_private_name(DeprecatedFlyString const& description) const
  34. {
  35. return m_private_names.find_if([&](PrivateName const& private_name) {
  36. return private_name.description == description;
  37. });
  38. }
  39. static u64 s_next_id;
  40. GCPtr<PrivateEnvironment> m_outer_environment; // [[OuterEnv]]
  41. Vector<PrivateName> m_private_names; // [[Names]]
  42. u64 m_unique_id;
  43. };
  44. }