ObjectEnvironment.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. JS_DEFINE_ALLOCATOR(ObjectEnvironment);
  12. ObjectEnvironment::ObjectEnvironment(Object& binding_object, IsWithEnvironment is_with_environment, Environment* outer_environment)
  13. : Environment(outer_environment)
  14. , m_binding_object(binding_object)
  15. , m_with_environment(is_with_environment == IsWithEnvironment::Yes)
  16. {
  17. }
  18. void ObjectEnvironment::visit_edges(Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(m_binding_object);
  22. }
  23. // 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n
  24. ThrowCompletionOr<bool> ObjectEnvironment::has_binding(DeprecatedFlyString const& name, Optional<size_t>*) const
  25. {
  26. auto& vm = this->vm();
  27. // 1. Let bindingObject be envRec.[[BindingObject]].
  28. // 2. Let foundBinding be ? HasProperty(bindingObject, N).
  29. bool found_binding = TRY(m_binding_object->has_property(name));
  30. // 3. If foundBinding is false, return false.
  31. if (!found_binding)
  32. return false;
  33. // 4. If envRec.[[IsWithEnvironment]] is false, return true.
  34. if (!m_with_environment)
  35. return true;
  36. // 5. Let unscopables be ? Get(bindingObject, @@unscopables).
  37. auto unscopables = TRY(m_binding_object->get(vm.well_known_symbol_unscopables()));
  38. // 6. If Type(unscopables) is Object, then
  39. if (unscopables.is_object()) {
  40. // a. Let blocked be ToBoolean(? Get(unscopables, N)).
  41. auto blocked = TRY(unscopables.as_object().get(name)).to_boolean();
  42. // b. If blocked is true, return false.
  43. if (blocked)
  44. return false;
  45. }
  46. // 7. Return true.
  47. return true;
  48. }
  49. // 9.1.1.2.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-object-environment-records-createmutablebinding-n-d
  50. ThrowCompletionOr<void> ObjectEnvironment::create_mutable_binding(VM&, DeprecatedFlyString const& name, bool can_be_deleted)
  51. {
  52. // 1. Let bindingObject be envRec.[[BindingObject]].
  53. // 2. Perform ? DefinePropertyOrThrow(bindingObject, N, PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }).
  54. TRY(m_binding_object->define_property_or_throw(name, { .value = js_undefined(), .writable = true, .enumerable = true, .configurable = can_be_deleted }));
  55. // 3. Return unused.
  56. return {};
  57. }
  58. // 9.1.1.2.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-createimmutablebinding-n-s
  59. ThrowCompletionOr<void> ObjectEnvironment::create_immutable_binding(VM&, DeprecatedFlyString const&, bool)
  60. {
  61. // "The CreateImmutableBinding concrete method of an object Environment Record is never used within this specification."
  62. VERIFY_NOT_REACHED();
  63. }
  64. // 9.1.1.2.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-object-environment-records-initializebinding-n-v
  65. ThrowCompletionOr<void> ObjectEnvironment::initialize_binding(VM& vm, DeprecatedFlyString const& name, Value value, Environment::InitializeBindingHint hint)
  66. {
  67. // 1. Assert: hint is normal.
  68. VERIFY(hint == Environment::InitializeBindingHint::Normal);
  69. // 2. Perform ? envRec.SetMutableBinding(N, V, false).
  70. TRY(set_mutable_binding(vm, name, value, false));
  71. // 2. Return unused.
  72. return {};
  73. }
  74. // 9.1.1.2.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-object-environment-records-setmutablebinding-n-v-s
  75. ThrowCompletionOr<void> ObjectEnvironment::set_mutable_binding(VM&, DeprecatedFlyString const& name, Value value, bool strict)
  76. {
  77. auto& vm = this->vm();
  78. // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check since we only use that
  79. // information to throw errors in strict mode.
  80. // We can't do this for with environments, since it would be observable (e.g via a Proxy)
  81. // FIXME: I think we could combine HasProperty and Set in strict mode if Set would return a bit more failure information.
  82. if (!m_with_environment && !strict)
  83. return m_binding_object->set(name, value, Object::ShouldThrowExceptions::No);
  84. // 1. Let bindingObject be envRec.[[BindingObject]].
  85. // 2. Let stillExists be ? HasProperty(bindingObject, N).
  86. auto still_exists = TRY(m_binding_object->has_property(name));
  87. // 3. If stillExists is false and S is true, throw a ReferenceError exception.
  88. if (!still_exists && strict)
  89. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  90. // 4. Perform ? Set(bindingObject, N, V, S).
  91. auto result_or_error = m_binding_object->set(name, value, strict ? Object::ShouldThrowExceptions::Yes : Object::ShouldThrowExceptions::No);
  92. // Note: Nothing like this in the spec, this is here to produce nicer errors instead of the generic one thrown by Object::set().
  93. if (result_or_error.is_error() && strict) {
  94. auto property_or_error = m_binding_object->internal_get_own_property(name);
  95. // Return the initial error instead of masking it with the new error
  96. if (property_or_error.is_error())
  97. return result_or_error.release_error();
  98. auto property = property_or_error.release_value();
  99. if (property.has_value() && !property->writable.value_or(true)) {
  100. return vm.throw_completion<TypeError>(ErrorType::DescWriteNonWritable, name);
  101. }
  102. }
  103. if (result_or_error.is_error())
  104. return result_or_error.release_error();
  105. // 5. Return unused.
  106. return {};
  107. }
  108. // 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
  109. ThrowCompletionOr<Value> ObjectEnvironment::get_binding_value(VM&, DeprecatedFlyString const& name, bool strict)
  110. {
  111. auto& vm = this->vm();
  112. // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check
  113. // since Get will return undefined for missing properties anyway. So we take advantage of this
  114. // to avoid doing both HasProperty and Get.
  115. // We can't do this for with environments, since it would be observable (e.g via a Proxy)
  116. // FIXME: We could combine HasProperty and Get in non-strict mode if Get would return a bit more failure information.
  117. if (!m_with_environment && !strict)
  118. return m_binding_object->get(name);
  119. // 1. Let bindingObject be envRec.[[BindingObject]].
  120. // 2. Let value be ? HasProperty(bindingObject, N).
  121. auto value = TRY(m_binding_object->has_property(name));
  122. // 3. If value is false, then
  123. if (!value) {
  124. // a. If S is false, return undefined; otherwise throw a ReferenceError exception.
  125. if (!strict)
  126. return js_undefined();
  127. return vm.throw_completion<ReferenceError>(ErrorType::UnknownIdentifier, name);
  128. }
  129. // 4. Return ? Get(bindingObject, N).
  130. return m_binding_object->get(name);
  131. }
  132. // 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
  133. ThrowCompletionOr<bool> ObjectEnvironment::delete_binding(VM&, DeprecatedFlyString const& name)
  134. {
  135. // 1. Let bindingObject be envRec.[[BindingObject]].
  136. // 2. Return ? bindingObject.[[Delete]](N).
  137. return m_binding_object->internal_delete(name);
  138. }
  139. }