ArgumentsObject.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/ArgumentsObject.h>
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. namespace JS {
  10. ArgumentsObject::ArgumentsObject(GlobalObject& global_object, Environment& environment)
  11. : Object(*global_object.object_prototype())
  12. , m_environment(environment)
  13. {
  14. }
  15. void ArgumentsObject::initialize(GlobalObject& global_object)
  16. {
  17. Base::initialize(global_object);
  18. set_has_parameter_map();
  19. m_parameter_map = Object::create(global_object, nullptr);
  20. }
  21. ArgumentsObject::~ArgumentsObject()
  22. {
  23. }
  24. void ArgumentsObject::visit_edges(Cell::Visitor& visitor)
  25. {
  26. Base::visit_edges(visitor);
  27. visitor.visit(&m_environment);
  28. visitor.visit(m_parameter_map);
  29. }
  30. // 10.4.4.3 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-get-p-receiver
  31. ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyKey const& property_key, Value receiver) const
  32. {
  33. // 1. Let map be args.[[ParameterMap]].
  34. auto& map = *m_parameter_map;
  35. // 2. Let isMapped be ! HasOwnProperty(map, P).
  36. bool is_mapped = MUST(m_parameter_map->has_own_property(property_key));
  37. // 3. If isMapped is false, then
  38. if (!is_mapped) {
  39. // a. Return ? OrdinaryGet(args, P, Receiver).
  40. return Object::internal_get(property_key, receiver);
  41. }
  42. // FIXME: a. Assert: map contains a formal parameter mapping for P.
  43. // b. Return Get(map, P).
  44. return map.get(property_key);
  45. }
  46. // 10.4.4.4 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-set-p-v-receiver
  47. ThrowCompletionOr<bool> ArgumentsObject::internal_set(PropertyKey const& property_key, Value value, Value receiver)
  48. {
  49. bool is_mapped = false;
  50. // 1. If SameValue(args, Receiver) is false, then
  51. if (!same_value(this, receiver)) {
  52. // a. Let isMapped be false.
  53. is_mapped = false;
  54. } else {
  55. // a. Let map be args.[[ParameterMap]].
  56. // b. Let isMapped be ! HasOwnProperty(map, P).
  57. is_mapped = MUST(parameter_map().has_own_property(property_key));
  58. }
  59. // 3. If isMapped is true, then
  60. if (is_mapped) {
  61. // a. Let setStatus be Set(map, P, V, false).
  62. auto set_status = MUST(m_parameter_map->set(property_key, value, Object::ShouldThrowExceptions::No));
  63. // b. Assert: setStatus is true because formal parameters mapped by argument objects are always writable.
  64. VERIFY(set_status);
  65. }
  66. // 4. Return ? OrdinarySet(args, P, V, Receiver).
  67. return Object::internal_set(property_key, value, receiver);
  68. }
  69. // 10.4.4.5 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-delete-p
  70. ThrowCompletionOr<bool> ArgumentsObject::internal_delete(PropertyKey const& property_key)
  71. {
  72. // 1. Let map be args.[[ParameterMap]].
  73. auto& map = parameter_map();
  74. // 2. Let isMapped be ! HasOwnProperty(map, P).
  75. bool is_mapped = MUST(map.has_own_property(property_key));
  76. // 3. Let result be ? OrdinaryDelete(args, P).
  77. bool result = TRY(Object::internal_delete(property_key));
  78. // 4. If result is true and isMapped is true, then
  79. if (result && is_mapped) {
  80. // a. Call map.[[Delete]](P).
  81. MUST(map.internal_delete(property_key));
  82. }
  83. // 5. Return result.
  84. return result;
  85. }
  86. // 10.4.4.1 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-getownproperty-p
  87. ThrowCompletionOr<Optional<PropertyDescriptor>> ArgumentsObject::internal_get_own_property(PropertyKey const& property_key) const
  88. {
  89. // 1. Let desc be OrdinaryGetOwnProperty(args, P).
  90. auto desc = MUST(Object::internal_get_own_property(property_key));
  91. // 2. If desc is undefined, return desc.
  92. if (!desc.has_value())
  93. return desc;
  94. // 3. Let map be args.[[ParameterMap]].
  95. // 4. Let isMapped be ! HasOwnProperty(map, P).
  96. bool is_mapped = MUST(m_parameter_map->has_own_property(property_key));
  97. // 5. If isMapped is true, then
  98. if (is_mapped) {
  99. // a. Set desc.[[Value]] to Get(map, P).
  100. desc->value = TRY(m_parameter_map->get(property_key));
  101. }
  102. // 6. Return desc.
  103. return desc;
  104. }
  105. // 10.4.4.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-defineownproperty-p-desc
  106. ThrowCompletionOr<bool> ArgumentsObject::internal_define_own_property(PropertyKey const& property_key, PropertyDescriptor const& descriptor)
  107. {
  108. // 1. Let map be args.[[ParameterMap]].
  109. auto& map = parameter_map();
  110. // 2. Let isMapped be HasOwnProperty(map, P).
  111. bool is_mapped = MUST(map.has_own_property(property_key));
  112. // 3. Let newArgDesc be Desc.
  113. auto new_arg_desc = descriptor;
  114. // 4. If isMapped is true and IsDataDescriptor(Desc) is true, then
  115. if (is_mapped && descriptor.is_data_descriptor()) {
  116. // a. If Desc.[[Value]] is not present and Desc.[[Writable]] is present and its value is false, then
  117. if (!descriptor.value.has_value() && descriptor.writable.has_value() && descriptor.writable == false) {
  118. // i. Set newArgDesc to a copy of Desc.
  119. new_arg_desc = descriptor;
  120. // ii. Set newArgDesc.[[Value]] to Get(map, P).
  121. new_arg_desc.value = TRY(map.get(property_key));
  122. }
  123. }
  124. // 5. Let allowed be ? OrdinaryDefineOwnProperty(args, P, newArgDesc).
  125. bool allowed = TRY(Object::internal_define_own_property(property_key, new_arg_desc));
  126. // 6. If allowed is false, return false.
  127. if (!allowed)
  128. return false;
  129. // 7. If isMapped is true, then
  130. if (is_mapped) {
  131. // a. If IsAccessorDescriptor(Desc) is true, then
  132. if (descriptor.is_accessor_descriptor()) {
  133. // i. Call map.[[Delete]](P).
  134. MUST(map.internal_delete(property_key));
  135. } else {
  136. // i. If Desc.[[Value]] is present, then
  137. if (descriptor.value.has_value()) {
  138. // 1. Let setStatus be Set(map, P, Desc.[[Value]], false).
  139. bool set_status = MUST(map.set(property_key, descriptor.value.value(), Object::ShouldThrowExceptions::No));
  140. // 2. Assert: setStatus is true because formal parameters mapped by argument objects are always writable.
  141. VERIFY(set_status);
  142. }
  143. // ii. If Desc.[[Writable]] is present and its value is false, then
  144. if (descriptor.writable == false) {
  145. // 1. Call map.[[Delete]](P).
  146. MUST(map.internal_delete(property_key));
  147. }
  148. }
  149. }
  150. // 8. Return true.
  151. return true;
  152. }
  153. }