AsyncFunctionDriverWrapper.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AsyncFunctionDriverWrapper.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. #include <LibJS/Runtime/PromiseReaction.h>
  10. #include <LibJS/Runtime/VM.h>
  11. namespace JS {
  12. ThrowCompletionOr<Value> AsyncFunctionDriverWrapper::create(GlobalObject& global_object, GeneratorObject* generator_object)
  13. {
  14. auto wrapper = global_object.heap().allocate<AsyncFunctionDriverWrapper>(global_object, global_object, generator_object);
  15. return wrapper->react_to_async_task_completion(global_object.vm(), global_object, js_undefined(), true);
  16. }
  17. AsyncFunctionDriverWrapper::AsyncFunctionDriverWrapper(GlobalObject& global_object, GeneratorObject* generator_object)
  18. : Promise(global_object)
  19. , m_generator_object(generator_object)
  20. , m_on_fulfillment(NativeFunction::create(global_object, "async.on_fulfillment"sv, [this](VM& vm, GlobalObject& global_object) {
  21. return react_to_async_task_completion(vm, global_object, vm.argument(0), true);
  22. }))
  23. , m_on_rejection(NativeFunction::create(global_object, "async.on_rejection"sv, [this](VM& vm, GlobalObject& global_object) {
  24. return react_to_async_task_completion(vm, global_object, vm.argument(0), false);
  25. }))
  26. {
  27. }
  28. ThrowCompletionOr<Value> AsyncFunctionDriverWrapper::react_to_async_task_completion(VM& vm, GlobalObject& global_object, Value value, bool is_successful)
  29. {
  30. auto generator_result = is_successful
  31. ? m_generator_object->next_impl(vm, global_object, value, {})
  32. : m_generator_object->next_impl(vm, global_object, {}, value);
  33. if (generator_result.is_throw_completion()) {
  34. VERIFY(generator_result.throw_completion().type() == Completion::Type::Throw);
  35. auto promise = Promise::create(global_object);
  36. promise->reject(*generator_result.throw_completion().value());
  37. return promise;
  38. }
  39. auto result = generator_result.release_value();
  40. VERIFY(result.is_object());
  41. auto promise_value = TRY(result.get(global_object, vm.names.value));
  42. if (!promise_value.is_object() || !is<Promise>(promise_value.as_object())) {
  43. auto promise = Promise::create(global_object);
  44. promise->fulfill(promise_value);
  45. return promise;
  46. }
  47. auto* promise = static_cast<Promise*>(&promise_value.as_object());
  48. if (TRY(result.get(global_object, vm.names.done)).to_boolean())
  49. return promise;
  50. return promise->perform_then(m_on_fulfillment, m_on_rejection, PromiseCapability { promise, m_on_fulfillment, m_on_rejection });
  51. }
  52. void AsyncFunctionDriverWrapper::visit_edges(Cell::Visitor& visitor)
  53. {
  54. Base::visit_edges(visitor);
  55. visitor.visit(m_generator_object);
  56. visitor.visit(m_on_fulfillment);
  57. visitor.visit(m_on_rejection);
  58. }
  59. }