PromiseJobs.cpp 6.8 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. // a. Let promiseCapability be reaction.[[Capability]].
  28. auto& promise_capability = m_reaction.capability();
  29. // b. Let type be reaction.[[Type]].
  30. auto type = m_reaction.type();
  31. // c. Let handler be reaction.[[Handler]].
  32. auto handler = m_reaction.handler();
  33. Value handler_result;
  34. // d. If handler is empty, then
  35. if (!handler.has_value()) {
  36. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Handler is empty", this);
  37. // i. If type is Fulfill, let handlerResult be NormalCompletion(argument).
  38. if (type == PromiseReaction::Type::Fulfill) {
  39. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument);
  40. handler_result = m_argument;
  41. }
  42. // ii. Else,
  43. else {
  44. // 1. Assert: type is Reject.
  45. VERIFY(type == PromiseReaction::Type::Reject);
  46. // 2. Let handlerResult be ThrowCompletion(argument).
  47. // NOTE: handler_result is set to exception value further below
  48. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument);
  49. vm.throw_exception(global_object(), 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(vm, 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 (vm.exception()) {
  70. handler_result = vm.exception()->value();
  71. vm.clear_exception();
  72. vm.stop_unwind();
  73. // i. Let status be Call(promiseCapability.[[Reject]], undefined, « handlerResult.[[Value]] »).
  74. auto* reject_function = promise_capability.value().reject;
  75. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
  76. return vm.call(*reject_function, js_undefined(), handler_result);
  77. }
  78. // i. Else,
  79. else {
  80. // i. Let status be Call(promiseCapability.[[Resolve]], undefined, « handlerResult.[[Value]] »).
  81. auto* resolve_function = promise_capability.value().resolve;
  82. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
  83. return vm.call(*resolve_function, js_undefined(), handler_result);
  84. }
  85. // j. Return Completion(status).
  86. }
  87. void PromiseReactionJob::visit_edges(Visitor& visitor)
  88. {
  89. Base::visit_edges(visitor);
  90. visitor.visit(&m_reaction);
  91. visitor.visit(m_argument);
  92. }
  93. PromiseResolveThenableJob* PromiseResolveThenableJob::create(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
  94. {
  95. // FIXME: A bunch of stuff regarding realms, see step 2-5 in the spec linked below
  96. return global_object.heap().allocate<PromiseResolveThenableJob>(global_object, promise_to_resolve, thenable, then, *global_object.function_prototype());
  97. }
  98. PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve, Value thenable, JobCallback then, Object& prototype)
  99. : NativeFunction(prototype)
  100. , m_promise_to_resolve(promise_to_resolve)
  101. , m_thenable(thenable)
  102. , m_then(then)
  103. {
  104. }
  105. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  106. ThrowCompletionOr<Value> PromiseResolveThenableJob::call()
  107. {
  108. auto& vm = this->vm();
  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(vm, m_then, m_thenable, &resolve_function, &reject_function);
  114. // c. If thenCallResult is an abrupt completion, then
  115. if (vm.exception()) {
  116. auto error = vm.exception()->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);
  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. }