PromiseReaction.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. #include <LibJS/Runtime/PromiseReaction.h>
  10. namespace JS {
  11. // 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
  12. ThrowCompletionOr<PromiseCapability> new_promise_capability(VM& vm, Value constructor)
  13. {
  14. auto& realm = *vm.current_realm();
  15. auto& global_object = realm.global_object();
  16. // 1. If IsConstructor(C) is false, throw a TypeError exception.
  17. if (!constructor.is_constructor())
  18. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
  19. // 2. NOTE: C is assumed to be a constructor function that supports the parameter conventions of the Promise constructor (see 27.2.3.1).
  20. // 3. Let promiseCapability be the PromiseCapability Record { [[Promise]]: undefined, [[Resolve]]: undefined, [[Reject]]: undefined }.
  21. // FIXME: This should not be stack-allocated, the executor function below can be captured and outlive it!
  22. // See https://discord.com/channels/830522505605283862/886211697843531866/900081190621569154 for some discussion.
  23. struct {
  24. Value resolve { js_undefined() };
  25. Value reject { js_undefined() };
  26. } promise_capability_functions;
  27. // 4. Let executorClosure be a new Abstract Closure with parameters (resolve, reject) that captures promiseCapability and performs the following steps when called:
  28. auto executor_closure = [&promise_capability_functions](auto& vm, auto&) -> ThrowCompletionOr<Value> {
  29. auto resolve = vm.argument(0);
  30. auto reject = vm.argument(1);
  31. // No idea what other engines say here.
  32. // a. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception.
  33. if (!promise_capability_functions.resolve.is_undefined())
  34. return vm.template throw_completion<TypeError>(ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  35. // b. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception.
  36. if (!promise_capability_functions.reject.is_undefined())
  37. return vm.template throw_completion<TypeError>(ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  38. // c. Set promiseCapability.[[Resolve]] to resolve.
  39. promise_capability_functions.resolve = resolve;
  40. // d. Set promiseCapability.[[Reject]] to reject.
  41. promise_capability_functions.reject = reject;
  42. // e. Return undefined.
  43. return js_undefined();
  44. };
  45. // 5. Let executor be CreateBuiltinFunction(executorClosure, 2, "", « »).
  46. auto* executor = NativeFunction::create(realm, move(executor_closure), 2, "");
  47. // 6. Let promise be ? Construct(C, « executor »).
  48. auto* promise = TRY(construct(global_object, constructor.as_function(), executor));
  49. // 7. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception.
  50. if (!promise_capability_functions.resolve.is_function())
  51. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  52. // 8. If IsCallable(promiseCapability.[[Reject]]) is false, throw a TypeError exception.
  53. if (!promise_capability_functions.reject.is_function())
  54. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  55. // 9. Set promiseCapability.[[Promise]] to promise.
  56. // 10. Return promiseCapability.
  57. return PromiseCapability {
  58. promise,
  59. &promise_capability_functions.resolve.as_function(),
  60. &promise_capability_functions.reject.as_function(),
  61. };
  62. }
  63. PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
  64. : m_type(type)
  65. , m_capability(move(capability))
  66. , m_handler(move(handler))
  67. {
  68. }
  69. void PromiseReaction::visit_edges(Cell::Visitor& visitor)
  70. {
  71. Cell::visit_edges(visitor);
  72. if (m_capability.has_value()) {
  73. auto& capability = m_capability.value();
  74. visitor.visit(capability.promise);
  75. visitor.visit(capability.resolve);
  76. visitor.visit(capability.reject);
  77. }
  78. }
  79. }