PromiseConstructor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Interpreter.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/Function.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Promise.h>
  11. #include <LibJS/Runtime/PromiseConstructor.h>
  12. #include <LibJS/Runtime/PromiseReaction.h>
  13. namespace JS {
  14. PromiseConstructor::PromiseConstructor(GlobalObject& global_object)
  15. : NativeFunction(vm().names.Promise, *global_object.function_prototype())
  16. {
  17. }
  18. void PromiseConstructor::initialize(GlobalObject& global_object)
  19. {
  20. auto& vm = this->vm();
  21. NativeFunction::initialize(global_object);
  22. define_property(vm.names.prototype, global_object.promise_prototype());
  23. define_property(vm.names.length, Value(1));
  24. u8 attr = Attribute::Writable | Attribute::Configurable;
  25. define_native_function(vm.names.all, all, 1, attr);
  26. define_native_function(vm.names.allSettled, all_settled, 1, attr);
  27. define_native_function(vm.names.any, any, 1, attr);
  28. define_native_function(vm.names.race, race, 1, attr);
  29. define_native_function(vm.names.reject, reject, 1, attr);
  30. define_native_function(vm.names.resolve, resolve, 1, attr);
  31. }
  32. Value PromiseConstructor::call()
  33. {
  34. auto& vm = this->vm();
  35. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Promise);
  36. return {};
  37. }
  38. // 27.2.3.1 Promise, https://tc39.es/ecma262/#sec-promise-executor
  39. Value PromiseConstructor::construct(Function&)
  40. {
  41. auto& vm = this->vm();
  42. auto executor = vm.argument(0);
  43. if (!executor.is_function()) {
  44. vm.throw_exception<TypeError>(global_object(), ErrorType::PromiseExecutorNotAFunction);
  45. return {};
  46. }
  47. auto* promise = Promise::create(global_object());
  48. auto [resolve_function, reject_function] = promise->create_resolving_functions();
  49. auto completion_value = vm.call(executor.as_function(), js_undefined(), &resolve_function, &reject_function);
  50. if (vm.exception()) {
  51. vm.clear_exception();
  52. vm.stop_unwind();
  53. [[maybe_unused]] auto result = vm.call(reject_function, js_undefined(), completion_value);
  54. }
  55. return promise;
  56. }
  57. // 27.2.4.1 Promise.all, https://tc39.es/ecma262/#sec-promise.all
  58. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all)
  59. {
  60. TODO();
  61. }
  62. // 27.2.4.2 Promise.allSettled, https://tc39.es/ecma262/#sec-promise.allsettled
  63. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all_settled)
  64. {
  65. TODO();
  66. }
  67. // 27.2.4.3 Promise.any, https://tc39.es/ecma262/#sec-promise.any
  68. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::any)
  69. {
  70. TODO();
  71. }
  72. // 27.2.4.5 Promise.race, https://tc39.es/ecma262/#sec-promise.race
  73. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::race)
  74. {
  75. TODO();
  76. }
  77. // 27.2.4.6 Promise.reject, https://tc39.es/ecma262/#sec-promise.reject
  78. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::reject)
  79. {
  80. auto* constructor = vm.this_value(global_object).to_object(global_object);
  81. if (!constructor)
  82. return {};
  83. auto promise_capability = new_promise_capability(global_object, constructor);
  84. if (vm.exception())
  85. return {};
  86. auto reason = vm.argument(0);
  87. [[maybe_unused]] auto result = vm.call(*promise_capability.reject, js_undefined(), reason);
  88. return promise_capability.promise;
  89. }
  90. // 27.2.4.7 Promise.resolve, https://tc39.es/ecma262/#sec-promise.resolve
  91. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::resolve)
  92. {
  93. auto* constructor = vm.this_value(global_object).to_object(global_object);
  94. if (!constructor)
  95. return {};
  96. auto value = vm.argument(0);
  97. return promise_resolve(global_object, *constructor, value);
  98. }
  99. }