Promise.cpp 7.8 KB

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