PromiseJobs.cpp 8.5 KB

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