PromiseReaction.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibJS/Runtime/Error.h>
  27. #include <LibJS/Runtime/MarkedValueList.h>
  28. #include <LibJS/Runtime/NativeFunction.h>
  29. #include <LibJS/Runtime/PromiseReaction.h>
  30. namespace JS {
  31. // 27.2.1.5 NewPromiseCapability, https://tc39.es/ecma262/#sec-newpromisecapability
  32. PromiseCapability new_promise_capability(GlobalObject& global_object, Value constructor)
  33. {
  34. auto& vm = global_object.vm();
  35. if (!constructor.is_constructor()) {
  36. vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
  37. return {};
  38. }
  39. struct {
  40. Value resolve { js_undefined() };
  41. Value reject { js_undefined() };
  42. } promise_capability_functions;
  43. auto* executor = NativeFunction::create(global_object, "", [&promise_capability_functions](auto& vm, auto& global_object) -> Value {
  44. auto resolve = vm.argument(0);
  45. auto reject = vm.argument(1);
  46. // No idea what other engines say here.
  47. if (!promise_capability_functions.resolve.is_undefined()) {
  48. vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  49. return {};
  50. }
  51. if (!promise_capability_functions.resolve.is_undefined()) {
  52. vm.template throw_exception<TypeError>(global_object, ErrorType::GetCapabilitiesExecutorCalledMultipleTimes);
  53. return {};
  54. }
  55. promise_capability_functions.resolve = resolve;
  56. promise_capability_functions.reject = reject;
  57. return js_undefined();
  58. });
  59. executor->define_property(vm.names.length, Value(2));
  60. MarkedValueList arguments(vm.heap());
  61. arguments.append(executor);
  62. auto promise = vm.construct(constructor.as_function(), constructor.as_function(), move(arguments), global_object);
  63. if (vm.exception())
  64. return {};
  65. // I'm not sure if we could VERIFY(promise.is_object()) instead - the spec doesn't have this check...
  66. if (!promise.is_object()) {
  67. vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, promise.to_string_without_side_effects());
  68. return {};
  69. }
  70. if (!promise_capability_functions.resolve.is_function()) {
  71. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.resolve.to_string_without_side_effects());
  72. return {};
  73. }
  74. if (!promise_capability_functions.reject.is_function()) {
  75. vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, promise_capability_functions.reject.to_string_without_side_effects());
  76. return {};
  77. }
  78. return {
  79. &promise.as_object(),
  80. &promise_capability_functions.resolve.as_function(),
  81. &promise_capability_functions.reject.as_function(),
  82. };
  83. }
  84. PromiseReaction::PromiseReaction(Type type, Optional<PromiseCapability> capability, Optional<JobCallback> handler)
  85. : m_type(type)
  86. , m_capability(move(capability))
  87. , m_handler(move(handler))
  88. {
  89. }
  90. void PromiseReaction::visit_edges(Cell::Visitor& visitor)
  91. {
  92. Cell::visit_edges(visitor);
  93. if (m_capability.has_value()) {
  94. auto& capability = m_capability.value();
  95. visitor.visit(capability.promise);
  96. visitor.visit(capability.resolve);
  97. visitor.visit(capability.reject);
  98. }
  99. if (m_handler.has_value()) {
  100. auto& handler = m_handler.value();
  101. visitor.visit(handler.callback);
  102. }
  103. }
  104. }