PromiseReaction.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/MarkedValueList.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. #include <LibJS/Runtime/PromiseReaction.h>
  10. namespace JS {
  11. // 27.2.1.5 NewPromiseCapability ( C ), https://tc39.es/ecma262/#sec-newpromisecapability
  12. PromiseCapability new_promise_capability(GlobalObject& global_object, Value constructor)
  13. {
  14. auto& vm = global_object.vm();
  15. if (!constructor.is_constructor()) {
  16. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
  17. return {};
  18. }
  19. struct {
  20. Value resolve { js_undefined() };
  21. Value reject { js_undefined() };
  22. } promise_capability_functions;
  23. // 27.2.1.5.1 GetCapabilitiesExecutor Functions, https://tc39.es/ecma262/#sec-getcapabilitiesexecutor-functions
  24. auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> Value {
  25. auto resolve = vm.argument(0);
  26. auto reject = vm.argument(1);
  27. // No idea what other engines say here.
  28. if (!promise_capability_functions.resolve.is_undefined()) {
  29. vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  30. return {};
  31. }
  32. if (!promise_capability_functions.reject.is_undefined()) {
  33. vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  34. return {};
  35. }
  36. promise_capability_functions.resolve = resolve;
  37. promise_capability_functions.reject = reject;
  38. return js_undefined();
  39. });
  40. executor->define_direct_property(vm.names.length, Value(2), Attribute::Configurable);
  41. executor->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
  42. MarkedValueList arguments(vm.heap());
  43. arguments.append(executor);
  44. auto promise = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
  45. if (vm.exception())
  46. return {};
  47. // I'm not sure if we could VERIFY(promise.is_object()) instead - the spec doesn't have this check...
  48. if (!promise.is_object()) {
  49. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, promise.to_string_without_side_effects());
  50. return {};
  51. }
  52. if (!promise_capability_functions.resolve.is_function()) {
  53. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  54. return {};
  55. }
  56. if (!promise_capability_functions.reject.is_function()) {
  57. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  58. return {};
  59. }
  60. return {
  61. &promise.as_object(),
  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. }