DeclarativeEnvironment.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2020-2022, 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 index = m_names.find_first_index(name);
  34. if (!index.has_value())
  35. return false;
  36. if (!is_permanently_screwed_by_eval() && out_index)
  37. *out_index = *index;
  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. m_names.append(name);
  52. // 1. Assert: envRec does not already have a binding for N.
  53. // NOTE: We skip this to avoid O(n) traversal of m_names.
  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. m_names.append(name);
  69. // 1. Assert: envRec does not already have a binding for N.
  70. // NOTE: We skip this to avoid O(n) traversal of m_names.
  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 index = m_names.find_first_index(name);
  78. VERIFY(index.has_value());
  79. auto& binding = m_bindings[*index];
  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 index = m_names.find_first_index(name);
  94. if (!index.has_value()) {
  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, *index, 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, m_names[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 index = m_names.find_first_index(name);
  130. VERIFY(index.has_value());
  131. // 2-3. (extracted into a non-standard function below)
  132. return get_binding_value_direct(global_object, *index, 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, m_names[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 index = m_names.find_first_index(name);
  148. VERIFY(index.has_value());
  149. auto& binding = m_bindings[*index];
  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 entries in m_bindings and m_names to avoid disturbing indices.
  155. binding = {};
  156. m_names[*index] = {};
  157. // 4. Return true.
  158. return true;
  159. }
  160. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_or_set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value)
  161. {
  162. auto index = m_names.find_first_index(name);
  163. VERIFY(index.has_value());
  164. auto& binding = m_bindings[*index];
  165. if (!binding.initialized)
  166. TRY(initialize_binding(global_object, name, value));
  167. else
  168. TRY(set_mutable_binding(global_object, name, value, false));
  169. return {};
  170. }
  171. void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>, GlobalObject& global_object, FlyString const& name, Value value)
  172. {
  173. MUST(initialize_or_set_mutable_binding(global_object, name, value));
  174. }
  175. }