| 
									
										
										
										
											2021-06-28 13:26:01 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/ArgumentsObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:53:57 +01:00
										 |  |  | #include <LibJS/Runtime/Completion.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-28 13:26:01 +02:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  | ArgumentsObject::ArgumentsObject(Realm& realm, Environment& environment) | 
					
						
							| 
									
										
										
										
											2023-04-13 00:47:15 +02:00
										 |  |  |     : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype()) | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     , m_environment(environment) | 
					
						
							| 
									
										
										
										
											2021-06-28 13:26:01 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  | void ArgumentsObject::initialize(Realm& realm) | 
					
						
							| 
									
										
										
										
											2021-06-28 13:26:01 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2021-07-05 18:31:56 +01:00
										 |  |  |     set_has_parameter_map(); | 
					
						
							| 
									
										
										
										
											2022-08-16 00:20:49 +01:00
										 |  |  |     m_parameter_map = Object::create(realm, nullptr); | 
					
						
							| 
									
										
										
										
											2021-06-28 13:26:01 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | void ArgumentsObject::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     visitor.visit(m_environment); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     visitor.visit(m_parameter_map); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 10.4.4.3 [[Get]] ( P, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-get-p-receiver
 | 
					
						
							| 
									
										
										
										
											2023-07-08 17:39:18 +02:00
										 |  |  | ThrowCompletionOr<Value> ArgumentsObject::internal_get(PropertyKey const& property_key, Value receiver, CacheablePropertyMetadata* cacheable_metadata) const | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let map be args.[[ParameterMap]].
 | 
					
						
							|  |  |  |     auto& map = *m_parameter_map; | 
					
						
							| 
									
										
										
										
											2021-10-03 02:17:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 2. Let isMapped be ! HasOwnProperty(map, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     bool is_mapped = MUST(m_parameter_map->has_own_property(property_key)); | 
					
						
							| 
									
										
										
										
											2021-10-03 02:17:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 3. If isMapped is false, then
 | 
					
						
							|  |  |  |     if (!is_mapped) { | 
					
						
							| 
									
										
										
										
											2022-05-03 22:51:36 +02:00
										 |  |  |         // a. Return ? OrdinaryGet(args, P, Receiver).
 | 
					
						
							| 
									
										
										
										
											2023-07-08 17:39:18 +02:00
										 |  |  |         return Object::internal_get(property_key, receiver, cacheable_metadata); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: a. Assert: map contains a formal parameter mapping for P.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |     // b. Return ! Get(map, P).
 | 
					
						
							|  |  |  |     return MUST(map.get(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 10.4.4.4 [[Set]] ( P, V, Receiver ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-set-p-v-receiver
 | 
					
						
							| 
									
										
										
										
											2023-11-08 20:51:26 +01:00
										 |  |  | ThrowCompletionOr<bool> ArgumentsObject::internal_set(PropertyKey const& property_key, Value value, Value receiver, CacheablePropertyMetadata*) | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     bool is_mapped = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. If SameValue(args, Receiver) is false, then
 | 
					
						
							| 
									
										
										
										
											2021-07-05 02:52:02 +02:00
										 |  |  |     if (!same_value(this, receiver)) { | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |         // a. Let isMapped be false.
 | 
					
						
							|  |  |  |         is_mapped = false; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         // a. Let map be args.[[ParameterMap]].
 | 
					
						
							|  |  |  |         // b. Let isMapped be ! HasOwnProperty(map, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |         is_mapped = MUST(parameter_map().has_own_property(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. If isMapped is true, then
 | 
					
						
							|  |  |  |     if (is_mapped) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |         // a. Assert: The following Set will succeed, since formal parameters mapped by arguments objects are always writable.
 | 
					
						
							| 
									
										
										
										
											2021-10-03 00:32:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |         // b. Perform ! Set(map, P, V, false).
 | 
					
						
							|  |  |  |         MUST(m_parameter_map->set(property_key, value, Object::ShouldThrowExceptions::No)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Return ? OrdinarySet(args, P, V, Receiver).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     return Object::internal_set(property_key, value, receiver); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 10.4.4.5 [[Delete]] ( P ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-delete-p
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  | ThrowCompletionOr<bool> ArgumentsObject::internal_delete(PropertyKey const& property_key) | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let map be args.[[ParameterMap]].
 | 
					
						
							|  |  |  |     auto& map = parameter_map(); | 
					
						
							| 
									
										
										
										
											2021-09-29 18:45:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 2. Let isMapped be ! HasOwnProperty(map, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     bool is_mapped = MUST(map.has_own_property(property_key)); | 
					
						
							| 
									
										
										
										
											2021-09-29 18:45:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 3. Let result be ? OrdinaryDelete(args, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     bool result = TRY(Object::internal_delete(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. If result is true and isMapped is true, then
 | 
					
						
							|  |  |  |     if (result && is_mapped) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |         // a. Perform ! map.[[Delete]](P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |         MUST(map.internal_delete(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 5. Return result.
 | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 10.4.4.1 [[GetOwnProperty]] ( P ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-getownproperty-p
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  | ThrowCompletionOr<Optional<PropertyDescriptor>> ArgumentsObject::internal_get_own_property(PropertyKey const& property_key) const | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let desc be OrdinaryGetOwnProperty(args, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     auto desc = MUST(Object::internal_get_own_property(property_key)); | 
					
						
							| 
									
										
										
										
											2021-10-03 15:57:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 2. If desc is undefined, return desc.
 | 
					
						
							|  |  |  |     if (!desc.has_value()) | 
					
						
							|  |  |  |         return desc; | 
					
						
							| 
									
										
										
										
											2021-10-03 02:17:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 3. Let map be args.[[ParameterMap]].
 | 
					
						
							|  |  |  |     // 4. Let isMapped be ! HasOwnProperty(map, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     bool is_mapped = MUST(m_parameter_map->has_own_property(property_key)); | 
					
						
							| 
									
										
										
										
											2021-10-03 02:17:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 5. If isMapped is true, then
 | 
					
						
							|  |  |  |     if (is_mapped) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |         // a. Set desc.[[Value]] to ! Get(map, P).
 | 
					
						
							|  |  |  |         desc->value = MUST(m_parameter_map->get(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-03 02:17:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |     // 6. Return desc.
 | 
					
						
							|  |  |  |     return desc; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 10.4.4.2 [[DefineOwnProperty]] ( P, Desc ), https://tc39.es/ecma262/#sec-arguments-exotic-objects-defineownproperty-p-desc
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  | ThrowCompletionOr<bool> ArgumentsObject::internal_define_own_property(PropertyKey const& property_key, PropertyDescriptor const& descriptor) | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     // 1. Let map be args.[[ParameterMap]].
 | 
					
						
							|  |  |  |     auto& map = parameter_map(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |     // 2. Let isMapped be ! HasOwnProperty(map, P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |     bool is_mapped = MUST(map.has_own_property(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Let newArgDesc be Desc.
 | 
					
						
							|  |  |  |     auto new_arg_desc = descriptor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. If isMapped is true and IsDataDescriptor(Desc) is true, then
 | 
					
						
							|  |  |  |     if (is_mapped && descriptor.is_data_descriptor()) { | 
					
						
							| 
									
										
										
										
											2022-05-01 00:47:55 +02:00
										 |  |  |         // a. If Desc does not have a [[Value]] field and Desc has a [[Writable]] field, and Desc.[[Writable]] is false, then
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |         if (!descriptor.value.has_value() && descriptor.writable.has_value() && descriptor.writable == false) { | 
					
						
							|  |  |  |             // i. Set newArgDesc to a copy of Desc.
 | 
					
						
							|  |  |  |             new_arg_desc = descriptor; | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |             // ii. Set newArgDesc.[[Value]] to ! Get(map, P).
 | 
					
						
							|  |  |  |             new_arg_desc.value = MUST(map.get(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |     // 5. Let allowed be ! OrdinaryDefineOwnProperty(args, P, newArgDesc).
 | 
					
						
							|  |  |  |     bool allowed = MUST(Object::internal_define_own_property(property_key, new_arg_desc)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 6. If allowed is false, return false.
 | 
					
						
							|  |  |  |     if (!allowed) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 7. If isMapped is true, then
 | 
					
						
							|  |  |  |     if (is_mapped) { | 
					
						
							|  |  |  |         // a. If IsAccessorDescriptor(Desc) is true, then
 | 
					
						
							|  |  |  |         if (descriptor.is_accessor_descriptor()) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |             // i. Perform ! map.[[Delete]](P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |             MUST(map.internal_delete(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-05-01 00:47:55 +02:00
										 |  |  |             // i. If Desc has a [[Value]] field, then
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |             if (descriptor.value.has_value()) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |                 // 1. Assert: The following Set will succeed, since formal parameters mapped by arguments objects are always writable.
 | 
					
						
							| 
									
										
										
										
											2021-10-03 00:32:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |                 // 2. Perform ! Set(map, P, Desc.[[Value]], false).
 | 
					
						
							|  |  |  |                 MUST(map.set(property_key, descriptor.value.value(), Object::ShouldThrowExceptions::No)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-05-01 00:47:55 +02:00
										 |  |  |             // ii. If Desc has a [[Writable]] field and Desc.[[Writable]] is false, then
 | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |             if (descriptor.writable == false) { | 
					
						
							| 
									
										
										
										
											2022-05-02 20:54:39 +02:00
										 |  |  |                 // 1. Perform ! map.[[Delete]](P).
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:59:04 +00:00
										 |  |  |                 MUST(map.internal_delete(property_key)); | 
					
						
							| 
									
										
										
										
											2021-06-28 21:19:25 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 8. Return true.
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-28 13:26:01 +02:00
										 |  |  | } |