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