Reference.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Environment.h>
  8. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  9. #include <LibJS/Runtime/PropertyKey.h>
  10. #include <LibJS/Runtime/Value.h>
  11. namespace JS {
  12. Reference make_private_reference(VM&, Value base_value, DeprecatedFlyString const& private_identifier);
  13. class Reference {
  14. public:
  15. enum class BaseType : u8 {
  16. Unresolvable,
  17. Value,
  18. Environment,
  19. };
  20. Reference(BaseType type, PropertyKey name, bool strict)
  21. : m_base_type(type)
  22. , m_name(move(name))
  23. , m_strict(strict)
  24. {
  25. }
  26. Reference(Value base, PropertyKey name, Value this_value, bool strict = false)
  27. : m_base_type(BaseType::Value)
  28. , m_base_value(base)
  29. , m_name(move(name))
  30. , m_this_value(this_value)
  31. , m_strict(strict)
  32. {
  33. }
  34. Reference(Environment& base, DeprecatedFlyString referenced_name, bool strict = false, Optional<EnvironmentCoordinate> environment_coordinate = {})
  35. : m_base_type(BaseType::Environment)
  36. , m_base_environment(&base)
  37. , m_name(move(referenced_name))
  38. , m_strict(strict)
  39. , m_environment_coordinate(move(environment_coordinate))
  40. {
  41. }
  42. Reference(Value base, PrivateName name)
  43. : m_base_type(BaseType::Value)
  44. , m_base_value(base)
  45. , m_name(move(name))
  46. , m_strict(true)
  47. {
  48. }
  49. Value base() const
  50. {
  51. VERIFY(m_base_type == BaseType::Value);
  52. return m_base_value;
  53. }
  54. Environment& base_environment() const
  55. {
  56. VERIFY(m_base_type == BaseType::Environment);
  57. return *m_base_environment;
  58. }
  59. PropertyKey const& name() const { return m_name.get<PropertyKey>(); }
  60. PrivateName const& private_name() const { return m_name.get<PrivateName>(); }
  61. bool is_strict() const { return m_strict; }
  62. // 6.2.4.2 IsUnresolvableReference ( V ), https://tc39.es/ecma262/#sec-isunresolvablereference
  63. bool is_unresolvable() const { return m_base_type == BaseType::Unresolvable; }
  64. // 6.2.4.1 IsPropertyReference ( V ), https://tc39.es/ecma262/#sec-ispropertyreference
  65. bool is_property_reference() const
  66. {
  67. if (is_unresolvable())
  68. return false;
  69. if (m_base_type == BaseType::Environment)
  70. return false;
  71. return true;
  72. }
  73. // 6.2.4.7 GetThisValue ( V ), https://tc39.es/ecma262/#sec-getthisvalue
  74. Value get_this_value() const
  75. {
  76. VERIFY(is_property_reference());
  77. if (is_super_reference())
  78. return m_this_value;
  79. return m_base_value;
  80. }
  81. // 6.2.4.3 IsSuperReference ( V ), https://tc39.es/ecma262/#sec-issuperreference
  82. bool is_super_reference() const
  83. {
  84. return !m_this_value.is_empty();
  85. }
  86. // 6.2.4.4 IsPrivateReference ( V ), https://tc39.es/ecma262/#sec-isprivatereference
  87. bool is_private_reference() const
  88. {
  89. return m_name.has<PrivateName>();
  90. }
  91. // Note: Non-standard helper.
  92. bool is_environment_reference() const
  93. {
  94. return m_base_type == BaseType::Environment;
  95. }
  96. ThrowCompletionOr<void> initialize_referenced_binding(VM&, Value value, Environment::InitializeBindingHint hint = Environment::InitializeBindingHint::Normal) const;
  97. ThrowCompletionOr<void> put_value(VM&, Value);
  98. ThrowCompletionOr<Value> get_value(VM&) const;
  99. ThrowCompletionOr<bool> delete_(VM&);
  100. bool is_valid_reference() const { return true; }
  101. Optional<EnvironmentCoordinate> environment_coordinate() const { return m_environment_coordinate; }
  102. private:
  103. Completion throw_reference_error(VM&) const;
  104. BaseType m_base_type { BaseType::Unresolvable };
  105. union {
  106. Value m_base_value {};
  107. mutable Environment* m_base_environment;
  108. };
  109. Variant<PropertyKey, PrivateName> m_name;
  110. Value m_this_value;
  111. bool m_strict { false };
  112. Optional<EnvironmentCoordinate> m_environment_coordinate;
  113. };
  114. }