ArgumentsObject.cpp 6.7 KB

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