DeclarativeEnvironment.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. ThrowCompletionOr<void> DeclarativeEnvironment::create_immutable_binding(GlobalObject&, FlyString const& name, bool strict)
  59. {
  60. // 2. Create an immutable binding in envRec for N and record that it is uninitialized. If S is true, record that the newly created binding is a strict binding.
  61. m_bindings.append(Binding {
  62. .value = {},
  63. .strict = strict,
  64. .mutable_ = false,
  65. .can_be_deleted = false,
  66. .initialized = false,
  67. });
  68. auto result = m_names.set(name, m_bindings.size() - 1);
  69. // 1. Assert: envRec does not already have a binding for N.
  70. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  71. // 3. Return NormalCompletion(empty).
  72. return {};
  73. }
  74. // 9.1.1.1.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
  75. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_binding(GlobalObject&, FlyString const& name, Value value)
  76. {
  77. auto it = m_names.find(name);
  78. VERIFY(it != m_names.end());
  79. auto& binding = m_bindings[it->value];
  80. // 1. Assert: envRec must have an uninitialized binding for N.
  81. VERIFY(binding.initialized == false);
  82. // 2. Set the bound value for N in envRec to V.
  83. binding.value = value;
  84. // 3. Record that the binding for N in envRec has been initialized.
  85. binding.initialized = true;
  86. // 4. Return NormalCompletion(empty).
  87. return {};
  88. }
  89. // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
  90. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  91. {
  92. // 1. If envRec does not have a binding for N, then
  93. auto it = m_names.find(name);
  94. if (it == m_names.end()) {
  95. // a. If S is true, throw a ReferenceError exception.
  96. if (strict)
  97. return vm().throw_completion<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  98. // b. Perform envRec.CreateMutableBinding(N, true).
  99. MUST(create_mutable_binding(global_object, name, true));
  100. // c. Perform envRec.InitializeBinding(N, V).
  101. MUST(initialize_binding(global_object, name, value));
  102. // d. Return NormalCompletion(empty).
  103. return {};
  104. }
  105. // 2-5. (extracted into a non-standard function below)
  106. TRY(set_mutable_binding_direct(global_object, it->value, value, strict));
  107. // 6. Return NormalCompletion(empty).
  108. return {};
  109. }
  110. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(GlobalObject& global_object, size_t index, Value value, bool strict)
  111. {
  112. auto& binding = m_bindings[index];
  113. if (binding.strict)
  114. strict = true;
  115. if (!binding.initialized)
  116. return vm().throw_completion<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name_from_index(index));
  117. if (binding.mutable_) {
  118. binding.value = value;
  119. } else {
  120. if (strict)
  121. return vm().throw_completion<TypeError>(global_object, ErrorType::InvalidAssignToConst);
  122. }
  123. return {};
  124. }
  125. // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
  126. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  127. {
  128. // 1. Assert: envRec has a binding for N.
  129. auto it = m_names.find(name);
  130. VERIFY(it != m_names.end());
  131. // 2-3. (extracted into a non-standard function below)
  132. return get_binding_value_direct(global_object, it->value, strict);
  133. }
  134. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value_direct(GlobalObject& global_object, size_t index, bool)
  135. {
  136. auto& binding = m_bindings[index];
  137. // 2. If the binding for N in envRec is an uninitialized binding, throw a ReferenceError exception.
  138. if (!binding.initialized)
  139. return vm().throw_completion<ReferenceError>(global_object, ErrorType::BindingNotInitialized, name_from_index(index));
  140. // 3. Return the value currently bound to N in envRec.
  141. return binding.value;
  142. }
  143. // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
  144. ThrowCompletionOr<bool> DeclarativeEnvironment::delete_binding(GlobalObject&, FlyString const& name)
  145. {
  146. // 1. Assert: envRec has a binding for the name that is the value of N.
  147. auto it = m_names.find(name);
  148. VERIFY(it != m_names.end());
  149. auto& binding = m_bindings[it->value];
  150. // 2. If the binding for N in envRec cannot be deleted, return false.
  151. if (!binding.can_be_deleted)
  152. return false;
  153. // 3. Remove the binding for N from envRec.
  154. // NOTE: We keep the entry in m_bindings to avoid disturbing indices.
  155. binding = {};
  156. m_names.remove(it);
  157. // 4. Return true.
  158. return true;
  159. }
  160. void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>, GlobalObject& global_object, FlyString const& name, Value value)
  161. {
  162. auto it = m_names.find(name);
  163. VERIFY(it != m_names.end());
  164. auto& binding = m_bindings[it->value];
  165. if (!binding.initialized)
  166. MUST(initialize_binding(global_object, name, value));
  167. else
  168. MUST(set_mutable_binding(global_object, name, value, false));
  169. }
  170. Vector<String> DeclarativeEnvironment::bindings() const
  171. {
  172. Vector<String> names;
  173. for (auto& it : m_names) {
  174. names.append(it.key);
  175. }
  176. return names;
  177. }
  178. FlyString const& DeclarativeEnvironment::name_from_index(size_t index) const
  179. {
  180. for (auto& it : m_names) {
  181. if (it.value == index)
  182. return it.key;
  183. }
  184. VERIFY_NOT_REACHED();
  185. }
  186. }