AsyncFunctionDriverWrapper.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/AsyncFunctionDriverWrapper.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/NativeFunction.h>
  10. #include <LibJS/Runtime/PromiseCapability.h>
  11. #include <LibJS/Runtime/VM.h>
  12. namespace JS {
  13. ThrowCompletionOr<Value> AsyncFunctionDriverWrapper::create(Realm& realm, GeneratorObject* generator_object)
  14. {
  15. auto top_level_promise = Promise::create(realm);
  16. // Note: This generates a handle to itself, which it clears upon completing its execution
  17. // The top_level_promise is also kept alive by this Wrapper
  18. auto wrapper = MUST_OR_THROW_OOM(realm.heap().allocate<AsyncFunctionDriverWrapper>(realm, realm, *generator_object, *top_level_promise));
  19. // Prime the generator:
  20. // This runs until the first `await value;`
  21. wrapper->continue_async_execution(realm.vm(), js_undefined(), true);
  22. return top_level_promise;
  23. }
  24. AsyncFunctionDriverWrapper::AsyncFunctionDriverWrapper(Realm& realm, NonnullGCPtr<GeneratorObject> generator_object, NonnullGCPtr<Promise> top_level_promise)
  25. : Promise(realm.intrinsics().promise_prototype())
  26. , m_generator_object(generator_object)
  27. , m_on_fulfillment(*NativeFunction::create(realm, "async.on_fulfillment"sv, [this](VM& vm) -> ThrowCompletionOr<Value> {
  28. auto arg = vm.argument(0);
  29. if (m_expect_promise) {
  30. continue_async_execution(vm, arg, true);
  31. m_expect_promise = false;
  32. return js_undefined();
  33. }
  34. return arg;
  35. }))
  36. , m_on_rejection(*NativeFunction::create(realm, "async.on_rejection"sv, [this](VM& vm) -> ThrowCompletionOr<Value> {
  37. auto arg = vm.argument(0);
  38. if (m_expect_promise) {
  39. continue_async_execution(vm, arg, false);
  40. m_expect_promise = false;
  41. return js_undefined();
  42. }
  43. return throw_completion(arg);
  44. }))
  45. , m_top_level_promise(top_level_promise)
  46. , m_self_handle(make_handle(*this))
  47. {
  48. }
  49. void AsyncFunctionDriverWrapper::continue_async_execution(VM& vm, Value value, bool is_successful)
  50. {
  51. auto generator_result = is_successful
  52. ? m_generator_object->resume(vm, value, {})
  53. : m_generator_object->resume_abrupt(vm, throw_completion(value), {});
  54. auto result = [&, this]() -> ThrowCompletionOr<void> {
  55. // This loop is for the trivial case of awaiting a non-Promise value,
  56. // and pseudo promises, that are actually resolved in a synchronous manner
  57. // It's either this, a goto, or a needles indirection
  58. while (true) {
  59. if (generator_result.is_throw_completion())
  60. return generator_result.throw_completion();
  61. auto result = generator_result.release_value();
  62. VERIFY(result.is_object());
  63. auto promise_value = TRY(result.get(vm, vm.names.value));
  64. if (TRY(result.get(vm, vm.names.done)).to_boolean()) {
  65. // We hit a `return value;`
  66. m_top_level_promise->fulfill(promise_value);
  67. // We should not execute anymore, so we are safe to allow our selfs to be GC'd
  68. m_self_handle = {};
  69. return {};
  70. }
  71. if (!promise_value.is_object() || !is<Promise>(promise_value.as_object())) {
  72. // We hit the trivial case of `await value`, where value is not a
  73. // Promise, so we can just continue the execution
  74. generator_result = m_generator_object->resume(vm, promise_value, {});
  75. continue;
  76. }
  77. // We hit `await Promise`
  78. m_current_promise = static_cast<Promise*>(&promise_value.as_object());
  79. // FIXME: We need to be a bit explicit here,
  80. // because for non async promises we arrive late to register us as handlers,
  81. // so we need to just pretend we are early and do the main logic ourselves,
  82. // Boon: This allows us to short-circuit to immediately continuing the execution
  83. // FIXME: This then causes a warning to be printed to the console, that we supposedly did not handle the promise
  84. if (m_current_promise->state() == Promise::State::Fulfilled) {
  85. generator_result = m_generator_object->resume(vm, m_current_promise->result(), {});
  86. continue;
  87. }
  88. if (m_current_promise->state() == Promise::State::Rejected) {
  89. generator_result = m_generator_object->resume_abrupt(vm, m_current_promise->result(), {});
  90. continue;
  91. }
  92. // Due to the nature of promise capabilities we might get called on either one path,
  93. // so we use a flag to make sure only accept one call
  94. // FIXME: There might be a cleaner way to accomplish this
  95. m_expect_promise = true;
  96. auto promise_capability = PromiseCapability::create(vm, *m_current_promise,
  97. m_on_fulfillment,
  98. m_on_rejection);
  99. m_current_promise->perform_then(
  100. m_on_fulfillment,
  101. m_on_rejection,
  102. promise_capability);
  103. return {};
  104. }
  105. }();
  106. if (result.is_throw_completion()) {
  107. m_top_level_promise->reject(result.throw_completion().value().value_or(js_undefined()));
  108. // We should not execute anymore, so we are safe to allow our selfs to be GC'd
  109. m_self_handle = {};
  110. }
  111. }
  112. void AsyncFunctionDriverWrapper::visit_edges(Cell::Visitor& visitor)
  113. {
  114. Base::visit_edges(visitor);
  115. visitor.visit(m_generator_object);
  116. visitor.visit(m_on_fulfillment);
  117. visitor.visit(m_on_rejection);
  118. visitor.visit(m_top_level_promise);
  119. if (m_current_promise)
  120. visitor.visit(m_current_promise);
  121. }
  122. }