DeclarativeEnvironment.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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::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, Span<Binding const> 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. }
  38. // 9.1.1.1.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-hasbinding-n
  39. ThrowCompletionOr<bool> DeclarativeEnvironment::has_binding(DeprecatedFlyString const& name, Optional<size_t>* out_index) const
  40. {
  41. auto binding_and_index = find_binding_and_index(name);
  42. if (!binding_and_index.has_value())
  43. return false;
  44. if (!is_permanently_screwed_by_eval() && out_index && binding_and_index->index().has_value())
  45. *out_index = *(binding_and_index->index());
  46. return true;
  47. }
  48. // 9.1.1.1.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-declarative-environment-records-createmutablebinding-n-d
  49. ThrowCompletionOr<void> DeclarativeEnvironment::create_mutable_binding(VM&, DeprecatedFlyString const& name, bool can_be_deleted)
  50. {
  51. // 1. Assert: envRec does not already have a binding for N.
  52. // NOTE: We skip this to avoid O(n) traversal of m_bindings.
  53. // 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.
  54. m_bindings.append(Binding {
  55. .name = name,
  56. .value = {},
  57. .strict = false,
  58. .mutable_ = true,
  59. .can_be_deleted = can_be_deleted,
  60. .initialized = false,
  61. });
  62. // 3. Return unused.
  63. return {};
  64. }
  65. // 9.1.1.1.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-createimmutablebinding-n-s
  66. ThrowCompletionOr<void> DeclarativeEnvironment::create_immutable_binding(VM&, DeprecatedFlyString const& name, bool strict)
  67. {
  68. // 1. Assert: envRec does not already have a binding for N.
  69. // NOTE: We skip this to avoid O(n) traversal of m_bindings.
  70. // 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.
  71. m_bindings.append(Binding {
  72. .name = name,
  73. .value = {},
  74. .strict = strict,
  75. .mutable_ = false,
  76. .can_be_deleted = false,
  77. .initialized = false,
  78. });
  79. // 3. Return unused.
  80. return {};
  81. }
  82. // 9.1.1.1.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-declarative-environment-records-initializebinding-n-v
  83. // 4.1.1.1.1 InitializeBinding ( N, V, hint ), https://tc39.es/proposal-explicit-resource-management/#sec-declarative-environment-records
  84. ThrowCompletionOr<void> DeclarativeEnvironment::initialize_binding(VM&, DeprecatedFlyString const& name, Value value, Environment::InitializeBindingHint)
  85. {
  86. auto binding_and_index = find_binding_and_index(name);
  87. VERIFY(binding_and_index.has_value());
  88. auto& binding = binding_and_index->binding();
  89. // 1. Assert: envRec must have an uninitialized binding for N.
  90. VERIFY(binding.initialized == false);
  91. // FIXME: 2. If hint is not normal, perform ? AddDisposableResource(envRec, V, hint).
  92. // 3. Set the bound value for N in envRec to V.
  93. binding.value = value;
  94. // 4. Record that the binding for N in envRec has been initialized.
  95. binding.initialized = true;
  96. // 5. Return unused.
  97. return {};
  98. }
  99. // 9.1.1.1.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-setmutablebinding-n-v-s
  100. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value, bool strict)
  101. {
  102. // 1. If envRec does not have a binding for N, then
  103. auto binding_and_index = find_binding_and_index(name);
  104. if (!binding_and_index.has_value()) {
  105. // a. If S is true, throw a ReferenceError exception.
  106. if (strict)
  107. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  108. // b. Perform ! envRec.CreateMutableBinding(N, true).
  109. MUST(create_mutable_binding(vm, name, true));
  110. // c. Perform ! envRec.InitializeBinding(N, V, normal).
  111. MUST(initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal));
  112. // d. Return unused.
  113. return {};
  114. }
  115. // 2-5. (extracted into a non-standard function below)
  116. TRY(set_mutable_binding_direct(vm, binding_and_index->binding(), value, strict));
  117. // 6. Return unused.
  118. return {};
  119. }
  120. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, size_t index, Value value, bool strict)
  121. {
  122. return set_mutable_binding_direct(vm, m_bindings[index], value, strict);
  123. }
  124. ThrowCompletionOr<void> DeclarativeEnvironment::set_mutable_binding_direct(VM& vm, Binding& binding, Value value, bool strict)
  125. {
  126. if (binding.strict)
  127. strict = true;
  128. if (!binding.initialized)
  129. return vm.throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, binding.name);
  130. if (binding.mutable_) {
  131. binding.value = value;
  132. } else {
  133. if (strict)
  134. return vm.throw_completion<TypeError>(ErrorType::InvalidAssignToConst);
  135. }
  136. return {};
  137. }
  138. // 9.1.1.1.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-declarative-environment-records-getbindingvalue-n-s
  139. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value(VM& vm, DeprecatedFlyString const& name, bool strict)
  140. {
  141. // 1. Assert: envRec has a binding for N.
  142. auto binding_and_index = find_binding_and_index(name);
  143. VERIFY(binding_and_index.has_value());
  144. // 2-3. (extracted into a non-standard function below)
  145. return get_binding_value_direct(vm, binding_and_index->binding(), strict);
  146. }
  147. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value_direct(VM& vm, size_t index, bool strict)
  148. {
  149. return get_binding_value_direct(vm, m_bindings[index], strict);
  150. }
  151. ThrowCompletionOr<Value> DeclarativeEnvironment::get_binding_value_direct(VM&, Binding& binding, bool)
  152. {
  153. // 2. If the binding for N in envRec is an uninitialized binding, throw a ReferenceError exception.
  154. if (!binding.initialized)
  155. return vm().throw_completion<ReferenceError>(ErrorType::BindingNotInitialized, binding.name);
  156. // 3. Return the value currently bound to N in envRec.
  157. return binding.value;
  158. }
  159. // 9.1.1.1.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-declarative-environment-records-deletebinding-n
  160. ThrowCompletionOr<bool> DeclarativeEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
  161. {
  162. // 1. Assert: envRec has a binding for the name that is the value of N.
  163. auto binding_and_index = find_binding_and_index(name);
  164. VERIFY(binding_and_index.has_value());
  165. // 2. If the binding for N in envRec cannot be deleted, return false.
  166. if (!binding_and_index->binding().can_be_deleted)
  167. return false;
  168. // 3. Remove the binding for N from envRec.
  169. // NOTE: We keep the entries in m_bindings to avoid disturbing indices.
  170. binding_and_index->binding() = {};
  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. }