PromiseJobs.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/JobCallback.h>
  11. #include <LibJS/Runtime/Promise.h>
  12. #include <LibJS/Runtime/PromiseJobs.h>
  13. #include <LibJS/Runtime/PromiseReaction.h>
  14. namespace JS {
  15. // 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
  16. static ThrowCompletionOr<Value> run_reaction_job(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
  17. {
  18. auto& vm = global_object.vm();
  19. // a. Let promiseCapability be reaction.[[Capability]].
  20. auto& promise_capability = reaction.capability();
  21. // b. Let type be reaction.[[Type]].
  22. auto type = reaction.type();
  23. // c. Let handler be reaction.[[Handler]].
  24. auto& handler = reaction.handler();
  25. Completion handler_result;
  26. // d. If handler is empty, then
  27. if (!handler.has_value()) {
  28. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Handler is empty");
  29. // i. If type is Fulfill, let handlerResult be NormalCompletion(argument).
  30. if (type == PromiseReaction::Type::Fulfill) {
  31. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Reaction type is Type::Fulfill, setting handler result to {}", argument);
  32. handler_result = normal_completion(argument);
  33. }
  34. // ii. Else,
  35. else {
  36. // 1. Assert: type is Reject.
  37. VERIFY(type == PromiseReaction::Type::Reject);
  38. // 2. Let handlerResult be ThrowCompletion(argument).
  39. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Reaction type is Type::Reject, throwing exception with argument {}", argument);
  40. handler_result = throw_completion(argument);
  41. }
  42. }
  43. // e. Else, let handlerResult be HostCallJobCallback(handler, undefined, « argument »).
  44. else {
  45. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Calling handler callback {} @ {} with argument {}", handler.value().callback.cell()->class_name(), handler.value().callback.cell(), argument);
  46. MarkedVector<Value> arguments(vm.heap());
  47. arguments.append(argument);
  48. handler_result = vm.host_call_job_callback(global_object, handler.value(), js_undefined(), move(arguments));
  49. }
  50. // f. If promiseCapability is undefined, then
  51. if (!promise_capability.has_value()) {
  52. // i. Assert: handlerResult is not an abrupt completion.
  53. VERIFY(!handler_result.is_abrupt());
  54. // ii. Return NormalCompletion(empty).
  55. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Reaction has no PromiseCapability, returning empty value");
  56. // TODO: This can't return an empty value at the moment, because the implicit conversion to Completion would fail.
  57. // Change it back when this is using completions (`return normal_completion({})`)
  58. return js_undefined();
  59. }
  60. // g. Assert: promiseCapability is a PromiseCapability Record.
  61. // h. If handlerResult is an abrupt completion, then
  62. if (handler_result.is_abrupt()) {
  63. // i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
  64. auto* reject_function = promise_capability.value().reject;
  65. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Calling PromiseCapability's reject function @ {}", reject_function);
  66. return JS::call(global_object, *reject_function, js_undefined(), *handler_result.value());
  67. }
  68. // i. Else,
  69. else {
  70. // i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
  71. auto* resolve_function = promise_capability.value().resolve;
  72. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob]: Calling PromiseCapability's resolve function @ {}", resolve_function);
  73. return JS::call(global_object, *resolve_function, js_undefined(), *handler_result.value());
  74. }
  75. // j. Return Completion(status).
  76. }
  77. // 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
  78. PromiseJob create_promise_reaction_job(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
  79. {
  80. // 1. Let job be a new Job Abstract Closure with no parameters that captures reaction and argument and performs the following steps when called:
  81. // See run_reaction_job for "the following steps".
  82. auto job = [global_object = JS::make_handle(&global_object), reaction = JS::make_handle(&reaction), argument = JS::make_handle(argument)]() mutable {
  83. return run_reaction_job(*global_object.cell(), *reaction.cell(), argument.value());
  84. };
  85. // 2. Let handlerRealm be null.
  86. Realm* handler_realm { nullptr };
  87. // 3. If reaction.[[Handler]] is not empty, then
  88. auto& handler = reaction.handler();
  89. if (handler.has_value()) {
  90. // a. Let getHandlerRealmResult be GetFunctionRealm(reaction.[[Handler]].[[Callback]]).
  91. auto get_handler_realm_result = get_function_realm(global_object, *handler->callback.cell());
  92. // b. If getHandlerRealmResult is a normal completion, set handlerRealm to getHandlerRealmResult.[[Value]].
  93. if (!get_handler_realm_result.is_throw_completion()) {
  94. handler_realm = get_handler_realm_result.release_value();
  95. } else {
  96. // c. Else, set handlerRealm to the current Realm Record.
  97. handler_realm = global_object.vm().current_realm();
  98. }
  99. // d. NOTE: handlerRealm is never null unless the handler is undefined. When the handler is a revoked Proxy and no ECMAScript code runs, handlerRealm is used to create error objects.
  100. }
  101. // 4. Return the Record { [[Job]]: job, [[Realm]]: handlerRealm }.
  102. return { move(job), handler_realm };
  103. }
  104. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  105. static ThrowCompletionOr<Value> run_resolve_thenable_job(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback& then)
  106. {
  107. auto& vm = global_object.vm();
  108. // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
  109. auto [resolve_function, reject_function] = promise_to_resolve.create_resolving_functions();
  110. // b. Let thenCallResult be HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »).
  111. dbgln_if(PROMISE_DEBUG, "run_resolve_thenable_job: Calling then job callback for thenable {}", &thenable);
  112. MarkedVector<Value> arguments(vm.heap());
  113. arguments.append(Value(&resolve_function));
  114. arguments.append(Value(&reject_function));
  115. auto then_call_result = vm.host_call_job_callback(global_object, then, thenable, move(arguments));
  116. // c. If thenCallResult is an abrupt completion, then
  117. if (then_call_result.is_error()) {
  118. // i. Let status be Call(resolvingFunctions.[[Reject]], undefined, « thenCallResult.[[Value]] »).
  119. dbgln_if(PROMISE_DEBUG, "run_resolve_thenable_job: then_call_result is an abrupt completion, calling reject function with value {}", *then_call_result.throw_completion().value());
  120. auto status = JS::call(global_object, &reject_function, js_undefined(), *then_call_result.throw_completion().value());
  121. // ii. Return Completion(status).
  122. return status;
  123. }
  124. // d. Return Completion(thenCallResult).
  125. dbgln_if(PROMISE_DEBUG, "run_resolve_thenable_job: Returning then call result {}", then_call_result.value());
  126. return then_call_result;
  127. }
  128. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  129. PromiseJob create_promise_resolve_thenable_job(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
  130. {
  131. // 2. Let getThenRealmResult be GetFunctionRealm(then.[[Callback]]).
  132. Realm* then_realm { nullptr };
  133. auto get_then_realm_result = get_function_realm(global_object, *then.callback.cell());
  134. // 3. If getThenRealmResult is a normal completion, let thenRealm be getThenRealmResult.[[Value]].
  135. if (!get_then_realm_result.is_throw_completion()) {
  136. then_realm = get_then_realm_result.release_value();
  137. } else {
  138. // 4. Else, let thenRealm be the current Realm Record.
  139. then_realm = global_object.vm().current_realm();
  140. }
  141. // 5. NOTE: thenRealm is never null. When then.[[Callback]] is a revoked Proxy and no code runs, thenRealm is used to create error objects.
  142. VERIFY(then_realm);
  143. // 1. Let job be a new Job Abstract Closure with no parameters that captures promiseToResolve, thenable, and then and performs the following steps when called:
  144. // See PromiseResolveThenableJob::call() for "the following steps".
  145. // NOTE: This is done out of order, since `then` is moved into the lambda and `then` would be invalid if it was done at the start.
  146. auto job = [global_object = JS::make_handle(&global_object), promise_to_resolve = JS::make_handle(&promise_to_resolve), thenable = JS::make_handle(thenable), then = move(then)]() mutable {
  147. return run_resolve_thenable_job(*global_object.cell(), *promise_to_resolve.cell(), thenable.value(), then);
  148. };
  149. // 6. Return the Record { [[Job]]: job, [[Realm]]: thenRealm }.
  150. return { move(job), then_realm };
  151. }
  152. }