PromiseConstructor.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/Interpreter.h>
  27. #include <LibJS/Runtime/Error.h>
  28. #include <LibJS/Runtime/Function.h>
  29. #include <LibJS/Runtime/GlobalObject.h>
  30. #include <LibJS/Runtime/Promise.h>
  31. #include <LibJS/Runtime/PromiseConstructor.h>
  32. #include <LibJS/Runtime/PromiseReaction.h>
  33. namespace JS {
  34. PromiseConstructor::PromiseConstructor(GlobalObject& global_object)
  35. : NativeFunction(vm().names.Promise, *global_object.function_prototype())
  36. {
  37. }
  38. void PromiseConstructor::initialize(GlobalObject& global_object)
  39. {
  40. auto& vm = this->vm();
  41. NativeFunction::initialize(global_object);
  42. define_property(vm.names.prototype, global_object.promise_prototype());
  43. define_property(vm.names.length, Value(1));
  44. u8 attr = Attribute::Writable | Attribute::Configurable;
  45. define_native_function(vm.names.all, all, 1, attr);
  46. define_native_function(vm.names.allSettled, all_settled, 1, attr);
  47. define_native_function(vm.names.any, any, 1, attr);
  48. define_native_function(vm.names.race, race, 1, attr);
  49. define_native_function(vm.names.reject, reject, 1, attr);
  50. define_native_function(vm.names.resolve, resolve, 1, attr);
  51. }
  52. Value PromiseConstructor::call()
  53. {
  54. auto& vm = this->vm();
  55. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Promise);
  56. return {};
  57. }
  58. // 27.2.3.1 Promise, https://tc39.es/ecma262/#sec-promise-executor
  59. Value PromiseConstructor::construct(Function&)
  60. {
  61. auto& vm = this->vm();
  62. auto executor = vm.argument(0);
  63. if (!executor.is_function()) {
  64. vm.throw_exception<TypeError>(global_object(), ErrorType::PromiseExecutorNotAFunction);
  65. return {};
  66. }
  67. auto* promise = Promise::create(global_object());
  68. auto [resolve_function, reject_function] = promise->create_resolving_functions();
  69. auto completion_value = vm.call(executor.as_function(), js_undefined(), &resolve_function, &reject_function);
  70. if (vm.exception()) {
  71. vm.clear_exception();
  72. vm.stop_unwind();
  73. [[maybe_unused]] auto result = vm.call(reject_function, js_undefined(), completion_value);
  74. }
  75. return promise;
  76. }
  77. // 27.2.4.1 Promise.all, https://tc39.es/ecma262/#sec-promise.all
  78. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all)
  79. {
  80. TODO();
  81. }
  82. // 27.2.4.2 Promise.allSettled, https://tc39.es/ecma262/#sec-promise.allsettled
  83. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all_settled)
  84. {
  85. TODO();
  86. }
  87. // 27.2.4.3 Promise.any, https://tc39.es/ecma262/#sec-promise.any
  88. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::any)
  89. {
  90. TODO();
  91. }
  92. // 27.2.4.5 Promise.race, https://tc39.es/ecma262/#sec-promise.race
  93. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::race)
  94. {
  95. TODO();
  96. }
  97. // 27.2.4.6 Promise.reject, https://tc39.es/ecma262/#sec-promise.reject
  98. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::reject)
  99. {
  100. auto* constructor = vm.this_value(global_object).to_object(global_object);
  101. if (!constructor)
  102. return {};
  103. auto promise_capability = new_promise_capability(global_object, constructor);
  104. if (vm.exception())
  105. return {};
  106. auto reason = vm.argument(0);
  107. [[maybe_unused]] auto result = vm.call(*promise_capability.reject, js_undefined(), reason);
  108. return promise_capability.promise;
  109. }
  110. // 27.2.4.7 Promise.resolve, https://tc39.es/ecma262/#sec-promise.resolve
  111. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::resolve)
  112. {
  113. auto* constructor = vm.this_value(global_object).to_object(global_object);
  114. if (!constructor)
  115. return {};
  116. auto value = vm.argument(0);
  117. return promise_resolve(global_object, *constructor, value);
  118. }
  119. }