Reference.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.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() = default;
  21. Reference(BaseType type, PropertyKey name, bool strict)
  22. : m_base_type(type)
  23. , m_name(move(name))
  24. , m_strict(strict)
  25. {
  26. }
  27. Reference(Value base, PropertyKey name, Value this_value, bool strict = false)
  28. : m_base_type(BaseType::Value)
  29. , m_base_value(base)
  30. , m_name(move(name))
  31. , m_this_value(this_value)
  32. , m_strict(strict)
  33. {
  34. if (base.is_nullish()) {
  35. m_base_type = BaseType::Unresolvable;
  36. m_base_value = {};
  37. m_this_value = {};
  38. m_name = {};
  39. }
  40. }
  41. Reference(Environment& base, DeprecatedFlyString referenced_name, bool strict = false, Optional<EnvironmentCoordinate> environment_coordinate = {})
  42. : m_base_type(BaseType::Environment)
  43. , m_base_environment(&base)
  44. , m_name(move(referenced_name))
  45. , m_strict(strict)
  46. , m_environment_coordinate(move(environment_coordinate))
  47. {
  48. }
  49. Reference(Value base, PrivateName name)
  50. : m_base_type(BaseType::Value)
  51. , m_base_value(base)
  52. , m_this_value(Value {})
  53. , m_strict(true)
  54. , m_is_private(true)
  55. , m_private_name(move(name))
  56. {
  57. }
  58. Value base() const
  59. {
  60. VERIFY(m_base_type == BaseType::Value);
  61. return m_base_value;
  62. }
  63. Environment& base_environment() const
  64. {
  65. VERIFY(m_base_type == BaseType::Environment);
  66. return *m_base_environment;
  67. }
  68. PropertyKey const& name() const { return m_name; }
  69. bool is_strict() const { return m_strict; }
  70. // 6.2.4.2 IsUnresolvableReference ( V ), https://tc39.es/ecma262/#sec-isunresolvablereference
  71. bool is_unresolvable() const { return m_base_type == BaseType::Unresolvable; }
  72. // 6.2.4.1 IsPropertyReference ( V ), https://tc39.es/ecma262/#sec-ispropertyreference
  73. bool is_property_reference() const
  74. {
  75. if (is_unresolvable())
  76. return false;
  77. if (m_base_type == BaseType::Environment)
  78. return false;
  79. return true;
  80. }
  81. // 6.2.4.7 GetThisValue ( V ), https://tc39.es/ecma262/#sec-getthisvalue
  82. Value get_this_value() const
  83. {
  84. VERIFY(is_property_reference());
  85. if (is_super_reference())
  86. return m_this_value;
  87. return m_base_value;
  88. }
  89. // 6.2.4.3 IsSuperReference ( V ), https://tc39.es/ecma262/#sec-issuperreference
  90. bool is_super_reference() const
  91. {
  92. return !m_this_value.is_empty();
  93. }
  94. // 6.2.4.4 IsPrivateReference ( V ), https://tc39.es/ecma262/#sec-isprivatereference
  95. bool is_private_reference() const
  96. {
  97. return m_is_private;
  98. }
  99. // Note: Non-standard helper.
  100. bool is_environment_reference() const
  101. {
  102. return m_base_type == BaseType::Environment;
  103. }
  104. ThrowCompletionOr<void> initialize_referenced_binding(VM&, Value value, Environment::InitializeBindingHint hint = Environment::InitializeBindingHint::Normal) const;
  105. ThrowCompletionOr<void> put_value(VM&, Value);
  106. ThrowCompletionOr<Value> get_value(VM&) const;
  107. ThrowCompletionOr<bool> delete_(VM&);
  108. bool is_valid_reference() const { return m_name.is_valid() || m_is_private; }
  109. Optional<EnvironmentCoordinate> environment_coordinate() const { return m_environment_coordinate; }
  110. private:
  111. Completion throw_reference_error(VM&) const;
  112. BaseType m_base_type { BaseType::Unresolvable };
  113. union {
  114. Value m_base_value {};
  115. mutable Environment* m_base_environment;
  116. };
  117. PropertyKey m_name;
  118. Value m_this_value;
  119. bool m_strict { false };
  120. bool m_is_private { false };
  121. // FIXME: This can (probably) be an union with m_name.
  122. PrivateName m_private_name;
  123. Optional<EnvironmentCoordinate> m_environment_coordinate;
  124. };
  125. }