2021-06-11 18:06:20 +01:00
|
|
|
/*
|
2023-04-13 00:47:15 +02:00
|
|
|
* Copyright (c) 2021-2023, 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)
|
2022-08-27 00:54:55 +01:00
|
|
|
: NativeFunction(static_cast<Object&>(*realm.intrinsics().error_constructor()))
|
2021-06-11 18:06:20 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:33:35 -05:00
|
|
|
ThrowCompletionOr<void> AggregateErrorConstructor::initialize(Realm& realm)
|
2021-06-11 18:06:20 +01:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(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-27 00:54:55 +01:00
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().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);
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
2021-06-11 18:06:20 +01:00
|
|
|
}
|
|
|
|
|
2022-12-12 21:18:48 +00:00
|
|
|
// 20.5.7.1.1 AggregateError ( errors, message [ , options ] ), 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
|
|
|
{
|
2022-12-12 21:18:48 +00:00
|
|
|
// 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
|
2021-10-20 21:16:30 +01:00
|
|
|
return TRY(construct(*this));
|
2021-06-11 18:06:20 +01:00
|
|
|
}
|
|
|
|
|
2022-12-12 21:18:48 +00:00
|
|
|
// 20.5.7.1.1 AggregateError ( errors, message [ , options ] ), https://tc39.es/ecma262/#sec-aggregate-error
|
2022-12-14 19:18:10 +00:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<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-12-12 21:18:48 +00:00
|
|
|
auto errors = vm.argument(0);
|
|
|
|
auto message = vm.argument(1);
|
|
|
|
auto options = vm.argument(2);
|
|
|
|
|
|
|
|
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%", « [[ErrorData]] »).
|
2022-12-14 18:34:32 +00:00
|
|
|
auto aggregate_error = TRY(ordinary_create_from_constructor<AggregateError>(vm, new_target, &Intrinsics::aggregate_error_prototype));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-12-12 21:18:48 +00:00
|
|
|
// 3. If message is not undefined, then
|
|
|
|
if (!message.is_undefined()) {
|
|
|
|
// a. Let msg be ? ToString(message).
|
2023-01-13 10:29:02 -05:00
|
|
|
auto msg = TRY(message.to_deprecated_string(vm));
|
2022-12-12 21:18:48 +00:00
|
|
|
|
|
|
|
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
|
|
|
|
aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, msg));
|
2021-06-11 18:06:20 +01:00
|
|
|
}
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-12-12 21:18:48 +00:00
|
|
|
// 4. Perform ? InstallErrorCause(O, options).
|
|
|
|
TRY(aggregate_error->install_error_cause(options));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2023-07-18 15:02:28 -04:00
|
|
|
// 5. Let errorsList be ? IteratorToList(? GetIterator(errors, sync)).
|
|
|
|
auto errors_list = TRY(iterator_to_list(vm, TRY(get_iterator(vm, errors, IteratorHint::Sync))));
|
2021-06-11 20:40:08 +01:00
|
|
|
|
2022-12-12 21:18:48 +00:00
|
|
|
// 6. Perform ! DefinePropertyOrThrow(O, "errors", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: CreateArrayFromList(errorsList) }).
|
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
|
|
|
|
2022-12-12 21:18:48 +00:00
|
|
|
// 7. Return O.
|
2022-12-14 19:18:10 +00:00
|
|
|
return aggregate_error;
|
2021-06-11 18:06:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|