ObjectEnvironmentRecord.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/ObjectEnvironmentRecord.h>
  9. namespace JS {
  10. ObjectEnvironmentRecord::ObjectEnvironmentRecord(Object& object, EnvironmentRecord* parent_scope)
  11. : EnvironmentRecord(parent_scope)
  12. , m_object(object)
  13. {
  14. }
  15. void ObjectEnvironmentRecord::visit_edges(Cell::Visitor& visitor)
  16. {
  17. Base::visit_edges(visitor);
  18. visitor.visit(&m_object);
  19. }
  20. Optional<Variable> ObjectEnvironmentRecord::get_from_environment_record(FlyString const& name) const
  21. {
  22. auto value = m_object.get(name);
  23. if (value.is_empty())
  24. return {};
  25. return Variable { value, DeclarationKind::Var };
  26. }
  27. void ObjectEnvironmentRecord::put_into_environment_record(FlyString const& name, Variable variable)
  28. {
  29. m_object.put(name, variable.value);
  30. }
  31. bool ObjectEnvironmentRecord::delete_from_environment_record(FlyString const& name)
  32. {
  33. return m_object.delete_property(name);
  34. }
  35. // 9.1.1.2.1 HasBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-hasbinding-n
  36. bool ObjectEnvironmentRecord::has_binding(FlyString const& name) const
  37. {
  38. bool found_binding = m_object.has_property(name);
  39. if (!found_binding)
  40. return false;
  41. // FIXME: Implement the rest of this operation.
  42. return true;
  43. }
  44. // 9.1.1.2.2 CreateMutableBinding ( N, D ), https://tc39.es/ecma262/#sec-object-environment-records-createmutablebinding-n-d
  45. void ObjectEnvironmentRecord::create_mutable_binding(GlobalObject&, FlyString const& name, bool can_be_deleted)
  46. {
  47. PropertyAttributes attributes;
  48. attributes.set_enumerable();
  49. attributes.set_has_enumerable();
  50. attributes.set_writable();
  51. attributes.set_has_writable();
  52. attributes.set_has_configurable();
  53. if (can_be_deleted)
  54. attributes.set_configurable();
  55. m_object.define_property(name, js_undefined(), attributes, true);
  56. }
  57. // 9.1.1.2.3 CreateImmutableBinding ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-createimmutablebinding-n-s
  58. void ObjectEnvironmentRecord::create_immutable_binding(GlobalObject&, FlyString const&, bool)
  59. {
  60. // "The CreateImmutableBinding concrete method of an object Environment Record is never used within this specification."
  61. VERIFY_NOT_REACHED();
  62. }
  63. // 9.1.1.2.4 InitializeBinding ( N, V ), https://tc39.es/ecma262/#sec-object-environment-records-initializebinding-n-v
  64. void ObjectEnvironmentRecord::initialize_binding(GlobalObject& global_object, FlyString const& name, Value value)
  65. {
  66. 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. void ObjectEnvironmentRecord::set_mutable_binding(GlobalObject& global_object, FlyString const& name, Value value, bool strict)
  70. {
  71. bool still_exists = m_object.has_property(name);
  72. if (!still_exists && strict) {
  73. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  74. return;
  75. }
  76. // FIXME: This should use the Set abstract operation.
  77. // FIXME: Set returns a bool, so this may need to return a bool as well.
  78. m_object.put(name, value);
  79. }
  80. // 9.1.1.2.6 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-object-environment-records-getbindingvalue-n-s
  81. Value ObjectEnvironmentRecord::get_binding_value(GlobalObject& global_object, FlyString const& name, bool strict)
  82. {
  83. if (!m_object.has_property(name)) {
  84. if (!strict)
  85. return js_undefined();
  86. global_object.vm().throw_exception<ReferenceError>(global_object, ErrorType::UnknownIdentifier, name);
  87. return {};
  88. }
  89. // FIXME: This should use the Get abstract operation.
  90. return m_object.get(name);
  91. }
  92. // 9.1.1.2.7 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-object-environment-records-deletebinding-n
  93. bool ObjectEnvironmentRecord::delete_binding(GlobalObject&, FlyString const& name)
  94. {
  95. return m_object.delete_property(name);
  96. }
  97. }