PromiseReaction.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.resolve.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. MarkedValueList arguments(vm.heap());
  42. arguments.append(executor);
  43. auto promise = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments));
  44. if (vm.exception())
  45. return {};
  46. // I'm not sure if we could VERIFY(promise.is_object()) instead - the spec doesn't have this check...
  47. if (!promise.is_object()) {
  48. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, promise.to_string_without_side_effects());
  49. return {};
  50. }
  51. if (!promise_capability_functions.resolve.is_function()) {
  52. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  53. return {};
  54. }
  55. if (!promise_capability_functions.reject.is_function()) {
  56. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  57. return {};
  58. }
  59. return {
  60. &promise.as_object(),
  61. &promise_capability_functions.resolve.as_function(),
  62. &promise_capability_functions.reject.as_function(),
  63. };
  64. }
  65. PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
  66. : m_type(type)
  67. , m_capability(move(capability))
  68. , m_handler(move(handler))
  69. {
  70. }
  71. void PromiseReaction::visit_edges(Cell::Visitor& visitor)
  72. {
  73. Cell::visit_edges(visitor);
  74. if (m_capability.has_value()) {
  75. auto& capability = m_capability.value();
  76. visitor.visit(capability.promise);
  77. visitor.visit(capability.resolve);
  78. visitor.visit(capability.reject);
  79. }
  80. if (m_handler.has_value()) {
  81. auto& handler = m_handler.value();
  82. visitor.visit(handler.callback);
  83. }
  84. }
  85. }