DeclarativeEnvironment.cpp 9.2 KB

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