Reference.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <AK/String.h>
  8. #include <LibJS/Runtime/Environment.h>
  9. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  10. #include <LibJS/Runtime/PropertyKey.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS {
  13. Reference make_private_reference(VM&, Value base_value, FlyString const& private_identifier);
  14. class Reference {
  15. public:
  16. enum class BaseType : u8 {
  17. Unresolvable,
  18. Value,
  19. Environment,
  20. };
  21. Reference() { }
  22. Reference(BaseType type, PropertyKey name, bool strict)
  23. : m_base_type(type)
  24. , m_name(move(name))
  25. , m_strict(strict)
  26. {
  27. }
  28. Reference(Value base, PropertyKey name, Value this_value, bool strict = false)
  29. : m_base_type(BaseType::Value)
  30. , m_base_value(base)
  31. , m_name(move(name))
  32. , m_this_value(this_value)
  33. , m_strict(strict)
  34. {
  35. if (base.is_nullish()) {
  36. m_base_type = BaseType::Unresolvable;
  37. m_base_value = {};
  38. m_this_value = {};
  39. m_name = {};
  40. }
  41. }
  42. Reference(Environment& base, FlyString referenced_name, bool strict = false, Optional<EnvironmentCoordinate> environment_coordinate = {})
  43. : m_base_type(BaseType::Environment)
  44. , m_base_environment(&base)
  45. , m_name(move(referenced_name))
  46. , m_strict(strict)
  47. , m_environment_coordinate(move(environment_coordinate))
  48. {
  49. }
  50. Reference(Value base, PrivateName name)
  51. : m_base_type(BaseType::Value)
  52. , m_base_value(base)
  53. , m_this_value(Value {})
  54. , m_strict(true)
  55. , m_is_private(true)
  56. , m_private_name(move(name))
  57. {
  58. }
  59. Value base() const
  60. {
  61. VERIFY(m_base_type == BaseType::Value);
  62. return m_base_value;
  63. }
  64. Environment& base_environment() const
  65. {
  66. VERIFY(m_base_type == BaseType::Environment);
  67. return *m_base_environment;
  68. }
  69. PropertyKey const& name() const { return m_name; }
  70. bool is_strict() const { return m_strict; }
  71. // 6.2.4.2 IsUnresolvableReference ( V ), https://tc39.es/ecma262/#sec-isunresolvablereference
  72. bool is_unresolvable() const { return m_base_type == BaseType::Unresolvable; }
  73. // 6.2.4.1 IsPropertyReference ( V ), https://tc39.es/ecma262/#sec-ispropertyreference
  74. bool is_property_reference() const
  75. {
  76. if (is_unresolvable())
  77. return false;
  78. if (m_base_type == BaseType::Environment)
  79. return false;
  80. return true;
  81. }
  82. // 6.2.4.7 GetThisValue ( V ), https://tc39.es/ecma262/#sec-getthisvalue
  83. Value get_this_value() const
  84. {
  85. VERIFY(is_property_reference());
  86. if (is_super_reference())
  87. return m_this_value;
  88. return m_base_value;
  89. }
  90. // 6.2.4.3 IsSuperReference ( V ), https://tc39.es/ecma262/#sec-issuperreference
  91. bool is_super_reference() const
  92. {
  93. return !m_this_value.is_empty();
  94. }
  95. // 6.2.4.4 IsPrivateReference ( V ), https://tc39.es/ecma262/#sec-isprivatereference
  96. bool is_private_reference() const
  97. {
  98. return m_is_private;
  99. }
  100. // Note: Non-standard helper.
  101. bool is_environment_reference() const
  102. {
  103. return m_base_type == BaseType::Environment;
  104. }
  105. // 6.2.4.8 InitializeReferencedBinding ( V, W ), https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
  106. ThrowCompletionOr<void> initialize_referenced_binding(GlobalObject& global_object, Value value) const
  107. {
  108. VERIFY(!is_unresolvable());
  109. VERIFY(m_base_type == BaseType::Environment);
  110. return m_base_environment->initialize_binding(global_object, m_name.as_string(), value);
  111. }
  112. ThrowCompletionOr<void> put_value(GlobalObject&, Value);
  113. ThrowCompletionOr<Value> get_value(GlobalObject&) const;
  114. ThrowCompletionOr<bool> delete_(GlobalObject&);
  115. String to_string() const;
  116. bool is_valid_reference() const { return m_name.is_valid() || m_is_private; }
  117. Optional<EnvironmentCoordinate> environment_coordinate() const { return m_environment_coordinate; }
  118. private:
  119. Completion throw_reference_error(GlobalObject&) const;
  120. BaseType m_base_type { BaseType::Unresolvable };
  121. union {
  122. Value m_base_value {};
  123. mutable Environment* m_base_environment;
  124. };
  125. PropertyKey m_name;
  126. Value m_this_value;
  127. bool m_strict { false };
  128. bool m_is_private { false };
  129. // FIXME: This can (probably) be an union with m_name.
  130. PrivateName m_private_name;
  131. Optional<EnvironmentCoordinate> m_environment_coordinate;
  132. };
  133. }