Promise.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Function.h>
  7. #include <AK/TypeCasts.h>
  8. #include <LibJS/Runtime/PromiseCapability.h>
  9. #include <LibJS/Runtime/PromiseConstructor.h>
  10. #include <LibJS/Runtime/Realm.h>
  11. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  12. #include <LibWeb/Bindings/HostDefined.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. #include <LibWeb/WebIDL/Promise.h>
  15. namespace Web::WebIDL {
  16. // https://webidl.spec.whatwg.org/#a-new-promise
  17. JS::NonnullGCPtr<Promise> create_promise(JS::Realm& realm)
  18. {
  19. auto& vm = realm.vm();
  20. // 1. Let constructor be realm.[[Intrinsics]].[[%Promise%]].
  21. auto constructor = realm.intrinsics().promise_constructor();
  22. // Return ? NewPromiseCapability(constructor).
  23. // NOTE: When called with %Promise%, NewPromiseCapability can't throw.
  24. return MUST(JS::new_promise_capability(vm, constructor));
  25. }
  26. // https://webidl.spec.whatwg.org/#a-promise-resolved-with
  27. JS::NonnullGCPtr<Promise> create_resolved_promise(JS::Realm& realm, JS::Value value)
  28. {
  29. auto& vm = realm.vm();
  30. // 1. Let value be the result of converting x to an ECMAScript value.
  31. // 2. Let constructor be realm.[[Intrinsics]].[[%Promise%]].
  32. auto constructor = realm.intrinsics().promise_constructor();
  33. // 3. Let promiseCapability be ? NewPromiseCapability(constructor).
  34. // NOTE: When called with %Promise%, NewPromiseCapability can't throw.
  35. auto promise_capability = MUST(JS::new_promise_capability(vm, constructor));
  36. // 4. Perform ! Call(promiseCapability.[[Resolve]], undefined, « value »).
  37. MUST(JS::call(vm, *promise_capability->resolve(), JS::js_undefined(), value));
  38. // 5. Return promiseCapability.
  39. return promise_capability;
  40. }
  41. // https://webidl.spec.whatwg.org/#a-promise-rejected-with
  42. JS::NonnullGCPtr<Promise> create_rejected_promise(JS::Realm& realm, JS::Value reason)
  43. {
  44. auto& vm = realm.vm();
  45. // 1. Let constructor be realm.[[Intrinsics]].[[%Promise%]].
  46. auto constructor = realm.intrinsics().promise_constructor();
  47. // 2. Let promiseCapability be ? NewPromiseCapability(constructor).
  48. // NOTE: When called with %Promise%, NewPromiseCapability can't throw.
  49. auto promise_capability = MUST(JS::new_promise_capability(vm, constructor));
  50. // 3. Perform ! Call(promiseCapability.[[Reject]], undefined, « r »).
  51. MUST(JS::call(vm, *promise_capability->reject(), JS::js_undefined(), reason));
  52. // 4. Return promiseCapability.
  53. return promise_capability;
  54. }
  55. // https://webidl.spec.whatwg.org/#resolve
  56. void resolve_promise(JS::Realm& realm, Promise const& promise, JS::Value value)
  57. {
  58. auto& vm = realm.vm();
  59. // 1. If x is not given, then let it be the undefined value.
  60. // NOTE: This is done via the default argument.
  61. // 2. Let value be the result of converting x to an ECMAScript value.
  62. // 3. Perform ! Call(p.[[Resolve]], undefined, « value »).
  63. MUST(JS::call(vm, *promise.resolve(), JS::js_undefined(), value));
  64. }
  65. // https://webidl.spec.whatwg.org/#reject
  66. void reject_promise(JS::Realm& realm, Promise const& promise, JS::Value reason)
  67. {
  68. auto& vm = realm.vm();
  69. // 1. Perform ! Call(p.[[Reject]], undefined, « r »).
  70. MUST(JS::call(vm, *promise.reject(), JS::js_undefined(), reason));
  71. }
  72. // https://webidl.spec.whatwg.org/#dfn-perform-steps-once-promise-is-settled
  73. JS::NonnullGCPtr<JS::Promise> react_to_promise(Promise const& promise, Optional<ReactionSteps> on_fulfilled_callback, Optional<ReactionSteps> on_rejected_callback)
  74. {
  75. auto& realm = promise.promise()->shape().realm();
  76. auto& vm = realm.vm();
  77. // 1. Let onFulfilledSteps be the following steps given argument V:
  78. auto on_fulfilled_steps = [on_fulfilled_callback = move(on_fulfilled_callback)](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> {
  79. // 1. Let value be the result of converting V to an IDL value of type T.
  80. auto value = vm.argument(0);
  81. // 2. If there is a set of steps to be run if the promise was fulfilled, then let result be the result of performing them, given value if T is not undefined. Otherwise, let result be value.
  82. auto result = on_fulfilled_callback.has_value()
  83. ? TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return (*on_fulfilled_callback)(value); }))
  84. : value;
  85. // 3. Return result, converted to an ECMAScript value.
  86. return result;
  87. };
  88. // 2. Let onFulfilled be CreateBuiltinFunction(onFulfilledSteps, « »):
  89. auto on_fulfilled = JS::NativeFunction::create(realm, move(on_fulfilled_steps), 1, "");
  90. // 3. Let onRejectedSteps be the following steps given argument R:
  91. auto on_rejected_steps = [&realm, on_rejected_callback = move(on_rejected_callback)](JS::VM& vm) -> JS::ThrowCompletionOr<JS::Value> {
  92. // 1. Let reason be the result of converting R to an IDL value of type any.
  93. auto reason = vm.argument(0);
  94. // 2. If there is a set of steps to be run if the promise was rejected, then let result be the result of performing them, given reason. Otherwise, let result be a promise rejected with reason.
  95. auto result = on_rejected_callback.has_value()
  96. ? TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return (*on_rejected_callback)(reason); }))
  97. : WebIDL::create_rejected_promise(realm, reason)->promise();
  98. // 3. Return result, converted to an ECMAScript value.
  99. return result;
  100. };
  101. // 4. Let onRejected be CreateBuiltinFunction(onRejectedSteps, « »):
  102. auto on_rejected = JS::NativeFunction::create(realm, move(on_rejected_steps), 1, "");
  103. // 5. Let constructor be promise.[[Promise]].[[Realm]].[[Intrinsics]].[[%Promise%]].
  104. auto constructor = realm.intrinsics().promise_constructor();
  105. // 6. Let newCapability be ? NewPromiseCapability(constructor).
  106. // NOTE: When called with %Promise%, NewPromiseCapability can't throw.
  107. auto new_capability = MUST(JS::new_promise_capability(vm, constructor));
  108. // 7. Return PerformPromiseThen(promise.[[Promise]], onFulfilled, onRejected, newCapability).
  109. auto promise_object = verify_cast<JS::Promise>(promise.promise().ptr());
  110. auto value = promise_object->perform_then(on_fulfilled, on_rejected, new_capability);
  111. return verify_cast<JS::Promise>(value.as_object());
  112. }
  113. // https://webidl.spec.whatwg.org/#upon-fulfillment
  114. JS::NonnullGCPtr<JS::Promise> upon_fulfillment(Promise const& promise, ReactionSteps steps)
  115. {
  116. // 1. Return the result of reacting to promise:
  117. return react_to_promise(promise,
  118. // - If promise was fulfilled with value v, then:
  119. [steps = move(steps)](auto value) {
  120. // 1. Perform steps with v.
  121. // NOTE: The `return` is not immediately obvious, but `steps` may be something like
  122. // "Return the result of ...", which we also need to do _here_.
  123. return steps(value);
  124. },
  125. {});
  126. }
  127. // https://webidl.spec.whatwg.org/#upon-rejection
  128. JS::NonnullGCPtr<JS::Promise> upon_rejection(Promise const& promise, ReactionSteps steps)
  129. {
  130. // 1. Return the result of reacting to promise:
  131. return react_to_promise(promise, {},
  132. // - If promise was rejected with reason r, then:
  133. [steps = move(steps)](auto reason) {
  134. // 1. Perform steps with r.
  135. // NOTE: The `return` is not immediately obvious, but `steps` may be something like
  136. // "Return the result of ...", which we also need to do _here_.
  137. return steps(reason);
  138. });
  139. }
  140. // https://webidl.spec.whatwg.org/#mark-a-promise-as-handled
  141. void mark_promise_as_handled(Promise const& promise)
  142. {
  143. // To mark as handled a Promise<T> promise, set promise.[[Promise]].[[PromiseIsHandled]] to true.
  144. auto promise_object = verify_cast<JS::Promise>(promise.promise().ptr());
  145. promise_object->set_is_handled();
  146. }
  147. }