GlobalEnvironment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/AST.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/DeclarativeEnvironment.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(FlyString 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, FlyString 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, FlyString 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 ), https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v
  69. ThrowCompletionOr<void> GlobalEnvironment::initialize_binding(VM& vm, FlyString const& name, Value value)
  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).
  75. return MUST(m_declarative_record->initialize_binding(vm, name, value));
  76. }
  77. // 3. Assert: If the binding exists, it must be in the object Environment Record.
  78. // 4. Let ObjRec be envRec.[[ObjectRecord]].
  79. // 5. Return ? ObjRec.InitializeBinding(N, V).
  80. return m_object_record->initialize_binding(vm, name, value);
  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, FlyString 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, FlyString const& name, bool strict)
  97. {
  98. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  99. // 2. If ! DclRec.HasBinding(N) is true, then
  100. if (MUST(m_declarative_record->has_binding(name))) {
  101. // a. Return ? DclRec.GetBindingValue(N, S).
  102. return m_declarative_record->get_binding_value(vm, name, strict);
  103. }
  104. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  105. // 4. Return ? ObjRec.GetBindingValue(N, S).
  106. return m_object_record->get_binding_value(vm, name, strict);
  107. }
  108. // 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
  109. ThrowCompletionOr<bool> GlobalEnvironment::delete_binding(VM& vm, FlyString const& name)
  110. {
  111. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  112. // 2. If ! DclRec.HasBinding(N) is true, then
  113. if (MUST(m_declarative_record->has_binding(name))) {
  114. // a. Return ! DclRec.DeleteBinding(N).
  115. return MUST(m_declarative_record->delete_binding(vm, name));
  116. }
  117. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  118. // 4. Let globalObject be ObjRec.[[BindingObject]].
  119. // 5. Let existingProp be ? HasOwnProperty(globalObject, N).
  120. bool existing_prop = TRY(m_object_record->binding_object().has_own_property(name));
  121. // 6. If existingProp is true, then
  122. if (existing_prop) {
  123. // a. Let status be ? ObjRec.DeleteBinding(N).
  124. bool status = TRY(m_object_record->delete_binding(vm, name));
  125. // b. If status is true, then
  126. if (status) {
  127. // i. Let varNames be envRec.[[VarNames]].
  128. // ii. If N is an element of varNames, remove that element from the varNames.
  129. m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
  130. }
  131. // c. Return status.
  132. return status;
  133. }
  134. // 7. Return true.
  135. return true;
  136. }
  137. // 9.1.1.4.12 HasVarDeclaration ( N ), https://tc39.es/ecma262/#sec-hasvardeclaration
  138. bool GlobalEnvironment::has_var_declaration(FlyString const& name) const
  139. {
  140. // 1. Let varDeclaredNames be envRec.[[VarNames]].
  141. // 2. If varDeclaredNames contains N, return true.
  142. // 3. Return false.
  143. return m_var_names.contains_slow(name);
  144. }
  145. // 9.1.1.4.13 HasLexicalDeclaration ( N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
  146. bool GlobalEnvironment::has_lexical_declaration(FlyString const& name) const
  147. {
  148. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  149. // 2. Return ! DclRec.HasBinding(N).
  150. return MUST(m_declarative_record->has_binding(name));
  151. }
  152. // 9.1.1.4.14 HasRestrictedGlobalProperty ( N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
  153. ThrowCompletionOr<bool> GlobalEnvironment::has_restricted_global_property(FlyString const& name) const
  154. {
  155. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  156. // 2. Let globalObject be ObjRec.[[BindingObject]].
  157. auto& global_object = m_object_record->binding_object();
  158. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  159. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  160. // 4. If existingProp is undefined, return false.
  161. if (!existing_prop.has_value())
  162. return false;
  163. // 5. If existingProp.[[Configurable]] is true, return false.
  164. if (*existing_prop->configurable)
  165. return false;
  166. // 6. Return true.
  167. return true;
  168. }
  169. // 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
  170. ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_var(FlyString const& name) const
  171. {
  172. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  173. // 2. Let globalObject be ObjRec.[[BindingObject]].
  174. auto& global_object = m_object_record->binding_object();
  175. // 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
  176. bool has_property = TRY(global_object.has_own_property(name));
  177. // 4. If hasProperty is true, return true.
  178. if (has_property)
  179. return true;
  180. // 5. Return ? IsExtensible(globalObject).
  181. return global_object.is_extensible();
  182. }
  183. // 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
  184. ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_function(FlyString const& name) const
  185. {
  186. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  187. // 2. Let globalObject be ObjRec.[[BindingObject]].
  188. auto& global_object = m_object_record->binding_object();
  189. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  190. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  191. // 4. If existingProp is undefined, return ? IsExtensible(globalObject).
  192. if (!existing_prop.has_value())
  193. return TRY(global_object.is_extensible());
  194. // 5. If existingProp.[[Configurable]] is true, return true.
  195. if (*existing_prop->configurable)
  196. return true;
  197. // 6. If IsDataDescriptor(existingProp) is true and existingProp has attribute values { [[Writable]]: true, [[Enumerable]]: true }, return true.
  198. if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
  199. return true;
  200. // 7. Return false.
  201. return false;
  202. }
  203. // 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
  204. ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted)
  205. {
  206. auto& vm = this->vm();
  207. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  208. // 2. Let globalObject be ObjRec.[[BindingObject]].
  209. auto& global_object = m_object_record->binding_object();
  210. // 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
  211. auto has_property = TRY(global_object.has_own_property(name));
  212. // 4. Let extensible be ? IsExtensible(globalObject).
  213. auto extensible = TRY(global_object.is_extensible());
  214. // 5. If hasProperty is false and extensible is true, then
  215. if (!has_property && extensible) {
  216. // a. Perform ? ObjRec.CreateMutableBinding(N, D).
  217. TRY(m_object_record->create_mutable_binding(vm, name, can_be_deleted));
  218. // b. Perform ? ObjRec.InitializeBinding(N, undefined).
  219. TRY(m_object_record->initialize_binding(vm, name, js_undefined()));
  220. }
  221. // 6. Let varDeclaredNames be envRec.[[VarNames]].
  222. // 7. If varDeclaredNames does not contain N, then
  223. if (!m_var_names.contains_slow(name)) {
  224. // a. Append N to varDeclaredNames.
  225. m_var_names.append(name);
  226. }
  227. // 8. Return unused.
  228. return {};
  229. }
  230. // 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  231. ThrowCompletionOr<void> GlobalEnvironment::create_global_function_binding(FlyString const& name, Value value, bool can_be_deleted)
  232. {
  233. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  234. // 2. Let globalObject be ObjRec.[[BindingObject]].
  235. auto& global_object = m_object_record->binding_object();
  236. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  237. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  238. PropertyDescriptor desc;
  239. // 4. If existingProp is undefined or existingProp.[[Configurable]] is true, then
  240. if (!existing_prop.has_value() || *existing_prop->configurable) {
  241. // a. Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }.
  242. desc = { .value = value, .writable = true, .enumerable = true, .configurable = can_be_deleted };
  243. }
  244. // 5. Else,
  245. else {
  246. // a. Let desc be the PropertyDescriptor { [[Value]]: V }.
  247. desc = { .value = value };
  248. }
  249. // 6. Perform ? DefinePropertyOrThrow(globalObject, N, desc).
  250. TRY(global_object.define_property_or_throw(name, desc));
  251. // 7. Perform ? Set(globalObject, N, V, false).
  252. TRY(global_object.set(name, value, Object::ShouldThrowExceptions::Yes));
  253. // 8. Let varDeclaredNames be envRec.[[VarNames]].
  254. // 9. If varDeclaredNames does not contain N, then
  255. if (!m_var_names.contains_slow(name)) {
  256. // a. Append N to varDeclaredNames.
  257. m_var_names.append(name);
  258. }
  259. // 10. Return unused.
  260. return {};
  261. }
  262. }