PromiseReaction.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2021, 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. // 5. Let executor be ! CreateBuiltinFunction(executorClosure, 2, "", « »).
  28. auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> 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>(global_object, 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>(global_object, 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. executor->define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  46. executor->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  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>(global_object, 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>(global_object, 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. if (m_handler.has_value()) {
  79. auto& handler = m_handler.value();
  80. visitor.visit(handler.callback);
  81. }
  82. }
  83. }