PromiseJobs.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/JobCallback.h>
  9. #include <LibJS/Runtime/Promise.h>
  10. #include <LibJS/Runtime/PromiseJobs.h>
  11. #include <LibJS/Runtime/PromiseReaction.h>
  12. namespace JS {
  13. PromiseReactionJob* PromiseReactionJob::create(GlobalObject& global_object, PromiseReaction& reaction, Value argument)
  14. {
  15. return global_object.heap().allocate<PromiseReactionJob>(global_object, reaction, argument, *global_object.function_prototype());
  16. }
  17. PromiseReactionJob::PromiseReactionJob(PromiseReaction& reaction, Value argument, Object& prototype)
  18. : NativeFunction(prototype)
  19. , m_reaction(reaction)
  20. , m_argument(argument)
  21. {
  22. }
  23. // 27.2.2.1 NewPromiseReactionJob ( reaction, argument ), https://tc39.es/ecma262/#sec-newpromisereactionjob
  24. ThrowCompletionOr<Value> PromiseReactionJob::call()
  25. {
  26. auto& vm = this->vm();
  27. auto& global_object = this->global_object();
  28. // a. Let promiseCapability be reaction.[[Capability]].
  29. auto& promise_capability = m_reaction.capability();
  30. // b. Let type be reaction.[[Type]].
  31. auto type = m_reaction.type();
  32. // c. Let handler be reaction.[[Handler]].
  33. auto handler = m_reaction.handler();
  34. Completion handler_result;
  35. // d. If handler is empty, then
  36. if (!handler.has_value()) {
  37. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Handler is empty", this);
  38. // i. If type is Fulfill, let handlerResult be NormalCompletion(argument).
  39. if (type == PromiseReaction::Type::Fulfill) {
  40. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument);
  41. handler_result = normal_completion(m_argument);
  42. }
  43. // ii. Else,
  44. else {
  45. // 1. Assert: type is Reject.
  46. VERIFY(type == PromiseReaction::Type::Reject);
  47. // 2. Let handlerResult be ThrowCompletion(argument).
  48. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument);
  49. handler_result = throw_completion(m_argument);
  50. }
  51. }
  52. // e. Else, let handlerResult be HostCallJobCallback(handler, undefined, « argument »).
  53. else {
  54. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling handler callback {} @ {} with argument {}", this, handler.value().callback->class_name(), handler.value().callback, m_argument);
  55. handler_result = call_job_callback(global_object, handler.value(), js_undefined(), m_argument);
  56. }
  57. // f. If promiseCapability is undefined, then
  58. if (!promise_capability.has_value()) {
  59. // i. Assert: handlerResult is not an abrupt completion.
  60. VERIFY(!vm.exception());
  61. // ii. Return NormalCompletion(empty).
  62. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction has no PromiseCapability, returning empty value", this);
  63. // TODO: This can't return an empty value at the moment, because the implicit conversion to Completion would fail.
  64. // Change it back when this is using completions (`return normal_completion({})`)
  65. return js_undefined();
  66. }
  67. // g. Assert: promiseCapability is a PromiseCapability Record.
  68. // h. If handlerResult is an abrupt completion, then
  69. if (handler_result.is_abrupt()) {
  70. vm.clear_exception();
  71. vm.stop_unwind();
  72. // i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
  73. auto* reject_function = promise_capability.value().reject;
  74. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
  75. return vm.call(*reject_function, js_undefined(), handler_result.value());
  76. }
  77. // i. Else,
  78. else {
  79. // i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
  80. auto* resolve_function = promise_capability.value().resolve;
  81. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
  82. return vm.call(*resolve_function, js_undefined(), handler_result.value());
  83. }
  84. // j. Return Completion(status).
  85. }
  86. void PromiseReactionJob::visit_edges(Visitor& visitor)
  87. {
  88. Base::visit_edges(visitor);
  89. visitor.visit(&m_reaction);
  90. visitor.visit(m_argument);
  91. }
  92. PromiseResolveThenableJob* PromiseResolveThenableJob::create(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
  93. {
  94. // FIXME: A bunch of stuff regarding realms, see step 2-5 in the spec linked below
  95. return global_object.heap().allocate<PromiseResolveThenableJob>(global_object, promise_to_resolve, thenable, then, *global_object.function_prototype());
  96. }
  97. PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve, Value thenable, JobCallback then, Object& prototype)
  98. : NativeFunction(prototype)
  99. , m_promise_to_resolve(promise_to_resolve)
  100. , m_thenable(thenable)
  101. , m_then(then)
  102. {
  103. }
  104. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  105. ThrowCompletionOr<Value> PromiseResolveThenableJob::call()
  106. {
  107. auto& vm = this->vm();
  108. auto& global_object = this->global_object();
  109. // a. Let resolvingFunctions be CreateResolvingFunctions(promiseToResolve).
  110. auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions();
  111. // b. Let thenCallResult be HostCallJobCallback(then, thenable, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »).
  112. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable);
  113. auto then_call_result = call_job_callback(global_object, m_then, m_thenable, &resolve_function, &reject_function);
  114. // c. If thenCallResult is an abrupt completion, then
  115. if (then_call_result.is_error()) {
  116. auto error = then_call_result.throw_completion().value();
  117. vm.clear_exception();
  118. vm.stop_unwind();
  119. // i. Let status be Call(resolvingFunctions.[[Reject]], undefined, « thenCallResult.[[Value]] »).
  120. // FIXME: Actually do this... not sure why we don't? :yakfused:
  121. // ii. Return Completion(status).
  122. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: An exception was thrown, returning error {}", this, error);
  123. return error;
  124. }
  125. // d. Return Completion(thenCallResult).
  126. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result.value());
  127. return then_call_result;
  128. }
  129. void PromiseResolveThenableJob::visit_edges(Visitor& visitor)
  130. {
  131. Base::visit_edges(visitor);
  132. visitor.visit(&m_promise_to_resolve);
  133. visitor.visit(m_thenable);
  134. visitor.visit(m_then.callback);
  135. }
  136. }