Completion.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibCore/EventLoop.h>
  9. #include <LibJS/Runtime/Completion.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/NativeFunction.h>
  12. #include <LibJS/Runtime/PromiseConstructor.h>
  13. #include <LibJS/Runtime/PromiseReaction.h>
  14. #include <LibJS/Runtime/VM.h>
  15. #include <LibJS/Runtime/Value.h>
  16. namespace JS {
  17. Completion::Completion(ThrowCompletionOr<Value> const& throw_completion_or_value)
  18. {
  19. if (throw_completion_or_value.is_throw_completion()) {
  20. m_type = Type::Throw;
  21. m_value = throw_completion_or_value.throw_completion().value();
  22. } else {
  23. m_type = Type::Normal;
  24. m_value = throw_completion_or_value.value();
  25. }
  26. }
  27. // 6.2.3.1 Await, https://tc39.es/ecma262/#await
  28. ThrowCompletionOr<Value> await(GlobalObject& global_object, Value value)
  29. {
  30. auto& vm = global_object.vm();
  31. // 1. Let asyncContext be the running execution context.
  32. // NOTE: This is not needed, as we don't suspend anything.
  33. // 2. Let promise be ? PromiseResolve(%Promise%, value).
  34. auto* promise_object = TRY(promise_resolve(global_object, *global_object.promise_constructor(), value));
  35. Optional<bool> success;
  36. Value result;
  37. // 3. Let fulfilledClosure be a new Abstract Closure with parameters (value) that captures asyncContext and performs the following steps when called:
  38. auto fulfilled_closure = [&success, &result](VM& vm, GlobalObject&) -> ThrowCompletionOr<Value> {
  39. // a. Let prevContext be the running execution context.
  40. // b. Suspend prevContext.
  41. // FIXME: We don't have this concept yet.
  42. // NOTE: Since we don't support context suspension, we exfiltrate the result to await()'s scope instead
  43. success = true;
  44. result = vm.argument(0);
  45. // c. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  46. // NOTE: This is not done, because we're not suspending anything (see above).
  47. // d. Resume the suspended evaluation of asyncContext using NormalCompletion(value) as the result of the operation that suspended it.
  48. // e. Assert: When we reach this step, asyncContext has already been removed from the execution context stack and prevContext is the currently running execution context.
  49. // FIXME: We don't have this concept yet.
  50. // f. Return undefined.
  51. return js_undefined();
  52. };
  53. // 4. Let onFulfilled be ! CreateBuiltinFunction(fulfilledClosure, 1, "", « »).
  54. auto on_fulfilled = NativeFunction::create(global_object, "", move(fulfilled_closure));
  55. // 5. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures asyncContext and performs the following steps when called:
  56. auto rejected_closure = [&success, &result](VM& vm, GlobalObject&) -> ThrowCompletionOr<Value> {
  57. // a. Let prevContext be the running execution context.
  58. // b. Suspend prevContext.
  59. // FIXME: We don't have this concept yet.
  60. // NOTE: Since we don't support context suspension, we exfiltrate the result to await()'s scope instead
  61. success = false;
  62. result = vm.argument(0);
  63. // c. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  64. // NOTE: This is not done, because we're not suspending anything (see above).
  65. // d. Resume the suspended evaluation of asyncContext using ThrowCompletion(reason) as the result of the operation that suspended it.
  66. // e. Assert: When we reach this step, asyncContext has already been removed from the execution context stack and prevContext is the currently running execution context.
  67. // FIXME: We don't have this concept yet.
  68. // f. Return undefined.
  69. return js_undefined();
  70. };
  71. // 6. Let onRejected be ! CreateBuiltinFunction(rejectedClosure, 1, "", « »).
  72. auto on_rejected = NativeFunction::create(global_object, "", move(rejected_closure));
  73. // 7. Perform ! PerformPromiseThen(promise, onFulfilled, onRejected).
  74. auto* promise = verify_cast<Promise>(promise_object);
  75. promise->perform_then(on_fulfilled, on_rejected, {});
  76. // FIXME: Since we don't support context suspension, we attempt to "wait" for the promise to resolve
  77. // by letting the event loop spin until our promise is no longer pending, and then synchronously
  78. // running all queued promise jobs.
  79. // Note: This is not used by LibJS itself, and is performed for the embedder (i.e. LibWeb).
  80. if (Core::EventLoop::has_been_instantiated())
  81. Core::EventLoop::current().spin_until([&] { return promise->state() != Promise::State::Pending; });
  82. // 8. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
  83. // NOTE: Since we don't push any EC, this step is not performed.
  84. // 9. Set the code evaluation state of asyncContext such that when evaluation is resumed with a Completion completion, the following steps of the algorithm that invoked Await will be performed, with completion available.
  85. // 10. Return.
  86. // 11. NOTE: This returns to the evaluation of the operation that had most previously resumed evaluation of asyncContext.
  87. vm.run_queued_promise_jobs();
  88. // Make sure that the promise _actually_ resolved.
  89. // Note that this is checked down the chain (result.is_empty()) anyway, but let's make the source of the issue more clear.
  90. VERIFY(success.has_value());
  91. if (success.value())
  92. return result;
  93. // NOTE: This is temporary until we remove VM::exception(). It's required as callers of
  94. // AwaitExpression still need to check for an exception rather than a completion
  95. // type as long as ASTNode::execute() returns a plain Value.
  96. vm.throw_exception(global_object, result);
  97. return throw_completion(result);
  98. }
  99. }