DeclarativeEnvironment.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Interpreter.h>
  7. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Value.h>
  12. namespace JS {
  13. DeclarativeEnvironment::DeclarativeEnvironment()
  14. : Environment(nullptr)
  15. {
  16. }
  17. DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_scope)
  18. : Environment(parent_scope)
  19. {
  20. }
  21. DeclarativeEnvironment::~DeclarativeEnvironment()
  22. {
  23. }
  24. void DeclarativeEnvironment::visit_edges(Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. for (auto& binding : m_bindings)
  28. visitor.visit(binding.value);
  29. }
  30. // 9.1.1.1.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-hasbinding-n
  31. ThrowCompletionOr<bool> DeclarativeEnvironment::has_binding(FlyString const& name, Optional<size_t>* out_index) const
  32. {
  33. auto it = m_names.find(name);
  34. if (it == m_names.end())
  35. return false;
  36. if (!is_permanently_screwed_by_eval() && out_index)
  37. *out_index = it->value;
  38. return true;
  39. }
  40. // 9.1.1.1.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-declarative-environment-records-createmutablebinding-n-d
  41. ThrowCompletionOr<void> DeclarativeEnvironment::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted)
  42. {
  43. // 2. Create a mutable binding in envRec for N and record that it is uninitialized. If D is true, record that the newly created binding may be deleted by a subsequent DeleteBinding call.
  44. m_bindings.append(Binding {
  45. .value = {},
  46. .strict = false,
  47. .mutable_ = true,
  48. .can_be_deleted = can_be_deleted,
  49. .initialized = false,
  50. });
  51. auto result = m_names.set(name, m_bindings.size() - 1);
  52. // 1. Assert: envRec does not already have a binding for N.
  53. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  54. // 3. Return NormalCompletion(empty).
  55. return {};
  56. }
  57. // 9.1.1.1.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-createimmutablebinding-n-s
  58. void DeclarativeEnvironment::create_immutable_binding(GlobalObject&, FlyString const& name, bool strict)
  59. {
  60. m_bindings.append(Binding {
  61. .value = {},
  62. .strict = strict,
  63. .mutable_ = false,
  64. .can_be_deleted = false,
  65. .initialized = false,
  66. });
  67. auto result = m_names.set(name, m_bindings.size() - 1);
  68. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  69. }
  70. // 9.1.1.1.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
  71. void DeclarativeEnvironment::initialize_binding(GlobalObject&, FlyString const& name, Value value)
  72. {
  73. auto it = m_names.find(name);
  74. VERIFY(it != m_names.end());
  75. auto& binding = m_bindings[it->value];
  76. VERIFY(binding.initialized == false);
  77. binding.value = value;
  78. binding.initialized = true;
  79. }
  80. // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
  81. void DeclarativeEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  82. {
  83. auto it = m_names.find(name);
  84. if (it == m_names.end()) {
  85. if (strict) {
  86. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  87. return;
  88. }
  89. (void)create_mutable_binding(global_object, name, true);
  90. initialize_binding(global_object, name, value);
  91. return;
  92. }
  93. set_mutable_binding_direct(global_object, it->value, value, strict);
  94. }
  95. void DeclarativeEnvironment::set_mutable_binding_direct(GlobalObject& global_object, size_t index, Value value, bool strict)
  96. {
  97. auto& binding = m_bindings[index];
  98. if (binding.strict)
  99. strict = true;
  100. if (!binding.initialized) {
  101. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name_from_index(index));
  102. return;
  103. }
  104. if (binding.mutable_) {
  105. binding.value = value;
  106. } else {
  107. if (strict) {
  108. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::InvalidAssignToConst);
  109. }
  110. }
  111. }
  112. // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
  113. Value DeclarativeEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  114. {
  115. auto it = m_names.find(name);
  116. VERIFY(it != m_names.end());
  117. return get_binding_value_direct(global_object, it->value, strict);
  118. }
  119. Value DeclarativeEnvironment::get_binding_value_direct(GlobalObject& global_object, size_t index, bool)
  120. {
  121. auto& binding = m_bindings[index];
  122. if (!binding.initialized) {
  123. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name_from_index(index));
  124. return {};
  125. }
  126. return binding.value;
  127. }
  128. // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
  129. bool DeclarativeEnvironment::delete_binding(GlobalObject&, FlyString const& name)
  130. {
  131. auto it = m_names.find(name);
  132. VERIFY(it != m_names.end());
  133. auto& binding = m_bindings[it->value];
  134. if (!binding.can_be_deleted)
  135. return false;
  136. // NOTE: We keep the entry in m_bindings to avoid disturbing indices.
  137. binding = {};
  138. m_names.remove(it);
  139. return true;
  140. }
  141. void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>, GlobalObject& global_object, FlyString const& name, Value value)
  142. {
  143. auto it = m_names.find(name);
  144. VERIFY(it != m_names.end());
  145. auto& binding = m_bindings[it->value];
  146. if (!binding.initialized)
  147. initialize_binding(global_object, name, value);
  148. else
  149. set_mutable_binding(global_object, name, value, false);
  150. }
  151. Vector<String> DeclarativeEnvironment::bindings() const
  152. {
  153. Vector<String> names;
  154. for (auto& it : m_names) {
  155. names.append(it.key);
  156. }
  157. return names;
  158. }
  159. FlyString const& DeclarativeEnvironment::name_from_index(size_t index) const
  160. {
  161. for (auto& it : m_names) {
  162. if (it.value == index)
  163. return it.key;
  164. }
  165. VERIFY_NOT_REACHED();
  166. }
  167. }