Promise.cpp 7.8 KB

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