PromiseReaction.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/MarkedValueList.h>
  9. #include <LibJS/Runtime/NativeFunction.h>
  10. #include <LibJS/Runtime/PromiseReaction.h>
  11. namespace JS {
  12. // 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
  13. ThrowCompletionOr<PromiseCapability> new_promise_capability(GlobalObject& global_object, Value constructor)
  14. {
  15. auto& vm = global_object.vm();
  16. // 1. If IsConstructor(C) is false, throw a TypeError exception.
  17. if (!constructor.is_constructor())
  18. return vm.throw_completion<TypeError>(global_object, 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. // 5. Let executor be ! CreateBuiltinFunction(executorClosure, 2, "", « »).
  29. auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
  30. auto resolve = vm.argument(0);
  31. auto reject = vm.argument(1);
  32. // No idea what other engines say here.
  33. // a. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception.
  34. if (!promise_capability_functions.resolve.is_undefined())
  35. return vm.template throw_completion<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  36. // b. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception.
  37. if (!promise_capability_functions.reject.is_undefined())
  38. return vm.template throw_completion<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  39. // c. Set promiseCapability.[[Resolve]] to resolve.
  40. promise_capability_functions.resolve = resolve;
  41. // d. Set promiseCapability.[[Reject]] to reject.
  42. promise_capability_functions.reject = reject;
  43. // e. Return undefined.
  44. return js_undefined();
  45. });
  46. executor->define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  47. executor->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  48. // 6. Let promise be ? Construct(C, « executor »).
  49. MarkedValueList arguments(vm.heap());
  50. arguments.append(executor);
  51. auto* promise = TRY(construct(global_object, constructor.as_function(), move(arguments)));
  52. // 7. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception.
  53. if (!promise_capability_functions.resolve.is_function())
  54. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  55. // 8. If IsCallable(promiseCapability.[[Reject]]) is false, throw a TypeError exception.
  56. if (!promise_capability_functions.reject.is_function())
  57. return vm.throw_completion<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  58. // 9. Set promiseCapability.[[Promise]] to promise.
  59. // 10. Return promiseCapability.
  60. return PromiseCapability {
  61. promise,
  62. &promise_capability_functions.resolve.as_function(),
  63. &promise_capability_functions.reject.as_function(),
  64. };
  65. }
  66. PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
  67. : m_type(type)
  68. , m_capability(move(capability))
  69. , m_handler(move(handler))
  70. {
  71. }
  72. void PromiseReaction::visit_edges(Cell::Visitor& visitor)
  73. {
  74. Cell::visit_edges(visitor);
  75. if (m_capability.has_value()) {
  76. auto& capability = m_capability.value();
  77. visitor.visit(capability.promise);
  78. visitor.visit(capability.resolve);
  79. visitor.visit(capability.reject);
  80. }
  81. if (m_handler.has_value()) {
  82. auto& handler = m_handler.value();
  83. visitor.visit(handler.callback);
  84. }
  85. }
  86. }