PromiseCapability.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 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. Base::visit_edges(visitor);
  24. visitor.visit(m_promise);
  25. visitor.visit(m_resolve);
  26. visitor.visit(m_reject);
  27. }
  28. // 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
  29. ThrowCompletionOr<NonnullGCPtr<PromiseCapability>> new_promise_capability(VM& vm, Value constructor)
  30. {
  31. auto& realm = *vm.current_realm();
  32. // 1. If IsConstructor(C) is false, throw a TypeError exception.
  33. if (!constructor.is_constructor())
  34. return vm.throw_completion<TypeError>(ErrorType::NotAConstructor, TRY_OR_THROW_OOM(vm, constructor.to_string_without_side_effects()));
  35. // 2. NOTE: C is assumed to be a constructor function that supports the parameter conventions of the Promise constructor (see 27.2.3.1).
  36. // 3. Let promiseCapability be the PromiseCapability Record { [[Promise]]: undefined, [[Resolve]]: undefined, [[Reject]]: undefined }.
  37. auto promise_capability = PromiseCapability::create(vm, nullptr, nullptr, nullptr);
  38. // 4. Let executorClosure be a new Abstract Closure with parameters (resolve, reject) that captures promiseCapability and performs the following steps when called:
  39. // NOTE: Additionally to capturing the GC-allocated promise capability, we also create handles for
  40. // the resolve and reject values to keep them alive within the closure, as it may outlive the former.
  41. auto executor_closure = [&promise_capability, resolve_handle = make_handle({}), reject_handle = make_handle({})](auto& vm) mutable -> ThrowCompletionOr<Value> {
  42. auto resolve = vm.argument(0);
  43. auto reject = vm.argument(1);
  44. // No idea what other engines say here.
  45. // a. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception.
  46. if (!resolve_handle.is_null())
  47. return vm.template throw_completion<TypeError>(ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  48. // b. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception.
  49. if (!reject_handle.is_null())
  50. return vm.template throw_completion<TypeError>(ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  51. // c. Set promiseCapability.[[Resolve]] to resolve.
  52. resolve_handle = make_handle(resolve);
  53. if (resolve.is_function())
  54. promise_capability->set_resolve(resolve.as_function());
  55. // d. Set promiseCapability.[[Reject]] to reject.
  56. reject_handle = make_handle(reject);
  57. if (reject.is_function())
  58. promise_capability->set_reject(reject.as_function());
  59. // e. Return undefined.
  60. return js_undefined();
  61. };
  62. // 5. Let executor be CreateBuiltinFunction(executorClosure, 2, "", « »).
  63. auto executor = NativeFunction::create(realm, move(executor_closure), 2, "");
  64. // 6. Let promise be ? Construct(C, « executor »).
  65. auto promise = TRY(construct(vm, constructor.as_function(), executor));
  66. // 7. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception.
  67. // NOTE: We only assign a value in the executor closure if it is a function.
  68. if (promise_capability->resolve() == nullptr)
  69. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "Promise capability resolve value");
  70. // 8. If IsCallable(promiseCapability.[[Reject]]) is false, throw a TypeError exception.
  71. // NOTE: We only assign a value in the executor closure if it is a function.
  72. if (promise_capability->reject() == nullptr)
  73. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "Promise capability reject value");
  74. // 9. Set promiseCapability.[[Promise]] to promise.
  75. promise_capability->set_promise(*promise);
  76. // 10. Return promiseCapability.
  77. return promise_capability;
  78. }
  79. }