ObjectEnvironment.cpp 4.7 KB

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