PromiseConstructor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Function.h>
  7. #include <LibJS/Interpreter.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/AggregateError.h>
  10. #include <LibJS/Runtime/Array.h>
  11. #include <LibJS/Runtime/Error.h>
  12. #include <LibJS/Runtime/FunctionObject.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <LibJS/Runtime/IteratorOperations.h>
  15. #include <LibJS/Runtime/Promise.h>
  16. #include <LibJS/Runtime/PromiseConstructor.h>
  17. #include <LibJS/Runtime/PromiseReaction.h>
  18. #include <LibJS/Runtime/PromiseResolvingElementFunctions.h>
  19. #include <LibJS/Runtime/TemporaryClearException.h>
  20. namespace JS {
  21. // 27.2.4.1.1 GetPromiseResolve ( promiseConstructor ), https://tc39.es/ecma262/#sec-getpromiseresolve
  22. static Value get_promise_resolve(GlobalObject& global_object, Value constructor)
  23. {
  24. VERIFY(constructor.is_constructor());
  25. auto& vm = global_object.vm();
  26. auto promise_resolve = constructor.get(global_object, vm.names.resolve);
  27. if (vm.exception())
  28. return {};
  29. if (!promise_resolve.is_function()) {
  30. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_resolve.to_string_without_side_effects());
  31. return {};
  32. }
  33. return promise_resolve;
  34. }
  35. // 27.2.1.1.1 IfAbruptRejectPromise ( value, capability ), https://tc39.es/ecma262/#sec-ifabruptrejectpromise
  36. static Optional<Value> if_abrupt_reject_promise(GlobalObject& global_object, Value, PromiseCapability capability)
  37. {
  38. auto& vm = global_object.vm();
  39. if (auto* exception = vm.exception()) {
  40. vm.clear_exception();
  41. vm.stop_unwind();
  42. (void)vm.call(*capability.reject, js_undefined(), exception->value());
  43. return capability.promise;
  44. }
  45. return {};
  46. }
  47. static bool iterator_record_is_complete(GlobalObject& global_object, Object& iterator_record)
  48. {
  49. auto& vm = global_object.vm();
  50. // FIXME: Create a native iterator structure with the [[Done]] internal slot. For now, temporarily clear
  51. // the exception so we can access the "done" property on the iterator object.
  52. TemporaryClearException clear_exception(vm);
  53. return iterator_complete(global_object, iterator_record);
  54. }
  55. static void set_iterator_record_complete(GlobalObject& global_object, Object& iterator_record)
  56. {
  57. auto& vm = global_object.vm();
  58. // FIXME: Create a native iterator structure with the [[Done]] internal slot. For now, temporarily clear
  59. // the exception so we can access the "done" property on the iterator object.
  60. TemporaryClearException clear_exception(vm);
  61. iterator_record.set(vm.names.done, Value(true), Object::ShouldThrowExceptions::No);
  62. }
  63. using EndOfElementsCallback = Function<Value(PromiseValueList&)>;
  64. using InvokeElementFunctionCallback = Function<void(PromiseValueList&, RemainingElements&, Value, size_t)>;
  65. static Value perform_promise_common(GlobalObject& global_object, Object& iterator_record, Value constructor, PromiseCapability result_capability, Value promise_resolve, EndOfElementsCallback end_of_list, InvokeElementFunctionCallback invoke_element_function)
  66. {
  67. auto& vm = global_object.vm();
  68. VERIFY(constructor.is_constructor());
  69. VERIFY(promise_resolve.is_function());
  70. auto* values = vm.heap().allocate_without_global_object<PromiseValueList>();
  71. auto* remaining_elements_count = vm.heap().allocate_without_global_object<RemainingElements>(1);
  72. size_t index = 0;
  73. while (true) {
  74. auto* next = iterator_step(global_object, iterator_record);
  75. if (vm.exception()) {
  76. set_iterator_record_complete(global_object, iterator_record);
  77. return {};
  78. }
  79. if (!next) {
  80. set_iterator_record_complete(global_object, iterator_record);
  81. if (vm.exception())
  82. return {};
  83. if (--remaining_elements_count->value == 0)
  84. return end_of_list(*values);
  85. return result_capability.promise;
  86. }
  87. auto next_value = iterator_value(global_object, *next);
  88. if (vm.exception()) {
  89. set_iterator_record_complete(global_object, iterator_record);
  90. return {};
  91. }
  92. values->values.append(js_undefined());
  93. auto next_promise = vm.call(promise_resolve.as_function(), constructor, next_value);
  94. if (vm.exception())
  95. return {};
  96. ++remaining_elements_count->value;
  97. invoke_element_function(*values, *remaining_elements_count, next_promise, index);
  98. if (vm.exception())
  99. return {};
  100. ++index;
  101. }
  102. }
  103. // 27.2.4.1.2 PerformPromiseAll ( iteratorRecord, constructor, resultCapability, promiseResolve ), https://tc39.es/ecma262/#sec-performpromiseall
  104. static Value perform_promise_all(GlobalObject& global_object, Object& iterator_record, Value constructor, PromiseCapability result_capability, Value promise_resolve)
  105. {
  106. auto& vm = global_object.vm();
  107. return perform_promise_common(
  108. global_object, iterator_record, constructor, result_capability, promise_resolve,
  109. [&](PromiseValueList& values) -> Value {
  110. auto values_array = Array::create_from(global_object, values.values);
  111. (void)vm.call(*result_capability.resolve, js_undefined(), values_array);
  112. if (vm.exception())
  113. return {};
  114. return result_capability.promise;
  115. },
  116. [&](PromiseValueList& values, RemainingElements& remaining_elements_count, Value next_promise, size_t index) {
  117. auto* on_fulfilled = PromiseAllResolveElementFunction::create(global_object, index, values, result_capability, remaining_elements_count);
  118. on_fulfilled->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  119. (void)next_promise.invoke(global_object, vm.names.then, on_fulfilled, result_capability.reject);
  120. });
  121. }
  122. // 27.2.4.3.1 PerformPromiseAny ( iteratorRecord, constructor, resultCapability, promiseResolve ), https://tc39.es/ecma262/#sec-performpromiseany
  123. static Value perform_promise_any(GlobalObject& global_object, Object& iterator_record, Value constructor, PromiseCapability result_capability, Value promise_resolve)
  124. {
  125. auto& vm = global_object.vm();
  126. return perform_promise_common(
  127. global_object, iterator_record, constructor, result_capability, promise_resolve,
  128. [&](PromiseValueList& errors) -> Value {
  129. auto errors_array = Array::create_from(global_object, errors.values);
  130. auto* error = AggregateError::create(global_object);
  131. error->define_property_or_throw(vm.names.errors, { .value = errors_array, .writable = true, .enumerable = false, .configurable = true });
  132. vm.throw_exception(global_object, error);
  133. return {};
  134. },
  135. [&](PromiseValueList& errors, RemainingElements& remaining_elements_count, Value next_promise, size_t index) {
  136. auto* on_rejected = PromiseAnyRejectElementFunction::create(global_object, index, errors, result_capability, remaining_elements_count);
  137. on_rejected->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  138. (void)next_promise.invoke(global_object, vm.names.then, result_capability.resolve, on_rejected);
  139. });
  140. }
  141. PromiseConstructor::PromiseConstructor(GlobalObject& global_object)
  142. : NativeFunction(vm().names.Promise.as_string(), *global_object.function_prototype())
  143. {
  144. }
  145. void PromiseConstructor::initialize(GlobalObject& global_object)
  146. {
  147. auto& vm = this->vm();
  148. NativeFunction::initialize(global_object);
  149. // 27.2.4.4 Promise.prototype, https://tc39.es/ecma262/#sec-promise.prototype
  150. define_direct_property(vm.names.prototype, global_object.promise_prototype(), 0);
  151. u8 attr = Attribute::Writable | Attribute::Configurable;
  152. define_native_function(vm.names.all, all, 1, attr);
  153. define_native_function(vm.names.any, any, 1, attr);
  154. // TODO: Implement these functions below and uncomment this.
  155. // define_native_function(vm.names.allSettled, all_settled, 1, attr);
  156. // define_native_function(vm.names.race, race, 1, attr);
  157. define_native_function(vm.names.reject, reject, 1, attr);
  158. define_native_function(vm.names.resolve, resolve, 1, attr);
  159. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  160. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  161. }
  162. // 27.2.3.1 Promise ( executor ), https://tc39.es/ecma262/#sec-promise-executor
  163. Value PromiseConstructor::call()
  164. {
  165. auto& vm = this->vm();
  166. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Promise);
  167. return {};
  168. }
  169. // 27.2.3.1 Promise ( executor ), https://tc39.es/ecma262/#sec-promise-executor
  170. Value PromiseConstructor::construct(FunctionObject& new_target)
  171. {
  172. auto& vm = this->vm();
  173. auto& global_object = this->global_object();
  174. auto executor = vm.argument(0);
  175. if (!executor.is_function()) {
  176. vm.throw_exception<TypeError>(global_object, ErrorType::PromiseExecutorNotAFunction);
  177. return {};
  178. }
  179. auto* promise = ordinary_create_from_constructor<Promise>(global_object, new_target, &GlobalObject::promise_prototype);
  180. if (vm.exception())
  181. return {};
  182. auto [resolve_function, reject_function] = promise->create_resolving_functions();
  183. (void)vm.call(executor.as_function(), js_undefined(), &resolve_function, &reject_function);
  184. if (auto* exception = vm.exception()) {
  185. vm.clear_exception();
  186. vm.stop_unwind();
  187. (void)vm.call(reject_function, js_undefined(), exception->value());
  188. }
  189. return promise;
  190. }
  191. // 27.2.4.1 Promise.all ( iterable ), https://tc39.es/ecma262/#sec-promise.all
  192. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all)
  193. {
  194. auto* constructor = vm.this_value(global_object).to_object(global_object);
  195. if (!constructor)
  196. return {};
  197. auto promise_capability = new_promise_capability(global_object, constructor);
  198. if (vm.exception())
  199. return {};
  200. auto promise_resolve = get_promise_resolve(global_object, constructor);
  201. if (auto abrupt = if_abrupt_reject_promise(global_object, promise_resolve, promise_capability); abrupt.has_value())
  202. return abrupt.value();
  203. auto iterator_record = get_iterator(global_object, vm.argument(0));
  204. if (auto abrupt = if_abrupt_reject_promise(global_object, iterator_record, promise_capability); abrupt.has_value())
  205. return abrupt.value();
  206. auto result = perform_promise_all(global_object, *iterator_record, constructor, promise_capability, promise_resolve);
  207. if (vm.exception()) {
  208. if (!iterator_record_is_complete(global_object, *iterator_record))
  209. iterator_close(*iterator_record);
  210. auto abrupt = if_abrupt_reject_promise(global_object, result, promise_capability);
  211. return abrupt.value();
  212. }
  213. return result;
  214. }
  215. // 27.2.4.2 Promise.allSettled ( iterable ), https://tc39.es/ecma262/#sec-promise.allsettled
  216. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all_settled)
  217. {
  218. TODO();
  219. }
  220. // 27.2.4.3 Promise.any ( iterable ), https://tc39.es/ecma262/#sec-promise.any
  221. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::any)
  222. {
  223. auto* constructor = vm.this_value(global_object).to_object(global_object);
  224. if (!constructor)
  225. return {};
  226. auto promise_capability = new_promise_capability(global_object, constructor);
  227. if (vm.exception())
  228. return {};
  229. auto promise_resolve = get_promise_resolve(global_object, constructor);
  230. if (auto abrupt = if_abrupt_reject_promise(global_object, promise_resolve, promise_capability); abrupt.has_value())
  231. return abrupt.value();
  232. auto iterator_record = get_iterator(global_object, vm.argument(0));
  233. if (auto abrupt = if_abrupt_reject_promise(global_object, iterator_record, promise_capability); abrupt.has_value())
  234. return abrupt.value();
  235. auto result = perform_promise_any(global_object, *iterator_record, constructor, promise_capability, promise_resolve);
  236. if (vm.exception()) {
  237. if (!iterator_record_is_complete(global_object, *iterator_record))
  238. iterator_close(*iterator_record);
  239. auto abrupt = if_abrupt_reject_promise(global_object, result, promise_capability);
  240. return abrupt.value();
  241. }
  242. return result;
  243. }
  244. // 27.2.4.5 Promise.race ( iterable ), https://tc39.es/ecma262/#sec-promise.race
  245. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::race)
  246. {
  247. TODO();
  248. }
  249. // 27.2.4.6 Promise.reject ( r ), https://tc39.es/ecma262/#sec-promise.reject
  250. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::reject)
  251. {
  252. auto* constructor = vm.this_value(global_object).to_object(global_object);
  253. if (!constructor)
  254. return {};
  255. auto promise_capability = new_promise_capability(global_object, constructor);
  256. if (vm.exception())
  257. return {};
  258. auto reason = vm.argument(0);
  259. [[maybe_unused]] auto result = vm.call(*promise_capability.reject, js_undefined(), reason);
  260. return promise_capability.promise;
  261. }
  262. // 27.2.4.7 Promise.resolve ( x ), https://tc39.es/ecma262/#sec-promise.resolve
  263. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::resolve)
  264. {
  265. auto* constructor = vm.this_value(global_object).to_object(global_object);
  266. if (!constructor)
  267. return {};
  268. auto value = vm.argument(0);
  269. return promise_resolve(global_object, *constructor, value);
  270. }
  271. // 27.2.4.8 get Promise [ @@species ], https://tc39.es/ecma262/#sec-get-promise-@@species
  272. JS_DEFINE_NATIVE_GETTER(PromiseConstructor::symbol_species_getter)
  273. {
  274. return vm.this_value(global_object);
  275. }
  276. }