DeclarativeEnvironment.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.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. JS_DEFINE_ALLOCATOR(DeclarativeEnvironment);
  14. DeclarativeEnvironment* DeclarativeEnvironment::create_for_per_iteration_bindings(Badge<ForStatement>, DeclarativeEnvironment& other, size_t bindings_size)
  15. {
  16. auto bindings = other.m_bindings.span().slice(0, bindings_size);
  17. auto* parent_environment = other.outer_environment();
  18. return parent_environment->heap().allocate<DeclarativeEnvironment>(parent_environment, bindings);
  19. }
  20. DeclarativeEnvironment::DeclarativeEnvironment()
  21. : Environment(nullptr, IsDeclarative::Yes)
  22. {
  23. }
  24. DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_environment)
  25. : Environment(parent_environment, IsDeclarative::Yes)
  26. {
  27. }
  28. DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_environment, ReadonlySpan<Binding> bindings)
  29. : Environment(parent_environment, IsDeclarative::Yes)
  30. , m_bindings(bindings)
  31. {
  32. }
  33. void DeclarativeEnvironment::visit_edges(Visitor& visitor)
  34. {
  35. Base::visit_edges(visitor);
  36. for (auto& binding : m_bindings)
  37. visitor.visit(binding.value);
  38. for (auto& disposable : m_disposable_resource_stack) {
  39. visitor.visit(disposable.resource_value);
  40. visitor.visit(disposable.dispose_method);
  41. }
  42. }
  43. // 9.1.1.1.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-hasbinding-n
  44. ThrowCompletionOr<bool> DeclarativeEnvironment::has_binding(DeprecatedFlyString const& name, Optional<size_t>* out_index) const
  45. {
  46. auto binding_and_index = find_binding_and_index(name);
  47. if (!binding_and_index.has_value())
  48. return false;
  49. if (!is_permanently_screwed_by_eval() && out_index && binding_and_index->index().has_value())
  50. *out_index = *(binding_and_index->index());
  51. return true;
  52. }
  53. // 9.1.1.1.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-declarative-environment-records-createmutablebinding-n-d
  54. ThrowCompletionOr<void> DeclarativeEnvironment::create_mutable_binding(VM&, DeprecatedFlyString const& name, bool can_be_deleted)
  55. {
  56. // 1. Assert: envRec does not already have a binding for N.
  57. // NOTE: We skip this to avoid O(n) traversal of m_bindings.
  58. // 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.
  59. m_bindings_assoc.set(name, m_bindings.size());
  60. m_bindings.append(Binding {
  61. .name = name,
  62. .value = {},
  63. .strict = false,
  64. .mutable_ = true,
  65. .can_be_deleted = can_be_deleted,
  66. .initialized = false,
  67. });
  68. ++m_environment_serial_number;
  69. // 3. Return unused.
  70. return {};
  71. }
  72. // 9.1.1.1.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-createimmutablebinding-n-s
  73. ThrowCompletionOr<void> DeclarativeEnvironment::create_immutable_binding(VM&, DeprecatedFlyString const& name, bool strict)
  74. {
  75. // 1. Assert: envRec does not already have a binding for N.
  76. // NOTE: We skip this to avoid O(n) traversal of m_bindings.
  77. // 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.
  78. m_bindings_assoc.set(name, m_bindings.size());
  79. m_bindings.append(Binding {
  80. .name = name,
  81. .value = {},
  82. .strict = strict,
  83. .mutable_ = false,
  84. .can_be_deleted = false,
  85. .initialized = false,
  86. });
  87. ++m_environment_serial_number;
  88. // 3. Return unused.
  89. return {};
  90. }
  91. // 9.1.1.1.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
  92. // 4.1.1.1.1 InitializeBinding ( N, V, hint ), https://tc39.es/proposal-explicit-resource-management/#sec-declarative-environment-records
  93. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_binding(VM& vm, DeprecatedFlyString const& name, Value value, Environment::InitializeBindingHint hint)
  94. {
  95. return initialize_binding_direct(vm, find_binding_and_index(name)->index().value(), value, hint);
  96. }
  97. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_binding_direct(VM& vm, size_t index, Value value, Environment::InitializeBindingHint hint)
  98. {
  99. auto& binding = m_bindings.at(index);
  100. // 1. Assert: envRec must have an uninitialized binding for N.
  101. VERIFY(binding.initialized == false);
  102. // 2. If hint is not normal, perform ? AddDisposableResource(envRec, V, hint).
  103. if (hint != Environment::InitializeBindingHint::Normal)
  104. TRY(add_disposable_resource(vm, m_disposable_resource_stack, value, hint));
  105. // 3. Set the bound value for N in envRec to V.
  106. binding.value = value;
  107. // 4. Record that the binding for N in envRec has been initialized.
  108. binding.initialized = true;
  109. // 5. Return unused.
  110. return {};
  111. }
  112. // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
  113. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value, bool strict)
  114. {
  115. // 1. If envRec does not have a binding for N, then
  116. auto binding_and_index = find_binding_and_index(name);
  117. if (!binding_and_index.has_value()) {
  118. // a. If S is true, throw a ReferenceError exception.
  119. if (strict)
  120. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  121. // b. Perform ! envRec.CreateMutableBinding(N, true).
  122. MUST(create_mutable_binding(vm, name, true));
  123. // c. Perform ! envRec.InitializeBinding(N, V, normal).
  124. MUST(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  125. // d. Return unused.
  126. return {};
  127. }
  128. // 2-5. (extracted into a non-standard function below)
  129. TRY(set_mutable_binding_direct(vm, binding_and_index->binding(), value, strict));
  130. // 6. Return unused.
  131. return {};
  132. }
  133. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, size_t index, Value value, bool strict)
  134. {
  135. return set_mutable_binding_direct(vm, m_bindings[index], value, strict);
  136. }
  137. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, Binding& binding, Value value, bool strict)
  138. {
  139. if (binding.strict)
  140. strict = true;
  141. if (!binding.initialized)
  142. return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, binding.name);
  143. if (binding.mutable_) {
  144. binding.value = value;
  145. } else {
  146. if (strict)
  147. return vm.throw_completion<TypeError>(ErrorType::InvalidAssignToConst);
  148. }
  149. return {};
  150. }
  151. // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
  152. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value(VM& vm, DeprecatedFlyString const& name, [[maybe_unused]] bool strict)
  153. {
  154. // 1. Assert: envRec has a binding for N.
  155. auto binding_and_index = find_binding_and_index(name);
  156. VERIFY(binding_and_index.has_value());
  157. // 2-3. (extracted into a non-standard function below)
  158. return get_binding_value_direct(vm, binding_and_index->binding());
  159. }
  160. // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
  161. ThrowCompletionOr<bool> DeclarativeEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
  162. {
  163. // 1. Assert: envRec has a binding for the name that is the value of N.
  164. auto binding_and_index = find_binding_and_index(name);
  165. VERIFY(binding_and_index.has_value());
  166. // 2. If the binding for N in envRec cannot be deleted, return false.
  167. if (!binding_and_index->binding().can_be_deleted)
  168. return false;
  169. // 3. Remove the binding for N from envRec.
  170. // NOTE: We keep the entries in m_bindings to avoid disturbing indices.
  171. binding_and_index->binding() = {};
  172. ++m_environment_serial_number;
  173. // 4. Return true.
  174. return true;
  175. }
  176. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_or_set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value)
  177. {
  178. auto binding_and_index = find_binding_and_index(name);
  179. VERIFY(binding_and_index.has_value());
  180. if (!binding_and_index->binding().initialized)
  181. TRY(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  182. else
  183. TRY(set_mutable_binding(vm, name, value, false));
  184. return {};
  185. }
  186. void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>, VM& vm, DeprecatedFlyString const& name, Value value)
  187. {
  188. MUST(initialize_or_set_mutable_binding(vm, name, value));
  189. }
  190. void DeclarativeEnvironment::shrink_to_fit()
  191. {
  192. m_bindings.shrink_to_fit();
  193. }
  194. }