AggregateErrorConstructor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/AggregateError.h>
  8. #include <LibJS/Runtime/AggregateErrorConstructor.h>
  9. #include <LibJS/Runtime/Array.h>
  10. #include <LibJS/Runtime/ErrorConstructor.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/Iterator.h>
  13. namespace JS {
  14. AggregateErrorConstructor::AggregateErrorConstructor(Realm& realm)
  15. : NativeFunction(static_cast<Object&>(*realm.intrinsics().error_constructor()))
  16. {
  17. }
  18. void AggregateErrorConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. Base::initialize(realm);
  22. // 20.5.7.2.1 AggregateError.prototype, https://tc39.es/ecma262/#sec-aggregate-error.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().aggregate_error_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  25. }
  26. // 20.5.7.1.1 AggregateError ( errors, message [ , options ] ), https://tc39.es/ecma262/#sec-aggregate-error
  27. ThrowCompletionOr<Value> AggregateErrorConstructor::call()
  28. {
  29. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  30. return TRY(construct(*this));
  31. }
  32. // 20.5.7.1.1 AggregateError ( errors, message [ , options ] ), https://tc39.es/ecma262/#sec-aggregate-error
  33. ThrowCompletionOr<NonnullGCPtr<Object>> AggregateErrorConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto& realm = *vm.current_realm();
  37. auto errors = vm.argument(0);
  38. auto message = vm.argument(1);
  39. auto options = vm.argument(2);
  40. // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%", « [[ErrorData]] »).
  41. auto aggregate_error = TRY(ordinary_create_from_constructor<AggregateError>(vm, new_target, &Intrinsics::aggregate_error_prototype));
  42. // 3. If message is not undefined, then
  43. if (!message.is_undefined()) {
  44. // a. Let msg be ? ToString(message).
  45. auto msg = TRY(message.to_deprecated_string(vm));
  46. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
  47. aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, msg));
  48. }
  49. // 4. Perform ? InstallErrorCause(O, options).
  50. TRY(aggregate_error->install_error_cause(options));
  51. // 5. Let errorsList be ? IteratorToList(? GetIterator(errors, sync)).
  52. auto errors_list = TRY(iterator_to_list(vm, TRY(get_iterator(vm, errors, IteratorHint::Sync))));
  53. // 6. Perform ! DefinePropertyOrThrow(O, "errors", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: CreateArrayFromList(errorsList) }).
  54. MUST(aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(realm, errors_list), .writable = true, .enumerable = false, .configurable = true }));
  55. // 7. Return O.
  56. return aggregate_error;
  57. }
  58. }