PromiseResolvingElementFunctions.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@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/Array.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/PromiseCapability.h>
  11. #include <LibJS/Runtime/PromiseResolvingElementFunctions.h>
  12. namespace JS {
  13. JS_DEFINE_ALLOCATOR(RemainingElements);
  14. JS_DEFINE_ALLOCATOR(PromiseValueList);
  15. JS_DEFINE_ALLOCATOR(PromiseResolvingElementFunction);
  16. JS_DEFINE_ALLOCATOR(PromiseAllResolveElementFunction);
  17. JS_DEFINE_ALLOCATOR(PromiseAllSettledResolveElementFunction);
  18. JS_DEFINE_ALLOCATOR(PromiseAllSettledRejectElementFunction);
  19. JS_DEFINE_ALLOCATOR(PromiseAnyRejectElementFunction);
  20. void PromiseValueList::visit_edges(Visitor& visitor)
  21. {
  22. Base::visit_edges(visitor);
  23. visitor.visit(m_values);
  24. }
  25. PromiseResolvingElementFunction::PromiseResolvingElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements, Object& prototype)
  26. : NativeFunction(prototype)
  27. , m_index(index)
  28. , m_values(values)
  29. , m_capability(capability)
  30. , m_remaining_elements(remaining_elements)
  31. {
  32. }
  33. void PromiseResolvingElementFunction::initialize(Realm& realm)
  34. {
  35. Base::initialize(realm);
  36. define_direct_property(vm().names.length, Value(1), Attribute::Configurable);
  37. }
  38. ThrowCompletionOr<Value> PromiseResolvingElementFunction::call()
  39. {
  40. if (m_already_called)
  41. return js_undefined();
  42. m_already_called = true;
  43. return resolve_element();
  44. }
  45. void PromiseResolvingElementFunction::visit_edges(Cell::Visitor& visitor)
  46. {
  47. Base::visit_edges(visitor);
  48. visitor.visit(m_values);
  49. visitor.visit(m_capability);
  50. visitor.visit(m_remaining_elements);
  51. }
  52. NonnullGCPtr<PromiseAllResolveElementFunction> PromiseAllResolveElementFunction::create(Realm& realm, size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements)
  53. {
  54. return realm.heap().allocate<PromiseAllResolveElementFunction>(realm, index, values, capability, remaining_elements, realm.intrinsics().function_prototype());
  55. }
  56. PromiseAllResolveElementFunction::PromiseAllResolveElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements, Object& prototype)
  57. : PromiseResolvingElementFunction(index, values, capability, remaining_elements, prototype)
  58. {
  59. }
  60. ThrowCompletionOr<Value> PromiseAllResolveElementFunction::resolve_element()
  61. {
  62. auto& vm = this->vm();
  63. auto& realm = *vm.current_realm();
  64. // 8. Set values[index] to x.
  65. m_values->values()[m_index] = vm.argument(0);
  66. // 9. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
  67. // 10. If remainingElementsCount.[[Value]] is 0, then
  68. if (--m_remaining_elements->value == 0) {
  69. // a. Let valuesArray be CreateArrayFromList(values).
  70. auto values_array = Array::create_from(realm, m_values->values());
  71. // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
  72. return JS::call(vm, *m_capability->resolve(), js_undefined(), values_array);
  73. }
  74. // 11. Return undefined.
  75. return js_undefined();
  76. }
  77. NonnullGCPtr<PromiseAllSettledResolveElementFunction> PromiseAllSettledResolveElementFunction::create(Realm& realm, size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements)
  78. {
  79. return realm.heap().allocate<PromiseAllSettledResolveElementFunction>(realm, index, values, capability, remaining_elements, realm.intrinsics().function_prototype());
  80. }
  81. PromiseAllSettledResolveElementFunction::PromiseAllSettledResolveElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements, Object& prototype)
  82. : PromiseResolvingElementFunction(index, values, capability, remaining_elements, prototype)
  83. {
  84. }
  85. ThrowCompletionOr<Value> PromiseAllSettledResolveElementFunction::resolve_element()
  86. {
  87. auto& vm = this->vm();
  88. auto& realm = *vm.current_realm();
  89. // 9. Let obj be OrdinaryObjectCreate(%Object.prototype%).
  90. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  91. // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "fulfilled").
  92. MUST(object->create_data_property_or_throw(vm.names.status, PrimitiveString::create(vm, "fulfilled"_string)));
  93. // 11. Perform ! CreateDataPropertyOrThrow(obj, "value", x).
  94. MUST(object->create_data_property_or_throw(vm.names.value, vm.argument(0)));
  95. // 12. Set values[index] to obj.
  96. m_values->values()[m_index] = object;
  97. // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
  98. // 14. If remainingElementsCount.[[Value]] is 0, then
  99. if (--m_remaining_elements->value == 0) {
  100. // a. Let valuesArray be CreateArrayFromList(values).
  101. auto values_array = Array::create_from(realm, m_values->values());
  102. // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
  103. return JS::call(vm, *m_capability->resolve(), js_undefined(), values_array);
  104. }
  105. // 15. Return undefined.
  106. return js_undefined();
  107. }
  108. NonnullGCPtr<PromiseAllSettledRejectElementFunction> PromiseAllSettledRejectElementFunction::create(Realm& realm, size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements)
  109. {
  110. return realm.heap().allocate<PromiseAllSettledRejectElementFunction>(realm, index, values, capability, remaining_elements, realm.intrinsics().function_prototype());
  111. }
  112. PromiseAllSettledRejectElementFunction::PromiseAllSettledRejectElementFunction(size_t index, PromiseValueList& values, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements, Object& prototype)
  113. : PromiseResolvingElementFunction(index, values, capability, remaining_elements, prototype)
  114. {
  115. }
  116. ThrowCompletionOr<Value> PromiseAllSettledRejectElementFunction::resolve_element()
  117. {
  118. auto& vm = this->vm();
  119. auto& realm = *vm.current_realm();
  120. // 9. Let obj be OrdinaryObjectCreate(%Object.prototype%).
  121. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  122. // 10. Perform ! CreateDataPropertyOrThrow(obj, "status", "rejected").
  123. MUST(object->create_data_property_or_throw(vm.names.status, PrimitiveString::create(vm, "rejected"_string)));
  124. // 11. Perform ! CreateDataPropertyOrThrow(obj, "reason", x).
  125. MUST(object->create_data_property_or_throw(vm.names.reason, vm.argument(0)));
  126. // 12. Set values[index] to obj.
  127. m_values->values()[m_index] = object;
  128. // 13. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
  129. // 14. If remainingElementsCount.[[Value]] is 0, then
  130. if (--m_remaining_elements->value == 0) {
  131. // a. Let valuesArray be CreateArrayFromList(values).
  132. auto values_array = Array::create_from(realm, m_values->values());
  133. // b. Return ? Call(promiseCapability.[[Resolve]], undefined, « valuesArray »).
  134. return JS::call(vm, *m_capability->resolve(), js_undefined(), values_array);
  135. }
  136. // 15. Return undefined.
  137. return js_undefined();
  138. }
  139. NonnullGCPtr<PromiseAnyRejectElementFunction> PromiseAnyRejectElementFunction::create(Realm& realm, size_t index, PromiseValueList& errors, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements)
  140. {
  141. return realm.heap().allocate<PromiseAnyRejectElementFunction>(realm, index, errors, capability, remaining_elements, realm.intrinsics().function_prototype());
  142. }
  143. PromiseAnyRejectElementFunction::PromiseAnyRejectElementFunction(size_t index, PromiseValueList& errors, NonnullGCPtr<PromiseCapability const> capability, RemainingElements& remaining_elements, Object& prototype)
  144. : PromiseResolvingElementFunction(index, errors, capability, remaining_elements, prototype)
  145. {
  146. }
  147. ThrowCompletionOr<Value> PromiseAnyRejectElementFunction::resolve_element()
  148. {
  149. auto& vm = this->vm();
  150. auto& realm = *vm.current_realm();
  151. // 8. Set errors[index] to x.
  152. m_values->values()[m_index] = vm.argument(0);
  153. // 9. Set remainingElementsCount.[[Value]] to remainingElementsCount.[[Value]] - 1.
  154. // 10. If remainingElementsCount.[[Value]] is 0, then
  155. if (--m_remaining_elements->value == 0) {
  156. // a. Let error be a newly created AggregateError object.
  157. auto error = AggregateError::create(realm);
  158. // b. Perform ! DefinePropertyOrThrow(error, "errors", PropertyDescriptor { [[Configurable]]: true, [[Enumerable]]: false, [[Writable]]: true, [[Value]]: CreateArrayFromList(errors) }).
  159. auto errors_array = Array::create_from(realm, m_values->values());
  160. MUST(error->define_property_or_throw(vm.names.errors, { .value = errors_array, .writable = true, .enumerable = false, .configurable = true }));
  161. // c. Return ? Call(promiseCapability.[[Reject]], undefined, « error »).
  162. return JS::call(vm, *m_capability->reject(), js_undefined(), error);
  163. }
  164. return js_undefined();
  165. }
  166. }