ObjectEnvironment.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/GlobalObject.h>
  9. #include <LibJS/Runtime/ObjectEnvironment.h>
  10. namespace JS {
  11. ObjectEnvironment::ObjectEnvironment(Object& binding_object, IsWithEnvironment is_with_environment, Environment* outer_environment)
  12. : Environment(outer_environment)
  13. , m_binding_object(binding_object)
  14. , m_with_environment(is_with_environment == IsWithEnvironment::Yes)
  15. {
  16. }
  17. void ObjectEnvironment::visit_edges(Cell::Visitor& visitor)
  18. {
  19. Base::visit_edges(visitor);
  20. visitor.visit(&m_binding_object);
  21. }
  22. Optional<Variable> ObjectEnvironment::get_from_environment(FlyString const& name) const
  23. {
  24. if (!m_binding_object.storage_has(name))
  25. return {};
  26. auto value = m_binding_object.get(name);
  27. return Variable { value, DeclarationKind::Var };
  28. }
  29. bool ObjectEnvironment::put_into_environment(FlyString const& name, Variable variable)
  30. {
  31. return m_binding_object.set(name, variable.value, Object::ShouldThrowExceptions::No);
  32. }
  33. bool ObjectEnvironment::delete_from_environment(FlyString const& name)
  34. {
  35. return m_binding_object.internal_delete(name);
  36. }
  37. // 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n
  38. bool ObjectEnvironment::has_binding(FlyString const& name) const
  39. {
  40. auto& vm = this->vm();
  41. bool found_binding = m_binding_object.has_property(name);
  42. if (vm.exception())
  43. return {};
  44. if (!found_binding)
  45. return false;
  46. if (!m_with_environment)
  47. return true;
  48. auto unscopables = m_binding_object.get(*vm.well_known_symbol_unscopables());
  49. if (vm.exception())
  50. return {};
  51. if (unscopables.is_object()) {
  52. auto blocked = unscopables.as_object().get(name);
  53. if (vm.exception())
  54. return {};
  55. if (blocked.to_boolean())
  56. return false;
  57. }
  58. return true;
  59. }
  60. // 9.1.1.2.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-object-environment-records-createmutablebinding-n-d
  61. void ObjectEnvironment::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted)
  62. {
  63. // 1. Let bindingObject be envRec.[[BindingObject]].
  64. // 2. Return ? DefinePropertyOrThrow(bindingObject, N, PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }).
  65. m_binding_object.define_property_or_throw(name, { .value = js_undefined(), .writable = true, .enumerable = true, .configurable = can_be_deleted });
  66. }
  67. // 9.1.1.2.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-createimmutablebinding-n-s
  68. void ObjectEnvironment::create_immutable_binding(GlobalObject&, FlyString const&, bool)
  69. {
  70. // "The CreateImmutableBinding concrete method of an object Environment Record is never used within this specification."
  71. VERIFY_NOT_REACHED();
  72. }
  73. // 9.1.1.2.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-object-environment-records-initializebinding-n-v
  74. void ObjectEnvironment::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
  75. {
  76. set_mutable_binding(global_object, name, value, false);
  77. }
  78. // 9.1.1.2.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-object-environment-records-setmutablebinding-n-v-s
  79. void ObjectEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  80. {
  81. auto& vm = this->vm();
  82. bool still_exists = m_binding_object.has_property(name);
  83. if (vm.exception())
  84. return;
  85. if (!still_exists && strict) {
  86. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  87. return;
  88. }
  89. auto result = m_binding_object.set(name, value, strict ? Object::ShouldThrowExceptions::Yes : Object::ShouldThrowExceptions::No);
  90. // Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
  91. if (!result && strict) {
  92. auto property = m_binding_object.internal_get_own_property(name);
  93. if (property.has_value() && !property->writable.value_or(true)) {
  94. vm.clear_exception();
  95. vm.throw_exception<TypeError>(global_object, ErrorType::DescWriteNonWritable, name);
  96. }
  97. }
  98. }
  99. // 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
  100. Value ObjectEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  101. {
  102. auto& vm = this->vm();
  103. auto value = m_binding_object.has_property(name);
  104. if (vm.exception())
  105. return {};
  106. if (!value) {
  107. if (!strict)
  108. return js_undefined();
  109. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  110. return {};
  111. }
  112. return m_binding_object.get(name);
  113. }
  114. // 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
  115. bool ObjectEnvironment::delete_binding(GlobalObject&, FlyString const& name)
  116. {
  117. return m_binding_object.internal_delete(name);
  118. }
  119. }