AggregateErrorConstructor.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. GC_DEFINE_ALLOCATOR(AggregateErrorConstructor);
  15. AggregateErrorConstructor::AggregateErrorConstructor(Realm& realm)
  16. : NativeFunction(static_cast<Object&>(*realm.intrinsics().error_constructor()))
  17. {
  18. }
  19. void AggregateErrorConstructor::initialize(Realm& realm)
  20. {
  21. auto& vm = this->vm();
  22. Base::initialize(realm);
  23. // 20.5.7.2.1 AggregateError.prototype, https://tc39.es/ecma262/#sec-aggregate-error.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().aggregate_error_prototype(), 0);
  25. define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  26. }
  27. // 20.5.7.1.1 AggregateError ( errors, message [ , options ] ), https://tc39.es/ecma262/#sec-aggregate-error
  28. ThrowCompletionOr<Value> AggregateErrorConstructor::call()
  29. {
  30. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  31. return TRY(construct(*this));
  32. }
  33. // 20.5.7.1.1 AggregateError ( errors, message [ , options ] ), https://tc39.es/ecma262/#sec-aggregate-error
  34. ThrowCompletionOr<GC::Ref<Object>> AggregateErrorConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto& realm = *vm.current_realm();
  38. auto errors = vm.argument(0);
  39. auto message = vm.argument(1);
  40. auto options = vm.argument(2);
  41. // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%", « [[ErrorData]] »).
  42. auto aggregate_error = TRY(ordinary_create_from_constructor<AggregateError>(vm, new_target, &Intrinsics::aggregate_error_prototype));
  43. // 3. If message is not undefined, then
  44. if (!message.is_undefined()) {
  45. // a. Let msg be ? ToString(message).
  46. auto msg = TRY(message.to_byte_string(vm));
  47. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
  48. aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, msg));
  49. }
  50. // 4. Perform ? InstallErrorCause(O, options).
  51. TRY(aggregate_error->install_error_cause(options));
  52. // 5. Let errorsList be ? IteratorToList(? GetIterator(errors, sync)).
  53. auto errors_list = TRY(iterator_to_list(vm, TRY(get_iterator(vm, errors, IteratorHint::Sync))));
  54. // 6. Perform ! DefinePropertyOrThrow(O, "errors", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: CreateArrayFromList(errorsList) }).
  55. MUST(aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(realm, errors_list), .writable = true, .enumerable = false, .configurable = true }));
  56. // 7. Return O.
  57. return aggregate_error;
  58. }
  59. }