| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  | #include <LibJS/Runtime/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | #include <LibJS/Runtime/AggregateError.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/AggregateErrorConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-11 20:40:08 +01:00
										 |  |  | #include <LibJS/Runtime/Array.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | #include <LibJS/Runtime/ErrorConstructor.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/IteratorOperations.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AggregateErrorConstructor::AggregateErrorConstructor(GlobalObject& global_object) | 
					
						
							|  |  |  |     : NativeFunction(*static_cast<Object*>(global_object.error_constructor())) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AggregateErrorConstructor::initialize(GlobalObject& global_object) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     NativeFunction::initialize(global_object); | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 20.5.7.2.1 AggregateError.prototype, https://tc39.es/ecma262/#sec-aggregate-error.prototype
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.prototype, global_object.aggregate_error_prototype(), 0); | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.length, Value(2), Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 20.5.7.1.1 AggregateError ( errors, message ), https://tc39.es/ecma262/#sec-aggregate-error
 | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | Value AggregateErrorConstructor::call() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return construct(*this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 00:22:35 +01:00
										 |  |  | // 20.5.7.1.1 AggregateError ( errors, message ), https://tc39.es/ecma262/#sec-aggregate-error
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  | Value AggregateErrorConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto& global_object = this->global_object(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-16 00:57:38 +03:00
										 |  |  |     auto* aggregate_error = TRY_OR_DISCARD(ordinary_create_from_constructor<AggregateError>(global_object, new_target, &GlobalObject::aggregate_error_prototype)); | 
					
						
							| 
									
										
										
										
											2021-06-11 20:40:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  |     if (!vm.argument(1).is_undefined()) { | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |         auto message = vm.argument(1).to_string(global_object); | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  |         if (vm.exception()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							| 
									
										
										
										
											2021-07-06 00:13:19 +03:00
										 |  |  |         aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message)); | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-11 20:40:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     aggregate_error->install_error_cause(vm.argument(2)); | 
					
						
							|  |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-20 01:09:39 +01:00
										 |  |  |     auto errors_list = iterable_to_list(global_object, vm.argument(0)); | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  |     if (vm.exception()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2021-06-11 20:40:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 00:13:19 +03:00
										 |  |  |     aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(global_object, errors_list), .writable = true, .enumerable = false, .configurable = true }); | 
					
						
							| 
									
										
										
										
											2021-06-11 20:40:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return aggregate_error; | 
					
						
							| 
									
										
										
										
											2021-06-11 18:06:20 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |