ObjectEnvironment.cpp 4.8 KB

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