ObjectEnvironment.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n
  23. ThrowCompletionOr<bool> ObjectEnvironment::has_binding(FlyString const& name, Optional<size_t>*) const
  24. {
  25. auto& vm = this->vm();
  26. // 1. Let bindingObject be envRec.[[BindingObject]].
  27. // 2. Let foundBinding be ? HasProperty(bindingObject, N).
  28. bool found_binding = TRY(m_binding_object.has_property(name));
  29. // 3. If foundBinding is false, return false.
  30. if (!found_binding)
  31. return false;
  32. // 4. If envRec.[[IsWithEnvironment]] is false, return true.
  33. if (!m_with_environment)
  34. return true;
  35. // 5. Let unscopables be ? Get(bindingObject, @@unscopables).
  36. auto unscopables = TRY(m_binding_object.get(*vm.well_known_symbol_unscopables()));
  37. // 6. If Type(unscopables) is Object, then
  38. if (unscopables.is_object()) {
  39. // a. Let blocked be ! ToBoolean(? Get(unscopables, N)).
  40. auto blocked = TRY(unscopables.as_object().get(name)).to_boolean();
  41. // b. If blocked is true, return false.
  42. if (blocked)
  43. return false;
  44. }
  45. // 7. Return true.
  46. return true;
  47. }
  48. // 9.1.1.2.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-object-environment-records-createmutablebinding-n-d
  49. ThrowCompletionOr<void> ObjectEnvironment::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted)
  50. {
  51. // 1. Let bindingObject be envRec.[[BindingObject]].
  52. // 2. Return ? DefinePropertyOrThrow(bindingObject, N, PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }).
  53. TRY(m_binding_object.define_property_or_throw(name, { .value = js_undefined(), .writable = true, .enumerable = true, .configurable = can_be_deleted }));
  54. return {};
  55. }
  56. // 9.1.1.2.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-createimmutablebinding-n-s
  57. void ObjectEnvironment::create_immutable_binding(GlobalObject&, FlyString const&, bool)
  58. {
  59. // "The CreateImmutableBinding concrete method of an object Environment Record is never used within this specification."
  60. VERIFY_NOT_REACHED();
  61. }
  62. // 9.1.1.2.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-object-environment-records-initializebinding-n-v
  63. void ObjectEnvironment::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
  64. {
  65. set_mutable_binding(global_object, name, value, false);
  66. }
  67. // 9.1.1.2.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-object-environment-records-setmutablebinding-n-v-s
  68. void ObjectEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  69. {
  70. auto& vm = this->vm();
  71. auto still_exists_or_error = m_binding_object.has_property(name);
  72. if (still_exists_or_error.is_error())
  73. return;
  74. auto still_exists = still_exists_or_error.release_value();
  75. if (!still_exists && strict) {
  76. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  77. return;
  78. }
  79. auto result_or_error = m_binding_object.set(name, value, strict ? Object::ShouldThrowExceptions::Yes : Object::ShouldThrowExceptions::No);
  80. // Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
  81. if (result_or_error.is_error() && strict) {
  82. auto property_or_error = m_binding_object.internal_get_own_property(name);
  83. if (property_or_error.is_error())
  84. return;
  85. auto property = property_or_error.release_value();
  86. if (property.has_value() && !property->writable.value_or(true)) {
  87. vm.clear_exception();
  88. vm.throw_exception<TypeError>(global_object, ErrorType::DescWriteNonWritable, name);
  89. }
  90. }
  91. }
  92. // 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
  93. Value ObjectEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  94. {
  95. auto& vm = this->vm();
  96. auto value = TRY_OR_DISCARD(m_binding_object.has_property(name));
  97. if (!value) {
  98. if (!strict)
  99. return js_undefined();
  100. vm.throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  101. return {};
  102. }
  103. return TRY_OR_DISCARD(m_binding_object.get(name));
  104. }
  105. // 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
  106. bool ObjectEnvironment::delete_binding(GlobalObject&, FlyString const& name)
  107. {
  108. return TRY_OR_DISCARD(m_binding_object.internal_delete(name));
  109. }
  110. }