PromiseJobs.cpp 9.1 KB

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