GlobalEnvironment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibJS/Runtime/DeclarativeEnvironment.h>
  9. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  10. #include <LibJS/Runtime/GlobalEnvironment.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/ObjectEnvironment.h>
  13. namespace JS {
  14. JS_DEFINE_ALLOCATOR(GlobalEnvironment);
  15. // 9.1.2.5 NewGlobalEnvironment ( G, thisValue ), https://tc39.es/ecma262/#sec-newglobalenvironment
  16. GlobalEnvironment::GlobalEnvironment(Object& global_object, Object& this_value)
  17. : Environment(nullptr)
  18. , m_global_this_value(&this_value)
  19. {
  20. m_object_record = global_object.heap().allocate<ObjectEnvironment>(global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
  21. m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>();
  22. }
  23. void GlobalEnvironment::visit_edges(Cell::Visitor& visitor)
  24. {
  25. Base::visit_edges(visitor);
  26. visitor.visit(m_object_record);
  27. visitor.visit(m_global_this_value);
  28. visitor.visit(m_declarative_record);
  29. }
  30. // 9.1.1.4.11 GetThisBinding ( ), https://tc39.es/ecma262/#sec-global-environment-records-getthisbinding
  31. ThrowCompletionOr<Value> GlobalEnvironment::get_this_binding(VM&) const
  32. {
  33. // 1. Return envRec.[[GlobalThisValue]].
  34. return m_global_this_value;
  35. }
  36. // 9.1.1.4.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n
  37. ThrowCompletionOr<bool> GlobalEnvironment::has_binding(DeprecatedFlyString const& name, Optional<size_t>*) const
  38. {
  39. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  40. // 2. If ! DclRec.HasBinding(N) is true, return true.
  41. if (MUST(m_declarative_record->has_binding(name)))
  42. return true;
  43. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  44. // 4. Return ? ObjRec.HasBinding(N).
  45. return m_object_record->has_binding(name);
  46. }
  47. // 9.1.1.4.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-global-environment-records-createmutablebinding-n-d
  48. ThrowCompletionOr<void> GlobalEnvironment::create_mutable_binding(VM& vm, DeprecatedFlyString const& name, bool can_be_deleted)
  49. {
  50. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  51. // 2. If ! DclRec.HasBinding(N) is true, throw a TypeError exception.
  52. if (MUST(m_declarative_record->has_binding(name)))
  53. return vm.throw_completion<TypeError>(ErrorType::GlobalEnvironmentAlreadyHasBinding, name);
  54. // 3. Return ! DclRec.CreateMutableBinding(N, D).
  55. return MUST(m_declarative_record->create_mutable_binding(vm, name, can_be_deleted));
  56. }
  57. // 9.1.1.4.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-createimmutablebinding-n-s
  58. ThrowCompletionOr<void> GlobalEnvironment::create_immutable_binding(VM& vm, DeprecatedFlyString const& name, bool strict)
  59. {
  60. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  61. // 2. If ! DclRec.HasBinding(N) is true, throw a TypeError exception.
  62. if (MUST(m_declarative_record->has_binding(name)))
  63. return vm.throw_completion<TypeError>(ErrorType::GlobalEnvironmentAlreadyHasBinding, name);
  64. // 3. Return ! DclRec.CreateImmutableBinding(N, S).
  65. return MUST(m_declarative_record->create_immutable_binding(vm, name, strict));
  66. }
  67. // 9.1.1.4.4 InitializeBinding ( N, V, hint ), https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v
  68. ThrowCompletionOr<void> GlobalEnvironment::initialize_binding(VM& vm, DeprecatedFlyString const& name, Value value, InitializeBindingHint hint)
  69. {
  70. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  71. // 2. If ! DclRec.HasBinding(N) is true, then
  72. if (MUST(m_declarative_record->has_binding(name))) {
  73. // a. Return ! DclRec.InitializeBinding(N, V, hint).
  74. return MUST(m_declarative_record->initialize_binding(vm, name, value, hint));
  75. }
  76. // 3. Assert: If the binding exists, it must be in the object Environment Record.
  77. // 4. Assert: hint is normal.
  78. VERIFY(hint == Environment::InitializeBindingHint::Normal);
  79. // 5. Let ObjRec be envRec.[[ObjectRecord]].
  80. // 6. Return ? ObjRec.InitializeBinding(N, V, normal).
  81. return m_object_record->initialize_binding(vm, name, value, Environment::InitializeBindingHint::Normal);
  82. }
  83. // 9.1.1.4.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s
  84. ThrowCompletionOr<void> GlobalEnvironment::set_mutable_binding(VM& vm, DeprecatedFlyString const& name, Value value, bool strict)
  85. {
  86. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  87. // 2. If ! DclRec.HasBinding(N) is true, then
  88. if (MUST(m_declarative_record->has_binding(name))) {
  89. // a. Return ? DclRec.SetMutableBinding(N, V, S).
  90. return m_declarative_record->set_mutable_binding(vm, name, value, strict);
  91. }
  92. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  93. // 4. Return ? ObjRec.SetMutableBinding(N, V, S).
  94. return m_object_record->set_mutable_binding(vm, name, value, strict);
  95. }
  96. // 9.1.1.4.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s
  97. ThrowCompletionOr<Value> GlobalEnvironment::get_binding_value(VM& vm, DeprecatedFlyString const& name, bool strict)
  98. {
  99. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  100. // 2. If ! DclRec.HasBinding(N) is true, then
  101. Optional<size_t> index;
  102. if (MUST(m_declarative_record->has_binding(name, &index))) {
  103. // a. Return ? DclRec.GetBindingValue(N, S).
  104. if (index.has_value())
  105. return m_declarative_record->get_binding_value_direct(vm, index.value());
  106. return m_declarative_record->get_binding_value(vm, name, strict);
  107. }
  108. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  109. // 4. Return ? ObjRec.GetBindingValue(N, S).
  110. return m_object_record->get_binding_value(vm, name, strict);
  111. }
  112. // 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
  113. ThrowCompletionOr<bool> GlobalEnvironment::delete_binding(VM& vm, DeprecatedFlyString const& name)
  114. {
  115. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  116. // 2. If ! DclRec.HasBinding(N) is true, then
  117. if (MUST(m_declarative_record->has_binding(name))) {
  118. // a. Return ! DclRec.DeleteBinding(N).
  119. return MUST(m_declarative_record->delete_binding(vm, name));
  120. }
  121. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  122. // 4. Let globalObject be ObjRec.[[BindingObject]].
  123. // 5. Let existingProp be ? HasOwnProperty(globalObject, N).
  124. bool existing_prop = TRY(m_object_record->binding_object().has_own_property(name));
  125. // 6. If existingProp is true, then
  126. if (existing_prop) {
  127. // a. Let status be ? ObjRec.DeleteBinding(N).
  128. bool status = TRY(m_object_record->delete_binding(vm, name));
  129. // b. If status is true, then
  130. if (status) {
  131. // i. Let varNames be envRec.[[VarNames]].
  132. // ii. If N is an element of varNames, remove that element from the varNames.
  133. m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
  134. }
  135. // c. Return status.
  136. return status;
  137. }
  138. // 7. Return true.
  139. return true;
  140. }
  141. // 9.1.1.4.12 HasVarDeclaration ( N ), https://tc39.es/ecma262/#sec-hasvardeclaration
  142. bool GlobalEnvironment::has_var_declaration(DeprecatedFlyString const& name) const
  143. {
  144. // 1. Let varDeclaredNames be envRec.[[VarNames]].
  145. // 2. If varDeclaredNames contains N, return true.
  146. // 3. Return false.
  147. return m_var_names.contains_slow(name);
  148. }
  149. // 9.1.1.4.13 HasLexicalDeclaration ( N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
  150. bool GlobalEnvironment::has_lexical_declaration(DeprecatedFlyString const& name) const
  151. {
  152. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  153. // 2. Return ! DclRec.HasBinding(N).
  154. return MUST(m_declarative_record->has_binding(name));
  155. }
  156. // 9.1.1.4.14 HasRestrictedGlobalProperty ( N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
  157. ThrowCompletionOr<bool> GlobalEnvironment::has_restricted_global_property(DeprecatedFlyString const& name) const
  158. {
  159. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  160. // 2. Let globalObject be ObjRec.[[BindingObject]].
  161. auto& global_object = m_object_record->binding_object();
  162. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  163. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  164. // 4. If existingProp is undefined, return false.
  165. if (!existing_prop.has_value())
  166. return false;
  167. // 5. If existingProp.[[Configurable]] is true, return false.
  168. if (*existing_prop->configurable)
  169. return false;
  170. // 6. Return true.
  171. return true;
  172. }
  173. // 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
  174. ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_var(DeprecatedFlyString const& name) const
  175. {
  176. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  177. // 2. Let globalObject be ObjRec.[[BindingObject]].
  178. auto& global_object = m_object_record->binding_object();
  179. // 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
  180. bool has_property = TRY(global_object.has_own_property(name));
  181. // 4. If hasProperty is true, return true.
  182. if (has_property)
  183. return true;
  184. // 5. Return ? IsExtensible(globalObject).
  185. return global_object.is_extensible();
  186. }
  187. // 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
  188. ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_function(DeprecatedFlyString const& name) const
  189. {
  190. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  191. // 2. Let globalObject be ObjRec.[[BindingObject]].
  192. auto& global_object = m_object_record->binding_object();
  193. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  194. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  195. // 4. If existingProp is undefined, return ? IsExtensible(globalObject).
  196. if (!existing_prop.has_value())
  197. return TRY(global_object.is_extensible());
  198. // 5. If existingProp.[[Configurable]] is true, return true.
  199. if (*existing_prop->configurable)
  200. return true;
  201. // 6. If IsDataDescriptor(existingProp) is true and existingProp has attribute values { [[Writable]]: true, [[Enumerable]]: true }, return true.
  202. if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
  203. return true;
  204. // 7. Return false.
  205. return false;
  206. }
  207. // 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
  208. ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(DeprecatedFlyString const& name, bool can_be_deleted)
  209. {
  210. auto& vm = this->vm();
  211. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  212. // 2. Let globalObject be ObjRec.[[BindingObject]].
  213. auto& global_object = m_object_record->binding_object();
  214. // 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
  215. auto has_property = TRY(global_object.has_own_property(name));
  216. // 4. Let extensible be ? IsExtensible(globalObject).
  217. auto extensible = TRY(global_object.is_extensible());
  218. // 5. If hasProperty is false and extensible is true, then
  219. if (!has_property && extensible) {
  220. // a. Perform ? ObjRec.CreateMutableBinding(N, D).
  221. TRY(m_object_record->create_mutable_binding(vm, name, can_be_deleted));
  222. // b. Perform ? ObjRec.InitializeBinding(N, undefined, normal).
  223. TRY(m_object_record->initialize_binding(vm, name, js_undefined(), Environment::InitializeBindingHint::Normal));
  224. }
  225. // 6. Let varDeclaredNames be envRec.[[VarNames]].
  226. // 7. If varDeclaredNames does not contain N, then
  227. if (!m_var_names.contains_slow(name)) {
  228. // a. Append N to varDeclaredNames.
  229. m_var_names.append(name);
  230. }
  231. // 8. Return unused.
  232. return {};
  233. }
  234. // 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  235. ThrowCompletionOr<void> GlobalEnvironment::create_global_function_binding(DeprecatedFlyString const& name, Value value, bool can_be_deleted)
  236. {
  237. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  238. // 2. Let globalObject be ObjRec.[[BindingObject]].
  239. auto& global_object = m_object_record->binding_object();
  240. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  241. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  242. PropertyDescriptor desc;
  243. // 4. If existingProp is undefined or existingProp.[[Configurable]] is true, then
  244. if (!existing_prop.has_value() || *existing_prop->configurable) {
  245. // a. Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }.
  246. desc = { .value = value, .writable = true, .enumerable = true, .configurable = can_be_deleted };
  247. }
  248. // 5. Else,
  249. else {
  250. // a. Let desc be the PropertyDescriptor { [[Value]]: V }.
  251. desc = { .value = value };
  252. }
  253. // 6. Perform ? DefinePropertyOrThrow(globalObject, N, desc).
  254. TRY(global_object.define_property_or_throw(name, desc));
  255. // 7. Perform ? Set(globalObject, N, V, false).
  256. TRY(global_object.set(name, value, Object::ShouldThrowExceptions::Yes));
  257. // 8. Let varDeclaredNames be envRec.[[VarNames]].
  258. // 9. If varDeclaredNames does not contain N, then
  259. if (!m_var_names.contains_slow(name)) {
  260. // a. Append N to varDeclaredNames.
  261. m_var_names.append(name);
  262. }
  263. // 10. Return unused.
  264. return {};
  265. }
  266. }