AggregateErrorConstructor.cpp 2.3 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/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(GlobalObject& global_object)
  15. : NativeFunction(*static_cast<Object*>(global_object.error_constructor()))
  16. {
  17. }
  18. void AggregateErrorConstructor::initialize(GlobalObject& global_object)
  19. {
  20. auto& vm = this->vm();
  21. NativeFunction::initialize(global_object);
  22. // 20.5.7.2.1 AggregateError.prototype, https://tc39.es/ecma262/#sec-aggregate-error.prototype
  23. define_direct_property(vm.names.prototype, 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. Value AggregateErrorConstructor::call()
  28. {
  29. return construct(*this);
  30. }
  31. // 20.5.7.1.1 AggregateError ( errors, message ), https://tc39.es/ecma262/#sec-aggregate-error
  32. Value AggregateErrorConstructor::construct(FunctionObject& new_target)
  33. {
  34. auto& vm = this->vm();
  35. auto& global_object = this->global_object();
  36. auto* aggregate_error = TRY_OR_DISCARD(ordinary_create_from_constructor<AggregateError>(global_object, new_target, &GlobalObject::aggregate_error_prototype));
  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->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message));
  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_or_throw(vm.names.errors, { .value = Array::create_from(global_object, errors_list), .writable = true, .enumerable = false, .configurable = true });
  50. return aggregate_error;
  51. }
  52. }