GlobalEnvironment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(GlobalObject& 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_global_object<ObjectEnvironment>(global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
  20. m_declarative_record = global_object.heap().allocate_without_global_object<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(GlobalObject&) 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>*) 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(GlobalObject& global_object, FlyString 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>(global_object, ErrorType::GlobalEnvironmentAlreadyHasBinding, name);
  53. // 3. Return DclRec.CreateMutableBinding(N, D).
  54. return m_declarative_record->create_mutable_binding(global_object, 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(GlobalObject& global_object, FlyString 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>(global_object, ErrorType::GlobalEnvironmentAlreadyHasBinding, name);
  63. // 3. Return DclRec.CreateImmutableBinding(N, S).
  64. return m_declarative_record->create_immutable_binding(global_object, name, strict);
  65. }
  66. // 9.1.1.4.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v
  67. ThrowCompletionOr<void> GlobalEnvironment::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
  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).
  73. return MUST(m_declarative_record->initialize_binding(global_object, name, value));
  74. }
  75. // 3. Assert: If the binding exists, it must be in the object Environment Record.
  76. // 4. Let ObjRec be envRec.[[ObjectRecord]].
  77. // 5. Return ? ObjRec.InitializeBinding(N, V).
  78. return m_object_record->initialize_binding(global_object, name, value);
  79. }
  80. // 9.1.1.4.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s
  81. ThrowCompletionOr<void> GlobalEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  82. {
  83. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  84. // 2. If ! DclRec.HasBinding(N) is true, then
  85. if (MUST(m_declarative_record->has_binding(name))) {
  86. // a. Return ! DclRec.SetMutableBinding(N, V, S).
  87. // FIXME: Using MUST here breaks 22 tests in test262 (spec issue).
  88. // Example: `function f() { x = 1; } f(); let x;`
  89. return m_declarative_record->set_mutable_binding(global_object, 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(global_object, 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(GlobalObject& global_object, 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. // FIXME: Should be `! DclRec.GetBindingValue(N, S)` (see https://github.com/tc39/ecma262/pull/2764)
  102. // a. Return DclRec.GetBindingValue(N, S).
  103. return m_declarative_record->get_binding_value(global_object, name, strict);
  104. }
  105. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  106. // 4. Return ? ObjRec.GetBindingValue(N, S).
  107. return m_object_record->get_binding_value(global_object, name, strict);
  108. }
  109. // 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
  110. ThrowCompletionOr<bool> GlobalEnvironment::delete_binding(GlobalObject& global_object, FlyString const& name)
  111. {
  112. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  113. // 2. If ! DclRec.HasBinding(N) is true, then
  114. if (MUST(m_declarative_record->has_binding(name))) {
  115. // a. Return ! DclRec.DeleteBinding(N).
  116. return MUST(m_declarative_record->delete_binding(global_object, name));
  117. }
  118. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  119. // 4. Let globalObject be ObjRec.[[BindingObject]].
  120. // 5. Let existingProp be ? HasOwnProperty(globalObject, N).
  121. bool existing_prop = TRY(m_object_record->binding_object().has_own_property(name));
  122. // 6. If existingProp is true, then
  123. if (existing_prop) {
  124. // a. Let status be ? ObjRec.DeleteBinding(N).
  125. bool status = TRY(m_object_record->delete_binding(global_object, name));
  126. // b. If status is true, then
  127. if (status) {
  128. // i. Let varNames be envRec.[[VarNames]].
  129. // ii. If N is an element of varNames, remove that element from the varNames.
  130. m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
  131. }
  132. // c. Return status.
  133. return status;
  134. }
  135. // 7. Return true.
  136. return true;
  137. }
  138. // 9.1.1.4.12 HasVarDeclaration ( N ), https://tc39.es/ecma262/#sec-hasvardeclaration
  139. bool GlobalEnvironment::has_var_declaration(FlyString const& name) const
  140. {
  141. // 1. Let varDeclaredNames be envRec.[[VarNames]].
  142. // 2. If varDeclaredNames contains N, return true.
  143. // 3. Return false.
  144. return m_var_names.contains_slow(name);
  145. }
  146. // 9.1.1.4.13 HasLexicalDeclaration ( N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
  147. bool GlobalEnvironment::has_lexical_declaration(FlyString const& name) const
  148. {
  149. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  150. // 2. Return ! DclRec.HasBinding(N).
  151. return MUST(m_declarative_record->has_binding(name));
  152. }
  153. // 9.1.1.4.14 HasRestrictedGlobalProperty ( N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
  154. ThrowCompletionOr<bool> GlobalEnvironment::has_restricted_global_property(FlyString const& name) const
  155. {
  156. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  157. // 2. Let globalObject be ObjRec.[[BindingObject]].
  158. auto& global_object = m_object_record->binding_object();
  159. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  160. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  161. // 4. If existingProp is undefined, return false.
  162. if (!existing_prop.has_value())
  163. return false;
  164. // 5. If existingProp.[[Configurable]] is true, return false.
  165. if (*existing_prop->configurable)
  166. return false;
  167. // 6. Return true.
  168. return true;
  169. }
  170. // 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
  171. ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_var(FlyString const& name) const
  172. {
  173. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  174. // 2. Let globalObject be ObjRec.[[BindingObject]].
  175. auto& global_object = m_object_record->binding_object();
  176. // 3. Let hasProperty be ? HasOwnProperty(globalObject, N).
  177. bool has_property = TRY(global_object.has_own_property(name));
  178. // 4. If hasProperty is true, return true.
  179. if (has_property)
  180. return true;
  181. // 5. Return ? IsExtensible(globalObject).
  182. return global_object.is_extensible();
  183. }
  184. // 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
  185. ThrowCompletionOr<bool> GlobalEnvironment::can_declare_global_function(FlyString const& name) const
  186. {
  187. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  188. // 2. Let globalObject be ObjRec.[[BindingObject]].
  189. auto& global_object = m_object_record->binding_object();
  190. // 3. Let existingProp be ? globalObject.[[GetOwnProperty]](N).
  191. auto existing_prop = TRY(global_object.internal_get_own_property(name));
  192. // 4. If existingProp is undefined, return ? IsExtensible(globalObject).
  193. if (!existing_prop.has_value())
  194. return TRY(global_object.is_extensible());
  195. // 5. If existingProp.[[Configurable]] is true, return true.
  196. if (*existing_prop->configurable)
  197. return true;
  198. // 6. If IsDataDescriptor(existingProp) is true and existingProp has attribute values { [[Writable]]: true, [[Enumerable]]: true }, return true.
  199. if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
  200. return true;
  201. // 7. Return false.
  202. return false;
  203. }
  204. // 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
  205. ThrowCompletionOr<void> GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted)
  206. {
  207. // 1. Let ObjRec be envRec.[[ObjectRecord]].
  208. // 2. Let globalObject be ObjRec.[[BindingObject]].
  209. auto& global_object = verify_cast<GlobalObject>(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(global_object, name, can_be_deleted));
  218. // b. Perform ? ObjRec.InitializeBinding(N, undefined).
  219. TRY(m_object_record->initialize_binding(global_object, 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. }