AggregateErrorConstructor.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AggregateError.h>
  7. #include <LibJS/Runtime/AggregateErrorConstructor.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibJS/Runtime/ErrorConstructor.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/IteratorOperations.h>
  12. namespace JS {
  13. AggregateErrorConstructor::AggregateErrorConstructor(GlobalObject& global_object)
  14. : NativeFunction(*static_cast<Object*>(global_object.error_constructor()))
  15. {
  16. }
  17. void AggregateErrorConstructor::initialize(GlobalObject& global_object)
  18. {
  19. auto& vm = this->vm();
  20. NativeFunction::initialize(global_object);
  21. // 20.5.7.2.1 AggregateError.prototype, https://tc39.es/ecma262/#sec-aggregate-error.prototype
  22. define_property(vm.names.prototype, global_object.aggregate_error_prototype(), 0);
  23. define_property(vm.names.length, Value(2), Attribute::Configurable);
  24. }
  25. // 20.5.7.1.1 AggregateError ( errors, message ), https://tc39.es/ecma262/#sec-aggregate-error
  26. Value AggregateErrorConstructor::call()
  27. {
  28. return construct(*this);
  29. }
  30. // 20.5.7.1.1 AggregateError ( errors, message ), https://tc39.es/ecma262/#sec-aggregate-error
  31. Value AggregateErrorConstructor::construct(Function&)
  32. {
  33. auto& vm = this->vm();
  34. // FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%")
  35. auto* aggregate_error = AggregateError::create(global_object());
  36. u8 attr = Attribute::Writable | Attribute::Configurable;
  37. if (!vm.argument(1).is_undefined()) {
  38. auto message = vm.argument(1).to_string(global_object());
  39. if (vm.exception())
  40. return {};
  41. aggregate_error->define_property(vm.names.message, js_string(vm, message), attr);
  42. }
  43. aggregate_error->install_error_cause(vm.argument(2));
  44. if (vm.exception())
  45. return {};
  46. auto errors_list = iterable_to_list(global_object(), vm.argument(0));
  47. if (vm.exception())
  48. return {};
  49. aggregate_error->define_property(vm.names.errors, Array::create_from(global_object(), errors_list), attr);
  50. return aggregate_error;
  51. }
  52. }