PromiseResolvingElementFunctions.cpp 8.6 KB

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