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