SuppressedErrorConstructor.cpp 3.2 KB

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