DeclarativeEnvironment.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.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_without_realm<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.append(Binding {
  60. .name = name,
  61. .value = {},
  62. .strict = false,
  63. .mutable_ = true,
  64. .can_be_deleted = can_be_deleted,
  65. .initialized = false,
  66. });
  67. ++m_environment_serial_number;
  68. // 3. Return unused.
  69. return {};
  70. }
  71. // 9.1.1.1.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-createimmutablebinding-n-s
  72. ThrowCompletionOr<void> DeclarativeEnvironment::create_immutable_binding(VM&, DeprecatedFlyString const& name, bool strict)
  73. {
  74. // 1. Assert: envRec does not already have a binding for N.
  75. // NOTE: We skip this to avoid O(n) traversal of m_bindings.
  76. // 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.
  77. m_bindings.append(Binding {
  78. .name = name,
  79. .value = {},
  80. .strict = strict,
  81. .mutable_ = false,
  82. .can_be_deleted = false,
  83. .initialized = false,
  84. });
  85. ++m_environment_serial_number;
  86. // 3. Return unused.
  87. return {};
  88. }
  89. // 9.1.1.1.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
  90. // 4.1.1.1.1 InitializeBinding ( N, V, hint ), https://tc39.es/proposal-explicit-resource-management/#sec-declarative-environment-records
  91. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_binding(VM& vm, DeprecatedFlyString const& name, Value value, Environment::InitializeBindingHint hint)
  92. {
  93. return initialize_binding_direct(vm, find_binding_and_index(name)->index().value(), value, hint);
  94. }
  95. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_binding_direct(VM& vm, size_t index, Value value, Environment::InitializeBindingHint hint)
  96. {
  97. auto& binding = m_bindings.at(index);
  98. // 1. Assert: envRec must have an uninitialized binding for N.
  99. VERIFY(binding.initialized == false);
  100. // 2. If hint is not normal, perform ? AddDisposableResource(envRec, V, hint).
  101. if (hint != Environment::InitializeBindingHint::Normal)
  102. TRY(add_disposable_resource(vm, m_disposable_resource_stack, value, hint));
  103. // 3. Set the bound value for N in envRec to V.
  104. binding.value = value;
  105. // 4. Record that the binding for N in envRec has been initialized.
  106. binding.initialized = true;
  107. // 5. Return unused.
  108. return {};
  109. }
  110. // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
  111. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value, bool strict)
  112. {
  113. // 1. If envRec does not have a binding for N, then
  114. auto binding_and_index = find_binding_and_index(name);
  115. if (!binding_and_index.has_value()) {
  116. // a. If S is true, throw a ReferenceError exception.
  117. if (strict)
  118. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  119. // b. Perform ! envRec.CreateMutableBinding(N, true).
  120. MUST(create_mutable_binding(vm, name, true));
  121. // c. Perform ! envRec.InitializeBinding(N, V, normal).
  122. MUST(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  123. // d. Return unused.
  124. return {};
  125. }
  126. // 2-5. (extracted into a non-standard function below)
  127. TRY(set_mutable_binding_direct(vm, binding_and_index->binding(), value, strict));
  128. // 6. Return unused.
  129. return {};
  130. }
  131. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, size_t index, Value value, bool strict)
  132. {
  133. return set_mutable_binding_direct(vm, m_bindings[index], value, strict);
  134. }
  135. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, Binding& binding, Value value, bool strict)
  136. {
  137. if (binding.strict)
  138. strict = true;
  139. if (!binding.initialized)
  140. return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, binding.name);
  141. if (binding.mutable_) {
  142. binding.value = value;
  143. } else {
  144. if (strict)
  145. return vm.throw_completion<TypeError>(ErrorType::InvalidAssignToConst);
  146. }
  147. return {};
  148. }
  149. // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
  150. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value(VM& vm, DeprecatedFlyString const& name, [[maybe_unused]] bool strict)
  151. {
  152. // 1. Assert: envRec has a binding for N.
  153. auto binding_and_index = find_binding_and_index(name);
  154. VERIFY(binding_and_index.has_value());
  155. // 2-3. (extracted into a non-standard function below)
  156. return get_binding_value_direct(vm, binding_and_index->binding());
  157. }
  158. // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
  159. ThrowCompletionOr<bool> DeclarativeEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
  160. {
  161. // 1. Assert: envRec has a binding for the name that is the value of N.
  162. auto binding_and_index = find_binding_and_index(name);
  163. VERIFY(binding_and_index.has_value());
  164. // 2. If the binding for N in envRec cannot be deleted, return false.
  165. if (!binding_and_index->binding().can_be_deleted)
  166. return false;
  167. // 3. Remove the binding for N from envRec.
  168. // NOTE: We keep the entries in m_bindings to avoid disturbing indices.
  169. binding_and_index->binding() = {};
  170. ++m_environment_serial_number;
  171. // 4. Return true.
  172. return true;
  173. }
  174. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_or_set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value)
  175. {
  176. auto binding_and_index = find_binding_and_index(name);
  177. VERIFY(binding_and_index.has_value());
  178. if (!binding_and_index->binding().initialized)
  179. TRY(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  180. else
  181. TRY(set_mutable_binding(vm, name, value, false));
  182. return {};
  183. }
  184. void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>, VM& vm, DeprecatedFlyString const& name, Value value)
  185. {
  186. MUST(initialize_or_set_mutable_binding(vm, name, value));
  187. }
  188. void DeclarativeEnvironment::shrink_to_fit()
  189. {
  190. m_bindings.shrink_to_fit();
  191. }
  192. }