GlobalEnvironment.cpp 13 KB

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