2021-06-11 18:06:20 +01:00
|
|
|
/*
|
2022-05-02 20:54:39 +02:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2021-06-11 18:06:20 +01:00
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
AggregateErrorConstructor::AggregateErrorConstructor(Realm& realm)
|
|
|
|
: NativeFunction(static_cast<Object&>(*realm.global_object().error_constructor()))
|
2021-06-11 18:06:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
void AggregateErrorConstructor::initialize(Realm& realm)
|
2021-06-11 18:06:20 +01:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2022-08-16 00:20:49 +01:00
|
|
|
NativeFunction::initialize(realm);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
|
|
// 20.5.7.2.1 AggregateError.prototype, https://tc39.es/ecma262/#sec-aggregate-error.prototype
|
2022-08-16 00:20:49 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.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-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> AggregateErrorConstructor::call()
|
2021-06-11 18:06:20 +01:00
|
|
|
{
|
2021-10-20 21:16:30 +01:00
|
|
|
return TRY(construct(*this));
|
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-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Object*> AggregateErrorConstructor::construct(FunctionObject& new_target)
|
2021-06-11 18:06:20 +01:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2022-08-22 19:00:49 +01:00
|
|
|
auto& realm = *vm.current_realm();
|
2021-06-20 01:09:39 +01:00
|
|
|
|
2022-08-21 19:24:32 +01:00
|
|
|
auto* aggregate_error = TRY(ordinary_create_from_constructor<AggregateError>(vm, 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()) {
|
2022-08-21 14:00:56 +01:00
|
|
|
auto message = TRY(vm.argument(1).to_string(vm));
|
2022-05-02 20:54:39 +02: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
|
|
|
|
2021-10-20 21:16:30 +01:00
|
|
|
TRY(aggregate_error->install_error_cause(vm.argument(2)));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-08-21 15:56:27 +01:00
|
|
|
auto errors_list = TRY(iterable_to_list(vm, vm.argument(0)));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-08-16 00:20:49 +01:00
|
|
|
MUST(aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(realm, 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
|
|
|
}
|
|
|
|
|
|
|
|
}
|