PromiseReaction.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> Value {
  24. auto resolve = vm.argument(0);
  25. auto reject = vm.argument(1);
  26. // No idea what other engines say here.
  27. if (!promise_capability_functions.resolve.is_undefined()) {
  28. vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  29. return {};
  30. }
  31. if (!promise_capability_functions.resolve.is_undefined()) {
  32. vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  33. return {};
  34. }
  35. promise_capability_functions.resolve = resolve;
  36. promise_capability_functions.reject = reject;
  37. return js_undefined();
  38. });
  39. executor->define_property(vm.names.length, Value(2));
  40. MarkedValueList arguments(vm.heap());
  41. arguments.append(executor);
  42. auto promise = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
  43. if (vm.exception())
  44. return {};
  45. // I'm not sure if we could VERIFY(promise.is_object()) instead - the spec doesn't have this check...
  46. if (!promise.is_object()) {
  47. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, promise.to_string_without_side_effects());
  48. return {};
  49. }
  50. if (!promise_capability_functions.resolve.is_function()) {
  51. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  52. return {};
  53. }
  54. if (!promise_capability_functions.reject.is_function()) {
  55. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  56. return {};
  57. }
  58. return {
  59. &promise.as_object(),
  60. &promise_capability_functions.resolve.as_function(),
  61. &promise_capability_functions.reject.as_function(),
  62. };
  63. }
  64. PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
  65. : m_type(type)
  66. , m_capability(move(capability))
  67. , m_handler(move(handler))
  68. {
  69. }
  70. void PromiseReaction::visit_edges(Cell::Visitor& visitor)
  71. {
  72. Cell::visit_edges(visitor);
  73. if (m_capability.has_value()) {
  74. auto& capability = m_capability.value();
  75. visitor.visit(capability.promise);
  76. visitor.visit(capability.resolve);
  77. visitor.visit(capability.reject);
  78. }
  79. if (m_handler.has_value()) {
  80. auto& handler = m_handler.value();
  81. visitor.visit(handler.callback);
  82. }
  83. }
  84. }