PromiseReaction.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. PromiseCapability new_promise_capability(GlobalObject& global_object, Value constructor)
  14. {
  15. auto& vm = global_object.vm();
  16. if (!constructor.is_constructor()) {
  17. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
  18. return {};
  19. }
  20. struct {
  21. Value resolve { js_undefined() };
  22. Value reject { js_undefined() };
  23. } promise_capability_functions;
  24. // 27.2.1.5.1 GetCapabilitiesExecutor Functions, https://tc39.es/ecma262/#sec-getcapabilitiesexecutor-functions
  25. auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> ThrowCompletionOr<Value> {
  26. auto resolve = vm.argument(0);
  27. auto reject = vm.argument(1);
  28. // No idea what other engines say here.
  29. if (!promise_capability_functions.resolve.is_undefined()) {
  30. return vm.template throw_completion<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  31. }
  32. if (!promise_capability_functions.reject.is_undefined()) {
  33. return vm.template throw_completion<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  34. }
  35. promise_capability_functions.resolve = resolve;
  36. promise_capability_functions.reject = reject;
  37. return js_undefined();
  38. });
  39. executor->define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  40. executor->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  41. MarkedValueList arguments(vm.heap());
  42. arguments.append(executor);
  43. auto* promise = TRY_OR_DISCARD(construct(global_object, constructor.as_function(), move(arguments)));
  44. if (!promise_capability_functions.resolve.is_function()) {
  45. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  46. return {};
  47. }
  48. if (!promise_capability_functions.reject.is_function()) {
  49. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  50. return {};
  51. }
  52. return {
  53. promise,
  54. &promise_capability_functions.resolve.as_function(),
  55. &promise_capability_functions.reject.as_function(),
  56. };
  57. }
  58. PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
  59. : m_type(type)
  60. , m_capability(move(capability))
  61. , m_handler(move(handler))
  62. {
  63. }
  64. void PromiseReaction::visit_edges(Cell::Visitor& visitor)
  65. {
  66. Cell::visit_edges(visitor);
  67. if (m_capability.has_value()) {
  68. auto& capability = m_capability.value();
  69. visitor.visit(capability.promise);
  70. visitor.visit(capability.resolve);
  71. visitor.visit(capability.reject);
  72. }
  73. if (m_handler.has_value()) {
  74. auto& handler = m_handler.value();
  75. visitor.visit(handler.callback);
  76. }
  77. }
  78. }