DeclarativeEnvironment.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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)
  22. {
  23. }
  24. DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_environment)
  25. : Environment(parent_environment)
  26. {
  27. }
  28. DeclarativeEnvironment::DeclarativeEnvironment(Environment* parent_environment, ReadonlySpan<Binding> bindings)
  29. : Environment(parent_environment)
  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. auto binding_and_index = find_binding_and_index(name);
  94. VERIFY(binding_and_index.has_value());
  95. auto& binding = binding_and_index->binding();
  96. // 1. Assert: envRec must have an uninitialized binding for N.
  97. VERIFY(binding.initialized == false);
  98. // 2. If hint is not normal, perform ? AddDisposableResource(envRec, V, hint).
  99. if (hint != Environment::InitializeBindingHint::Normal)
  100. TRY(add_disposable_resource(vm, m_disposable_resource_stack, value, hint));
  101. // 3. Set the bound value for N in envRec to V.
  102. binding.value = value;
  103. // 4. Record that the binding for N in envRec has been initialized.
  104. binding.initialized = true;
  105. // 5. Return unused.
  106. return {};
  107. }
  108. // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
  109. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value, bool strict)
  110. {
  111. // 1. If envRec does not have a binding for N, then
  112. auto binding_and_index = find_binding_and_index(name);
  113. if (!binding_and_index.has_value()) {
  114. // a. If S is true, throw a ReferenceError exception.
  115. if (strict)
  116. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  117. // b. Perform ! envRec.CreateMutableBinding(N, true).
  118. MUST(create_mutable_binding(vm, name, true));
  119. // c. Perform ! envRec.InitializeBinding(N, V, normal).
  120. MUST(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  121. // d. Return unused.
  122. return {};
  123. }
  124. // 2-5. (extracted into a non-standard function below)
  125. TRY(set_mutable_binding_direct(vm, binding_and_index->binding(), value, strict));
  126. // 6. Return unused.
  127. return {};
  128. }
  129. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, size_t index, Value value, bool strict)
  130. {
  131. return set_mutable_binding_direct(vm, m_bindings[index], value, strict);
  132. }
  133. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, Binding& binding, Value value, bool strict)
  134. {
  135. if (binding.strict)
  136. strict = true;
  137. if (!binding.initialized)
  138. return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, binding.name);
  139. if (binding.mutable_) {
  140. binding.value = value;
  141. } else {
  142. if (strict)
  143. return vm.throw_completion<TypeError>(ErrorType::InvalidAssignToConst);
  144. }
  145. return {};
  146. }
  147. // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
  148. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value(VM& vm, DeprecatedFlyString const& name, bool strict)
  149. {
  150. // 1. Assert: envRec has a binding for N.
  151. auto binding_and_index = find_binding_and_index(name);
  152. VERIFY(binding_and_index.has_value());
  153. // 2-3. (extracted into a non-standard function below)
  154. return get_binding_value_direct(vm, binding_and_index->binding(), strict);
  155. }
  156. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value_direct(VM& vm, size_t index, bool strict)
  157. {
  158. return get_binding_value_direct(vm, m_bindings[index], strict);
  159. }
  160. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value_direct(VM&, Binding& binding, bool)
  161. {
  162. // 2. If the binding for N in envRec is an uninitialized binding, throw a ReferenceError exception.
  163. if (!binding.initialized)
  164. return vm().throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, binding.name);
  165. // 3. Return the value currently bound to N in envRec.
  166. return binding.value;
  167. }
  168. // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
  169. ThrowCompletionOr<bool> DeclarativeEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
  170. {
  171. // 1. Assert: envRec has a binding for the name that is the value of N.
  172. auto binding_and_index = find_binding_and_index(name);
  173. VERIFY(binding_and_index.has_value());
  174. // 2. If the binding for N in envRec cannot be deleted, return false.
  175. if (!binding_and_index->binding().can_be_deleted)
  176. return false;
  177. // 3. Remove the binding for N from envRec.
  178. // NOTE: We keep the entries in m_bindings to avoid disturbing indices.
  179. binding_and_index->binding() = {};
  180. ++m_environment_serial_number;
  181. // 4. Return true.
  182. return true;
  183. }
  184. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_or_set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value)
  185. {
  186. auto binding_and_index = find_binding_and_index(name);
  187. VERIFY(binding_and_index.has_value());
  188. if (!binding_and_index->binding().initialized)
  189. TRY(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  190. else
  191. TRY(set_mutable_binding(vm, name, value, false));
  192. return {};
  193. }
  194. void DeclarativeEnvironment::initialize_or_set_mutable_binding(Badge<ScopeNode>, VM& vm, DeprecatedFlyString const& name, Value value)
  195. {
  196. MUST(initialize_or_set_mutable_binding(vm, name, value));
  197. }
  198. void DeclarativeEnvironment::shrink_to_fit()
  199. {
  200. m_bindings.shrink_to_fit();
  201. }
  202. }