GlobalEnvironment.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/DeclarativeEnvironment.h>
  9. #include <LibJS/Runtime/GlobalEnvironment.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/ObjectEnvironment.h>
  12. namespace JS {
  13. // 9.1.2.5 NewGlobalEnvironment ( G, thisValue ), https://tc39.es/ecma262/#sec-newglobalenvironment
  14. GlobalEnvironment::GlobalEnvironment(GlobalObject& global_object, Object& this_value)
  15. : Environment(nullptr)
  16. , m_global_this_value(&this_value)
  17. {
  18. m_object_record = global_object.heap().allocate<ObjectEnvironment>(global_object, global_object, ObjectEnvironment::IsWithEnvironment::No, nullptr);
  19. m_declarative_record = global_object.heap().allocate<DeclarativeEnvironment>(global_object);
  20. }
  21. void GlobalEnvironment::visit_edges(Cell::Visitor& visitor)
  22. {
  23. Base::visit_edges(visitor);
  24. visitor.visit(m_object_record);
  25. visitor.visit(m_global_this_value);
  26. visitor.visit(m_declarative_record);
  27. }
  28. Value GlobalEnvironment::get_this_binding(GlobalObject&) const
  29. {
  30. return m_global_this_value;
  31. }
  32. // 9.1.1.4.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-hasbinding-n
  33. bool GlobalEnvironment::has_binding(FlyString const& name) const
  34. {
  35. if (m_declarative_record->has_binding(name))
  36. return true;
  37. return m_object_record->has_binding(name);
  38. }
  39. // 9.1.1.4.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-global-environment-records-createmutablebinding-n-d
  40. void GlobalEnvironment::create_mutable_binding(GlobalObject& global_object, FlyString const& name, bool can_be_deleted)
  41. {
  42. if (m_declarative_record->has_binding(name)) {
  43. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  44. return;
  45. }
  46. m_declarative_record->create_mutable_binding(global_object, name, can_be_deleted);
  47. }
  48. // 9.1.1.4.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-createimmutablebinding-n-s
  49. void GlobalEnvironment::create_immutable_binding(GlobalObject& global_object, FlyString const& name, bool strict)
  50. {
  51. if (m_declarative_record->has_binding(name)) {
  52. global_object.vm().throw_exception<TypeError>(global_object, ErrorType::FixmeAddAnErrorString);
  53. return;
  54. }
  55. m_declarative_record->create_immutable_binding(global_object, name, strict);
  56. }
  57. // 9.1.1.4.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-global-environment-records-initializebinding-n-v
  58. void GlobalEnvironment::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
  59. {
  60. if (m_declarative_record->has_binding(name)) {
  61. m_declarative_record->initialize_binding(global_object, name, value);
  62. return;
  63. }
  64. m_object_record->initialize_binding(global_object, name, value);
  65. }
  66. // 9.1.1.4.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-global-environment-records-setmutablebinding-n-v-s
  67. void GlobalEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  68. {
  69. if (m_declarative_record->has_binding(name)) {
  70. m_declarative_record->set_mutable_binding(global_object, name, value, strict);
  71. return;
  72. }
  73. m_object_record->set_mutable_binding(global_object, name, value, strict);
  74. }
  75. // 9.1.1.4.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-global-environment-records-getbindingvalue-n-s
  76. Value GlobalEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  77. {
  78. if (m_declarative_record->has_binding(name))
  79. return m_declarative_record->get_binding_value(global_object, name, strict);
  80. return m_object_record->get_binding_value(global_object, name, strict);
  81. }
  82. // 9.1.1.4.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-global-environment-records-deletebinding-n
  83. bool GlobalEnvironment::delete_binding(GlobalObject& global_object, FlyString const& name)
  84. {
  85. if (m_declarative_record->has_binding(name))
  86. return m_declarative_record->delete_binding(global_object, name);
  87. bool existing_prop = m_object_record->binding_object().has_own_property(name);
  88. if (existing_prop) {
  89. bool status = m_object_record->delete_binding(global_object, name);
  90. if (status) {
  91. m_var_names.remove_all_matching([&](auto& entry) { return entry == name; });
  92. }
  93. return status;
  94. }
  95. return true;
  96. }
  97. // 9.1.1.4.12 HasVarDeclaration ( N ), https://tc39.es/ecma262/#sec-hasvardeclaration
  98. bool GlobalEnvironment::has_var_declaration(FlyString const& name) const
  99. {
  100. return m_var_names.contains_slow(name);
  101. }
  102. // 9.1.1.4.13 HasLexicalDeclaration ( N ), https://tc39.es/ecma262/#sec-haslexicaldeclaration
  103. bool GlobalEnvironment::has_lexical_declaration(FlyString const& name) const
  104. {
  105. return m_declarative_record->has_binding(name);
  106. }
  107. // 9.1.1.4.14 HasRestrictedGlobalProperty ( N ), https://tc39.es/ecma262/#sec-hasrestrictedglobalproperty
  108. bool GlobalEnvironment::has_restricted_global_property(FlyString const& name) const
  109. {
  110. auto& global_object = m_object_record->binding_object();
  111. auto existing_prop = TRY_OR_DISCARD(global_object.internal_get_own_property(name));
  112. if (!existing_prop.has_value())
  113. return false;
  114. if (*existing_prop->configurable)
  115. return false;
  116. return true;
  117. }
  118. // 9.1.1.4.15 CanDeclareGlobalVar ( N ), https://tc39.es/ecma262/#sec-candeclareglobalvar
  119. bool GlobalEnvironment::can_declare_global_var(FlyString const& name) const
  120. {
  121. auto& vm = this->vm();
  122. auto& global_object = m_object_record->binding_object();
  123. bool has_property = global_object.has_own_property(name);
  124. if (vm.exception())
  125. return {};
  126. if (has_property)
  127. return true;
  128. return TRY_OR_DISCARD(global_object.is_extensible());
  129. }
  130. // 9.1.1.4.16 CanDeclareGlobalFunction ( N ), https://tc39.es/ecma262/#sec-candeclareglobalfunction
  131. bool GlobalEnvironment::can_declare_global_function(FlyString const& name) const
  132. {
  133. auto& global_object = m_object_record->binding_object();
  134. auto existing_prop = TRY_OR_DISCARD(global_object.internal_get_own_property(name));
  135. if (!existing_prop.has_value())
  136. return TRY_OR_DISCARD(global_object.is_extensible());
  137. if (*existing_prop->configurable)
  138. return true;
  139. if (existing_prop->is_data_descriptor() && *existing_prop->writable && *existing_prop->enumerable)
  140. return true;
  141. return false;
  142. }
  143. // 9.1.1.4.17 CreateGlobalVarBinding ( N, D ), https://tc39.es/ecma262/#sec-createglobalvarbinding
  144. void GlobalEnvironment::create_global_var_binding(FlyString const& name, bool can_be_deleted)
  145. {
  146. auto& vm = this->vm();
  147. auto& global_object = m_object_record->binding_object();
  148. bool has_property = global_object.has_own_property(name);
  149. if (vm.exception())
  150. return;
  151. auto extensible_or_error = global_object.is_extensible();
  152. if (extensible_or_error.is_error())
  153. return;
  154. auto extensible = extensible_or_error.release_value();
  155. if (!has_property && extensible) {
  156. m_object_record->create_mutable_binding(m_object_record->global_object(), name, can_be_deleted);
  157. if (vm.exception())
  158. return;
  159. m_object_record->initialize_binding(m_object_record->global_object(), name, js_undefined());
  160. if (vm.exception())
  161. return;
  162. }
  163. if (!m_var_names.contains_slow(name))
  164. m_var_names.append(name);
  165. }
  166. // 9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D ), https://tc39.es/ecma262/#sec-createglobalfunctionbinding
  167. void GlobalEnvironment::create_global_function_binding(FlyString const& name, Value value, bool can_be_deleted)
  168. {
  169. auto& global_object = m_object_record->binding_object();
  170. auto existing_prop_or_error = global_object.internal_get_own_property(name);
  171. if (existing_prop_or_error.is_error())
  172. return;
  173. auto existing_prop = existing_prop_or_error.release_value();
  174. PropertyDescriptor desc;
  175. if (!existing_prop.has_value() || *existing_prop->configurable)
  176. desc = { .value = value, .writable = true, .enumerable = true, .configurable = can_be_deleted };
  177. else
  178. desc = { .value = value };
  179. auto result_or_error = global_object.define_property_or_throw(name, desc);
  180. if (result_or_error.is_error())
  181. return;
  182. result_or_error = global_object.set(name, value, Object::ShouldThrowExceptions::Yes);
  183. if (result_or_error.is_error())
  184. return;
  185. if (!m_var_names.contains_slow(name))
  186. m_var_names.append(name);
  187. }
  188. }