PromiseJobs.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. 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. return {};
  51. }
  52. if (vm.exception()) {
  53. handler_result = vm.exception()->value();
  54. vm.clear_exception();
  55. vm.stop_unwind();
  56. auto* reject_function = promise_capability.value().reject;
  57. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's reject function @ {}", this, reject_function);
  58. return vm.call(*reject_function, js_undefined(), handler_result);
  59. } else {
  60. auto* resolve_function = promise_capability.value().resolve;
  61. dbgln_if(PROMISE_DEBUG, "[PromiseReactionJob @ {}]: Calling PromiseCapability's resolve function @ {}", this, resolve_function);
  62. return vm.call(*resolve_function, js_undefined(), handler_result);
  63. }
  64. }
  65. void PromiseReactionJob::visit_edges(Visitor& visitor)
  66. {
  67. Base::visit_edges(visitor);
  68. visitor.visit(&m_reaction);
  69. visitor.visit(m_argument);
  70. }
  71. PromiseResolveThenableJob* PromiseResolveThenableJob::create(GlobalObject& global_object, Promise& promise_to_resolve, Value thenable, JobCallback then)
  72. {
  73. // FIXME: A bunch of stuff regarding realms, see step 2-5 in the spec linked below
  74. return global_object.heap().allocate<PromiseResolveThenableJob>(global_object, promise_to_resolve, thenable, then, *global_object.function_prototype());
  75. }
  76. PromiseResolveThenableJob::PromiseResolveThenableJob(Promise& promise_to_resolve, Value thenable, JobCallback then, Object& prototype)
  77. : NativeFunction(prototype)
  78. , m_promise_to_resolve(promise_to_resolve)
  79. , m_thenable(thenable)
  80. , m_then(then)
  81. {
  82. }
  83. // 27.2.2.2 NewPromiseResolveThenableJob ( promiseToResolve, thenable, then ), https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob
  84. Value PromiseResolveThenableJob::call()
  85. {
  86. auto& vm = this->vm();
  87. auto [resolve_function, reject_function] = m_promise_to_resolve.create_resolving_functions();
  88. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Calling then job callback for thenable {}", this, &m_thenable);
  89. auto then_call_result = call_job_callback(vm, m_then, m_thenable, &resolve_function, &reject_function);
  90. if (vm.exception()) {
  91. auto error = vm.exception()->value();
  92. vm.clear_exception();
  93. vm.stop_unwind();
  94. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: An exception was thrown, returning error {}", this, error);
  95. return error;
  96. }
  97. dbgln_if(PROMISE_DEBUG, "[PromiseResolveThenableJob @ {}]: Returning then call result {}", this, then_call_result);
  98. return then_call_result;
  99. }
  100. void PromiseResolveThenableJob::visit_edges(Visitor& visitor)
  101. {
  102. Base::visit_edges(visitor);
  103. visitor.visit(&m_promise_to_resolve);
  104. visitor.visit(m_thenable);
  105. visitor.visit(m_then.callback);
  106. }
  107. }