AggregateErrorConstructor.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2021-2022, 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/IteratorOperations.h>
  13. namespace JS {
  14. AggregateErrorConstructor::AggregateErrorConstructor(Realm& realm)
  15. : NativeFunction(static_cast<Object&>(*realm.global_object().error_constructor()))
  16. {
  17. }
  18. void AggregateErrorConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. NativeFunction::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.global_object().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 ), https://tc39.es/ecma262/#sec-aggregate-error
  27. ThrowCompletionOr<Value> AggregateErrorConstructor::call()
  28. {
  29. return TRY(construct(*this));
  30. }
  31. // 20.5.7.1.1 AggregateError ( errors, message ), https://tc39.es/ecma262/#sec-aggregate-error
  32. ThrowCompletionOr<Object*> AggregateErrorConstructor::construct(FunctionObject& new_target)
  33. {
  34. auto& vm = this->vm();
  35. auto& global_object = this->global_object();
  36. auto* aggregate_error = TRY(ordinary_create_from_constructor<AggregateError>(global_object, new_target, &GlobalObject::aggregate_error_prototype));
  37. if (!vm.argument(1).is_undefined()) {
  38. auto message = TRY(vm.argument(1).to_string(global_object));
  39. aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message));
  40. }
  41. TRY(aggregate_error->install_error_cause(vm.argument(2)));
  42. auto errors_list = TRY(iterable_to_list(global_object, vm.argument(0)));
  43. MUST(aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(global_object, errors_list), .writable = true, .enumerable = false, .configurable = true }));
  44. return aggregate_error;
  45. }
  46. }