GlobalEnvironment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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<ObjectEnvironment>(global_object, global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
  20. m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>(global_object);
  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 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. return m_declarative_record->set_mutable_binding(global_object, name, value, strict);
  88. }
  89. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  90. // 4. Return ? ObjRec.SetMutableBinding(N, V, S).
  91. return m_object_record->set_mutable_binding(global_object, name, value, strict);
  92. }
  93. // 9.1.1.4.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s
  94. ThrowCompletionOr<Value> GlobalEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  95. {
  96. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  97. // 2. If DclRec.HasBinding(N) is true, then
  98. if (MUST(m_declarative_record->has_binding(name))) {
  99. // a. Return DclRec.GetBindingValue(N, S).
  100. return m_declarative_record->get_binding_value(global_object, name, strict);
  101. }
  102. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  103. // 4. Return ? ObjRec.GetBindingValue(N, S).
  104. return m_object_record->get_binding_value(global_object, name, strict);
  105. }
  106. // 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
  107. ThrowCompletionOr<bool> GlobalEnvironment::delete_binding(GlobalObject& global_object, FlyString const& name)
  108. {
  109. // 1. Let DclRec be envRec.[[DeclarativeRecord]].
  110. // 2. If DclRec.HasBinding(N) is true, then
  111. if (MUST(m_declarative_record->has_binding(name))) {
  112. // a. Return DclRec.DeleteBinding(N).
  113. return m_declarative_record->delete_binding(global_object, name);
  114. }
  115. // 3. Let ObjRec be envRec.[[ObjectRecord]].
  116. // 4. Let globalObject be ObjRec.[[BindingObject]].
  117. // 5. Let existingProp be ? HasOwnProperty(globalObject, N).
  118. bool existing_prop = TRY(m_object_record->binding_object().has_own_property(name));
  119. // 6. If existingProp is true, then
  120. if (existing_prop) {
  121. // a. Let status be ? ObjRec.DeleteBinding(N).
  122. bool status = TRY(m_object_record->delete_binding(global_object, name));
  123. // b. If status is true, then
  124. if (status) {
  125. // i. Let varNames be envRec.[[VarNames]].
  126. // ii. If N is an element of varNames, remove that element from the varNames.
  127. m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
  128. }
  129. // c. Return status.
  130. return status;
  131. }
  132. // 7. Return true.
  133. return true;
  134. }
  135. // 9.1.1.4.12 HasVarDeclaration ( N ), https://tc39.es/ecma262/#sec-hasvardeclaration
  136. bool GlobalEnvironment::has_var_declaration(FlyString const& name) const
  137. {
  138. return m_var_names.contains_slow(name);
  139. }
  140. // 9.1.1.4.13 HasLexicalDeclaration ( N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
  141. bool GlobalEnvironment::has_lexical_declaration(FlyString const& name) const
  142. {
  143. return MUST(m_declarative_record->has_binding(name));
  144. }
  145. // 9.1.1.4.14 HasRestrictedGlobalProperty ( N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
  146. bool GlobalEnvironment::has_restricted_global_property(FlyString const& name) const
  147. {
  148. auto& global_object = m_object_record->binding_object();
  149. auto existing_prop = TRY_OR_DISCARD(global_object.internal_get_own_property(name));
  150. if (!existing_prop.has_value())
  151. return false;
  152. if (*existing_prop->configurable)
  153. return false;
  154. return true;
  155. }
  156. // 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
  157. bool GlobalEnvironment::can_declare_global_var(FlyString const& name) const
  158. {
  159. auto& global_object = m_object_record->binding_object();
  160. bool has_property = TRY_OR_DISCARD(global_object.has_own_property(name));
  161. if (has_property)
  162. return true;
  163. return TRY_OR_DISCARD(global_object.is_extensible());
  164. }
  165. // 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
  166. bool GlobalEnvironment::can_declare_global_function(FlyString const& name) const
  167. {
  168. auto& global_object = m_object_record->binding_object();
  169. auto existing_prop = TRY_OR_DISCARD(global_object.internal_get_own_property(name));
  170. if (!existing_prop.has_value())
  171. return TRY_OR_DISCARD(global_object.is_extensible());
  172. if (*existing_prop->configurable)
  173. return true;
  174. if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
  175. return true;
  176. return false;
  177. }
  178. // 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
  179. void GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted)
  180. {
  181. auto& global_object = m_object_record->binding_object();
  182. auto has_property_or_error = global_object.has_own_property(name);
  183. if (has_property_or_error.is_error())
  184. return;
  185. auto has_property = has_property_or_error.release_value();
  186. auto extensible_or_error = global_object.is_extensible();
  187. if (extensible_or_error.is_error())
  188. return;
  189. auto extensible = extensible_or_error.release_value();
  190. if (!has_property && extensible) {
  191. auto result = m_object_record->create_mutable_binding(m_object_record->global_object(), name, can_be_deleted);
  192. if (result.is_error())
  193. return;
  194. result = m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined());
  195. if (result.is_error())
  196. return;
  197. }
  198. if (!m_var_names.contains_slow(name))
  199. m_var_names.append(name);
  200. }
  201. // 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  202. void GlobalEnvironment::create_global_function_binding(FlyString const& name, Value value, bool can_be_deleted)
  203. {
  204. auto& global_object = m_object_record->binding_object();
  205. auto existing_prop_or_error = global_object.internal_get_own_property(name);
  206. if (existing_prop_or_error.is_error())
  207. return;
  208. auto existing_prop = existing_prop_or_error.release_value();
  209. PropertyDescriptor desc;
  210. if (!existing_prop.has_value() || *existing_prop->configurable)
  211. desc = { .value = value, .writable = true, .enumerable = true, .configurable = can_be_deleted };
  212. else
  213. desc = { .value = value };
  214. auto result_or_error = global_object.define_property_or_throw(name, desc);
  215. if (result_or_error.is_error())
  216. return;
  217. result_or_error = global_object.set(name, value, Object::ShouldThrowExceptions::Yes);
  218. if (result_or_error.is_error())
  219. return;
  220. if (!m_var_names.contains_slow(name))
  221. m_var_names.append(name);
  222. }
  223. }