GlobalEnvironment.cpp 13 KB

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