ObjectEnvironment.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/ObjectEnvironment.h>
  9. #include <LibJS/Runtime/ValueInlines.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(DeprecatedFlyString 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(VM&, DeprecatedFlyString const& name, bool can_be_deleted)
  50. {
  51. // 1. Let bindingObject be envRec.[[BindingObject]].
  52. // 2. Perform ? 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. // 3. Return unused.
  55. return {};
  56. }
  57. // 9.1.1.2.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-createimmutablebinding-n-s
  58. ThrowCompletionOr<void> ObjectEnvironment::create_immutable_binding(VM&, DeprecatedFlyString const&, bool)
  59. {
  60. // "The CreateImmutableBinding concrete method of an object Environment Record is never used within this specification."
  61. VERIFY_NOT_REACHED();
  62. }
  63. // 9.1.1.2.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-object-environment-records-initializebinding-n-v
  64. ThrowCompletionOr<void> ObjectEnvironment::initialize_binding(VM& vm, DeprecatedFlyString const& name, Value value, Environment::InitializeBindingHint hint)
  65. {
  66. // 1. Assert: hint is normal.
  67. VERIFY(hint == Environment::InitializeBindingHint::Normal);
  68. // 2. Perform ? envRec.SetMutableBinding(N, V, false).
  69. TRY(set_mutable_binding(vm, name, value, false));
  70. // 2. Return unused.
  71. return {};
  72. }
  73. // 9.1.1.2.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-object-environment-records-setmutablebinding-n-v-s
  74. ThrowCompletionOr<void> ObjectEnvironment::set_mutable_binding(VM&, DeprecatedFlyString const& name, Value value, bool strict)
  75. {
  76. auto& vm = this->vm();
  77. // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check since we only use that
  78. // information to throw errors in strict mode.
  79. // We can't do this for with environments, since it would be observable (e.g via a Proxy)
  80. // FIXME: I think we could combine HasProperty and Set in strict mode if Set would return a bit more failure information.
  81. if (!m_with_environment && !strict)
  82. return m_binding_object->set(name, value, Object::ShouldThrowExceptions::No);
  83. // 1. Let bindingObject be envRec.[[BindingObject]].
  84. // 2. Let stillExists be ? HasProperty(bindingObject, N).
  85. auto still_exists = TRY(m_binding_object->has_property(name));
  86. // 3. If stillExists is false and S is true, throw a ReferenceError exception.
  87. if (!still_exists && strict)
  88. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  89. // 4. Perform ? Set(bindingObject, N, V, S).
  90. auto result_or_error = m_binding_object->set(name, value, strict ? Object::ShouldThrowExceptions::Yes : Object::ShouldThrowExceptions::No);
  91. // Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
  92. if (result_or_error.is_error() && strict) {
  93. auto property_or_error = m_binding_object->internal_get_own_property(name);
  94. // Return the initial error instead of masking it with the new error
  95. if (property_or_error.is_error())
  96. return result_or_error.release_error();
  97. auto property = property_or_error.release_value();
  98. if (property.has_value() && !property->writable.value_or(true)) {
  99. return vm.throw_completion<TypeError>(ErrorType::DescWriteNonWritable, name);
  100. }
  101. }
  102. if (result_or_error.is_error())
  103. return result_or_error.release_error();
  104. // 5. Return unused.
  105. return {};
  106. }
  107. // 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
  108. ThrowCompletionOr<Value> ObjectEnvironment::get_binding_value(VM&, DeprecatedFlyString const& name, bool strict)
  109. {
  110. auto& vm = this->vm();
  111. // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check
  112. // since Get will return undefined for missing properties anyway. So we take advantage of this
  113. // to avoid doing both HasProperty and Get.
  114. // We can't do this for with environments, since it would be observable (e.g via a Proxy)
  115. // FIXME: We could combine HasProperty and Get in non-strict mode if Get would return a bit more failure information.
  116. if (!m_with_environment && !strict)
  117. return m_binding_object->get(name);
  118. // 1. Let bindingObject be envRec.[[BindingObject]].
  119. // 2. Let value be ? HasProperty(bindingObject, N).
  120. auto value = TRY(m_binding_object->has_property(name));
  121. // 3. If value is false, then
  122. if (!value) {
  123. // a. If S is false, return undefined; otherwise throw a ReferenceError exception.
  124. if (!strict)
  125. return js_undefined();
  126. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  127. }
  128. // 4. Return ? Get(bindingObject, N).
  129. return m_binding_object->get(name);
  130. }
  131. // 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
  132. ThrowCompletionOr<bool> ObjectEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
  133. {
  134. // 1. Let bindingObject be envRec.[[BindingObject]].
  135. // 2. Return ? bindingObject.[[Delete]](N).
  136. return m_binding_object->internal_delete(name);
  137. }
  138. }