ObjectEnvironment.cpp 7.3 KB

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