PromiseReaction.cpp 4.4 KB

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