PromiseJobs.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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& promise_capability = m_reaction.capability();
  28. auto type = m_reaction.type();
  29. auto handler = m_reaction.handler();
  30. Value handler_result;
  31. if (!handler.has_value()) {
  32. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Handler is empty", this);
  33. switch (type) {
  34. case PromiseReaction::Type::Fulfill:
  35. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Fulfill, setting handler result to {}", this, m_argument);
  36. handler_result = m_argument;
  37. break;
  38. case PromiseReaction::Type::Reject:
  39. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction type is Type::Reject, throwing exception with argument {}", this, m_argument);
  40. vm.throw_exception(global_object(), m_argument);
  41. // handler_result is set to exception value further below
  42. break;
  43. }
  44. } else {
  45. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling handler callback {} @ {} with argument {}", this, handler.value().callback->class_name(), handler.value().callback, m_argument);
  46. handler_result = call_job_callback(vm, handler.value(), js_undefined(), m_argument);
  47. }
  48. if (!promise_capability.has_value()) {
  49. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Reaction has no PromiseCapability, returning empty value", this);
  50. // TODO: This can't return an empty value at the moment, because the implicit conversion to Completion would fail.
  51. // Change it back when this is using completions (`return normal_completion({})`)
  52. return js_undefined();
  53. }
  54. if (vm.exception()) {
  55. handler_result = vm.exception()->value();
  56. vm.clear_exception();
  57. vm.stop_unwind();
  58. auto* reject_function = promise_capability.value().reject;
  59. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
  60. return vm.call(*reject_function, js_undefined(), handler_result);
  61. } else {
  62. auto* resolve_function = promise_capability.value().resolve;
  63. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
  64. return vm.call(*resolve_function, js_undefined(), handler_result);
  65. }
  66. }
  67. void PromiseReactionJob::visit_edges(Visitor& visitor)
  68. {
  69. Base::visit_edges(visitor);
  70. visitor.visit(&m_reaction);
  71. visitor.visit(m_argument);
  72. }
  73. PromiseResolveThenableJob* PromiseResolveThenableJob::create(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
  74. {
  75. // FIXME: A bunch of stuff regarding realms, see step 2-5 in the spec linked below
  76. return global_object.heap().allocate<PromiseResolveThenableJob>(global_object, promise_to_resolve, thenable, then, *global_object.function_prototype());
  77. }
  78. PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve, Value thenable, JobCallback then, Object& prototype)
  79. : NativeFunction(prototype)
  80. , m_promise_to_resolve(promise_to_resolve)
  81. , m_thenable(thenable)
  82. , m_then(then)
  83. {
  84. }
  85. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  86. ThrowCompletionOr<Value> PromiseResolveThenableJob::call()
  87. {
  88. auto& vm = this->vm();
  89. auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions();
  90. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable);
  91. auto then_call_result = call_job_callback(vm, m_then, m_thenable, &resolve_function, &reject_function);
  92. if (vm.exception()) {
  93. auto error = vm.exception()->value();
  94. vm.clear_exception();
  95. vm.stop_unwind();
  96. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: An exception was thrown, returning error {}", this, error);
  97. return error;
  98. }
  99. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result);
  100. return then_call_result;
  101. }
  102. void PromiseResolveThenableJob::visit_edges(Visitor& visitor)
  103. {
  104. Base::visit_edges(visitor);
  105. visitor.visit(&m_promise_to_resolve);
  106. visitor.visit(m_thenable);
  107. visitor.visit(m_then.callback);
  108. }
  109. }