PromiseJobs.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/PromiseCapability.h>
  13. #include <LibJS/Runtime/PromiseJobs.h>
  14. #include <LibJS/Runtime/PromiseReaction.h>
  15. namespace JS {
  16. // 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
  17. static ThrowCompletionOr<Value> run_reaction_job(VM& vm, PromiseReaction& reaction, Value argument)
  18. {
  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 Completion(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(handler.value(), js_undefined(), move(arguments));
  49. }
  50. // f. If promiseCapability is undefined, then
  51. if (promise_capability == nullptr) {
  52. // i. Assert: handlerResult is not an abrupt completion.
  53. VERIFY(!handler_result.is_abrupt());
  54. // ii. Return 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. Return ? Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
  64. auto reject_function = promise_capability->reject();
  65. dbgln_if(PROMISE_DEBUG, "run_reaction_job: Calling PromiseCapability's reject function @ {}", reject_function.ptr());
  66. return call(vm, *reject_function, js_undefined(), *handler_result.value());
  67. }
  68. // i. Else,
  69. else {
  70. // i. Return ? Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
  71. auto resolve_function = promise_capability->resolve();
  72. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob]: Calling PromiseCapability's resolve function @ {}", resolve_function.ptr());
  73. return call(vm, *resolve_function, js_undefined(), *handler_result.value());
  74. }
  75. }
  76. // 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
  77. PromiseJob create_promise_reaction_job(VM& vm, PromiseReaction& reaction, Value argument)
  78. {
  79. // 1. Let job be a new Job Abstract Closure with no parameters that captures reaction and argument and performs the following steps when called:
  80. // See run_reaction_job for "the following steps".
  81. auto job = [&vm, reaction = make_handle(&reaction), argument = make_handle(argument)] {
  82. return run_reaction_job(vm, *reaction.cell(), argument.value());
  83. };
  84. // 2. Let handlerRealm be null.
  85. Realm* handler_realm { nullptr };
  86. // 3. If reaction.[[Handler]] is not empty, then
  87. auto& handler = reaction.handler();
  88. if (handler.has_value()) {
  89. // a. Let getHandlerRealmResult be Completion(GetFunctionRealm(reaction.[[Handler]].[[Callback]])).
  90. auto get_handler_realm_result = get_function_realm(vm, *handler->callback.cell());
  91. // b. If getHandlerRealmResult is a normal completion, set handlerRealm to getHandlerRealmResult.[[Value]].
  92. if (!get_handler_realm_result.is_throw_completion()) {
  93. handler_realm = get_handler_realm_result.release_value();
  94. } else {
  95. // c. Else, set handlerRealm to the current Realm Record.
  96. handler_realm = vm.current_realm();
  97. }
  98. // 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.
  99. }
  100. // 4. Return the Record { [[Job]]: job, [[Realm]]: handlerRealm }.
  101. return { move(job), handler_realm };
  102. }
  103. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  104. static ThrowCompletionOr<Value> run_resolve_thenable_job(VM& vm, Promise& promise_to_resolve, Value thenable, JobCallback& then)
  105. {
  106. // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
  107. auto [resolve_function, reject_function] = promise_to_resolve.create_resolving_functions();
  108. // b. Let thenCallResult be Completion(HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).
  109. dbgln_if(PROMISE_DEBUG, "run_resolve_thenable_job: Calling then job callback for thenable {}", &thenable);
  110. MarkedVector<Value> arguments(vm.heap());
  111. arguments.append(Value(resolve_function));
  112. arguments.append(Value(reject_function));
  113. auto then_call_result = vm.host_call_job_callback(then, thenable, move(arguments));
  114. // c. If thenCallResult is an abrupt completion, then
  115. if (then_call_result.is_error()) {
  116. // i. Return ? Call(resolvingFunctions.[[Reject]], undefined, « thenCallResult.[[Value]] »).
  117. 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());
  118. return call(vm, *reject_function, js_undefined(), *then_call_result.throw_completion().value());
  119. }
  120. // d. Return ? thenCallResult.
  121. dbgln_if(PROMISE_DEBUG, "run_resolve_thenable_job: Returning then call result {}", then_call_result.value());
  122. return then_call_result;
  123. }
  124. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  125. PromiseJob create_promise_resolve_thenable_job(VM& vm, Promise& promise_to_resolve, Value thenable, JobCallback then)
  126. {
  127. // 2. Let getThenRealmResult be Completion(GetFunctionRealm(then.[[Callback]])).
  128. auto get_then_realm_result = get_function_realm(vm, *then.callback.cell());
  129. Realm* then_realm;
  130. // 3. If getThenRealmResult is a normal completion, let thenRealm be getThenRealmResult.[[Value]].
  131. if (!get_then_realm_result.is_throw_completion()) {
  132. then_realm = get_then_realm_result.release_value();
  133. } else {
  134. // 4. Else, let thenRealm be the current Realm Record.
  135. then_realm = vm.current_realm();
  136. }
  137. // 5. NOTE: thenRealm is never null. When then.[[Callback]] is a revoked Proxy and no code runs, thenRealm is used to create error objects.
  138. VERIFY(then_realm);
  139. // 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:
  140. // See run_resolve_thenable_job() for "the following steps".
  141. auto job = [&vm, promise_to_resolve = make_handle(promise_to_resolve), thenable = make_handle(thenable), then = move(then)]() mutable {
  142. return run_resolve_thenable_job(vm, *promise_to_resolve.cell(), thenable.value(), then);
  143. };
  144. // 6. Return the Record { [[Job]]: job, [[Realm]]: thenRealm }.
  145. return { move(job), then_realm };
  146. }
  147. }