SuppressedErrorConstructor.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/ErrorConstructor.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Iterator.h>
  11. #include <LibJS/Runtime/SuppressedError.h>
  12. #include <LibJS/Runtime/SuppressedErrorConstructor.h>
  13. namespace JS {
  14. SuppressedErrorConstructor::SuppressedErrorConstructor(Realm& realm)
  15. : NativeFunction(static_cast<Object&>(realm.intrinsics().error_constructor()))
  16. {
  17. }
  18. void SuppressedErrorConstructor::initialize(Realm& realm)
  19. {
  20. auto& vm = this->vm();
  21. Base::initialize(realm);
  22. // 10.1.4.2.1 SuppressedError.prototype, https://tc39.es/proposal-explicit-resource-management/#sec-suppressederror.prototype
  23. define_direct_property(vm.names.prototype, realm.intrinsics().suppressed_error_prototype(), 0);
  24. define_direct_property(vm.names.length, Value(3), Attribute::Configurable);
  25. }
  26. // 10.1.4.1.1 SuppressedError ( error, suppressed, message [ , options ] ), https://tc39.es/proposal-explicit-resource-management/#sec-suppressederror
  27. ThrowCompletionOr<Value> SuppressedErrorConstructor::call()
  28. {
  29. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  30. return TRY(construct(*this));
  31. }
  32. // 10.1.4.1.1 SuppressedError ( error, suppressed, message [ , options ] ), https://tc39.es/proposal-explicit-resource-management/#sec-suppressederror
  33. ThrowCompletionOr<NonnullGCPtr<Object>> SuppressedErrorConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto error = vm.argument(0);
  37. auto suppressed = vm.argument(1);
  38. auto message = vm.argument(2);
  39. auto options = vm.argument(3);
  40. // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%SuppressedError.prototype%", « [[ErrorData]] »).
  41. auto suppressed_error = TRY(ordinary_create_from_constructor<SuppressedError>(vm, new_target, &Intrinsics::suppressed_error_prototype));
  42. // 3. If message is not undefined, then
  43. if (!message.is_undefined()) {
  44. // a. Let msg be ? ToString(message).
  45. auto msg = TRY(message.to_string(vm));
  46. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
  47. suppressed_error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg)));
  48. }
  49. // 4. Perform ? InstallErrorCause(O, options).
  50. TRY(suppressed_error->install_error_cause(options));
  51. // 5. Perform ! DefinePropertyOrThrow(O, "error", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: error }).
  52. MUST(suppressed_error->define_property_or_throw(vm.names.error, { .value = error, .writable = true, .enumerable = false, .configurable = true }));
  53. // 6. Perform ! DefinePropertyOrThrow(O, "suppressed", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: suppressed }).
  54. MUST(suppressed_error->define_property_or_throw(vm.names.suppressed, { .value = suppressed, .writable = true, .enumerable = false, .configurable = true }));
  55. // 7. Return O.
  56. return suppressed_error;
  57. }
  58. }