PromiseResolvingElementFunctions.cpp 8.8 KB

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