AggregateErrorConstructor.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 = ordinary_create_from_constructor<AggregateError>(global_object, new_target, &GlobalObject::aggregate_error_prototype);
  37. if (vm.exception())
  38. return {};
  39. if (!vm.argument(1).is_undefined()) {
  40. auto message = vm.argument(1).to_string(global_object);
  41. if (vm.exception())
  42. return {};
  43. aggregate_error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(vm, message));
  44. }
  45. aggregate_error->install_error_cause(vm.argument(2));
  46. if (vm.exception())
  47. return {};
  48. auto errors_list = iterable_to_list(global_object, vm.argument(0));
  49. if (vm.exception())
  50. return {};
  51. aggregate_error->define_property_or_throw(vm.names.errors, { .value = Array::create_from(global_object, errors_list), .writable = true, .enumerable = false, .configurable = true });
  52. return aggregate_error;
  53. }
  54. }