PromiseCapability.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. auto promise_capability = PromiseCapability::create(vm, nullptr, nullptr, nullptr);
  37. // 4. Let executorClosure be a new Abstract Closure with parameters (resolve, reject) that captures promiseCapability and performs the following steps when called:
  38. // NOTE: Additionally to capturing the GC-allocated promise capability, we also create handles for
  39. // the resolve and reject values to keep them alive within the closure, as it may outlive the former.
  40. auto executor_closure = [&promise_capability, resolve_handle = make_handle({}), reject_handle = make_handle({})](auto& vm) mutable -> ThrowCompletionOr<Value> {
  41. auto resolve = vm.argument(0);
  42. auto reject = vm.argument(1);
  43. // No idea what other engines say here.
  44. // a. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception.
  45. if (!resolve_handle.is_null())
  46. return vm.template throw_completion<TypeError>(ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  47. // b. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception.
  48. if (!reject_handle.is_null())
  49. return vm.template throw_completion<TypeError>(ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  50. // c. Set promiseCapability.[[Resolve]] to resolve.
  51. resolve_handle = make_handle(resolve);
  52. if (resolve.is_function())
  53. promise_capability->set_resolve(resolve.as_function());
  54. // d. Set promiseCapability.[[Reject]] to reject.
  55. reject_handle = make_handle(reject);
  56. if (reject.is_function())
  57. promise_capability->set_reject(reject.as_function());
  58. // e. Return undefined.
  59. return js_undefined();
  60. };
  61. // 5. Let executor be CreateBuiltinFunction(executorClosure, 2, "", « »).
  62. auto executor = NativeFunction::create(realm, move(executor_closure), 2, "");
  63. // 6. Let promise be ? Construct(C, « executor »).
  64. auto promise = TRY(construct(vm, constructor.as_function(), executor));
  65. // 7. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception.
  66. // NOTE: We only assign a value in the executor closure if it is a function.
  67. if (promise_capability->resolve() == nullptr)
  68. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "Promise capability resolve value");
  69. // 8. If IsCallable(promiseCapability.[[Reject]]) is false, throw a TypeError exception.
  70. // NOTE: We only assign a value in the executor closure if it is a function.
  71. if (promise_capability->reject() == nullptr)
  72. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "Promise capability reject value");
  73. // 9. Set promiseCapability.[[Promise]] to promise.
  74. promise_capability->set_promise(*promise);
  75. // 10. Return promiseCapability.
  76. return promise_capability;
  77. }
  78. }