ObjectEnvironment.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. ThrowCompletionOr<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. ThrowCompletionOr<void> ObjectEnvironment::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
  64. {
  65. // 1. Return ? envRec.SetMutableBinding(N, V, false).
  66. return set_mutable_binding(global_object, name, value, false);
  67. }
  68. // 9.1.1.2.5 SetMutableBinding ( N, V, S ), https://tc39.es/ecma262/#sec-object-environment-records-setmutablebinding-n-v-s
  69. ThrowCompletionOr<void> ObjectEnvironment::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  70. {
  71. auto& vm = this->vm();
  72. // 1. Let bindingObject be envRec.[[BindingObject]].
  73. // 2. Let stillExists be ? HasProperty(bindingObject, N).
  74. auto still_exists = TRY(m_binding_object.has_property(name));
  75. // 3. If stillExists is false and S is true, throw a ReferenceError exception.
  76. if (!still_exists && strict)
  77. return vm.throw_completion<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  78. // 4. Return ? Set(bindingObject, N, V, S).
  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. // Return the initial error instead of masking it with the new error
  84. if (property_or_error.is_error())
  85. return result_or_error.release_error();
  86. auto property = property_or_error.release_value();
  87. if (property.has_value() && !property->writable.value_or(true)) {
  88. vm.clear_exception();
  89. return vm.throw_completion<TypeError>(global_object, ErrorType::DescWriteNonWritable, name);
  90. }
  91. }
  92. if (result_or_error.is_error())
  93. return result_or_error.release_error();
  94. return {};
  95. }
  96. // 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
  97. ThrowCompletionOr<Value> ObjectEnvironment::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  98. {
  99. auto& vm = this->vm();
  100. // 1. Let bindingObject be envRec.[[BindingObject]].
  101. // 2. Let value be ? HasProperty(bindingObject, N).
  102. auto value = TRY(m_binding_object.has_property(name));
  103. // 3. If value is false, then
  104. if (!value) {
  105. // a. If S is false, return the value undefined; otherwise throw a ReferenceError exception.
  106. if (!strict)
  107. return js_undefined();
  108. return vm.throw_completion<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  109. }
  110. // 4. Return ? Get(bindingObject, N).
  111. return m_binding_object.get(name);
  112. }
  113. // 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
  114. ThrowCompletionOr<bool> ObjectEnvironment::delete_binding(GlobalObject&, FlyString const& name)
  115. {
  116. // 1. Let bindingObject be envRec.[[BindingObject]].
  117. // 2. Return ? bindingObject.[[Delete]](N).
  118. return m_binding_object.internal_delete(name);
  119. }
  120. }