Completion.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/NativeFunction.h>
  11. #include <LibJS/Runtime/PromiseConstructor.h>
  12. #include <LibJS/Runtime/PromiseReaction.h>
  13. #include <LibJS/Runtime/VM.h>
  14. #include <LibJS/Runtime/Value.h>
  15. namespace JS {
  16. Completion::Completion(ThrowCompletionOr<Value> const& throw_completion_or_value)
  17. {
  18. if (throw_completion_or_value.is_throw_completion()) {
  19. m_type = Type::Throw;
  20. m_value = throw_completion_or_value.throw_completion().value();
  21. } else {
  22. m_type = Type::Normal;
  23. m_value = throw_completion_or_value.value();
  24. }
  25. }
  26. // 6.2.3.1 Await, https://tc39.es/ecma262/#await
  27. ThrowCompletionOr<Value> await(GlobalObject& global_object, Value value)
  28. {
  29. auto& vm = global_object.vm();
  30. // 1. Let asyncContext be the running execution context.
  31. auto& async_context = vm.running_execution_context();
  32. // 2. Let promise be ? PromiseResolve(%Promise%, value).
  33. auto* promise = TRY(promise_resolve(global_object, *global_object.promise_constructor(), value));
  34. bool success = false;
  35. Value result;
  36. // 3. Let fulfilledClosure be a new Abstract Closure with parameters (value) that captures asyncContext and performs the following steps when called:
  37. auto fulfilled_closure = [&async_context, &success, &result](VM& vm, GlobalObject& global_object) -> ThrowCompletionOr<Value> {
  38. // a. Let prevContext be the running execution context.
  39. // b. Suspend prevContext.
  40. // FIXME: We don't have this concept yet.
  41. // NOTE: Since we don't support context suspension, we exfiltrate the result to await()'s scope instead
  42. success = true;
  43. result = vm.argument(0);
  44. // c. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  45. vm.push_execution_context(async_context, global_object);
  46. // d. Resume the suspended evaluation of asyncContext using NormalCompletion(value) as the result of the operation that suspended it.
  47. // 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.
  48. // FIXME: We don't have this concept yet.
  49. // f. Return undefined.
  50. return js_undefined();
  51. };
  52. // 4. Let onFulfilled be ! CreateBuiltinFunction(fulfilledClosure, 1, "", « »).
  53. auto on_fulfilled = NativeFunction::create(global_object, "", move(fulfilled_closure));
  54. // 5. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures asyncContext and performs the following steps when called:
  55. auto rejected_closure = [&async_context, &success, &result](VM& vm, GlobalObject& global_object) -> ThrowCompletionOr<Value> {
  56. // a. Let prevContext be the running execution context.
  57. // b. Suspend prevContext.
  58. // FIXME: We don't have this concept yet.
  59. // NOTE: Since we don't support context suspension, we exfiltrate the result to await()'s scope instead
  60. success = false;
  61. result = vm.argument(0);
  62. // c. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  63. vm.push_execution_context(async_context, global_object);
  64. // d. Resume the suspended evaluation of asyncContext using ThrowCompletion(reason) as the result of the operation that suspended it.
  65. // 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.
  66. // FIXME: We don't have this concept yet.
  67. // f. Return undefined.
  68. return js_undefined();
  69. };
  70. // 6. Let onRejected be ! CreateBuiltinFunction(rejectedClosure, 1, "", « »).
  71. auto on_rejected = NativeFunction::create(global_object, "", move(rejected_closure));
  72. // 7. Perform ! PerformPromiseThen(promise, onFulfilled, onRejected).
  73. verify_cast<Promise>(promise)->perform_then(on_fulfilled, on_rejected, {});
  74. // 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.
  75. vm.pop_execution_context();
  76. // 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.
  77. // 10. Return.
  78. // 11. NOTE: This returns to the evaluation of the operation that had most previously resumed evaluation of asyncContext.
  79. // FIXME: Since we don't support context suspension, we synchronously execute the promise
  80. vm.run_queued_promise_jobs();
  81. if (success)
  82. return result;
  83. else
  84. return throw_completion(result);
  85. }
  86. }