PromiseConstructor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // TODO: Implement these functions below and uncomment this.
  26. // define_native_function(vm.names.all, all, 1, attr);
  27. // define_native_function(vm.names.allSettled, all_settled, 1, attr);
  28. // define_native_function(vm.names.any, any, 1, attr);
  29. // define_native_function(vm.names.race, race, 1, attr);
  30. define_native_function(vm.names.reject, reject, 1, attr);
  31. define_native_function(vm.names.resolve, resolve, 1, attr);
  32. }
  33. Value PromiseConstructor::call()
  34. {
  35. auto& vm = this->vm();
  36. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Promise);
  37. return {};
  38. }
  39. // 27.2.3.1 Promise, https://tc39.es/ecma262/#sec-promise-executor
  40. Value PromiseConstructor::construct(Function&)
  41. {
  42. auto& vm = this->vm();
  43. auto executor = vm.argument(0);
  44. if (!executor.is_function()) {
  45. vm.throw_exception<TypeError>(global_object(), ErrorType::PromiseExecutorNotAFunction);
  46. return {};
  47. }
  48. auto* promise = Promise::create(global_object());
  49. auto [resolve_function, reject_function] = promise->create_resolving_functions();
  50. auto completion_value = vm.call(executor.as_function(), js_undefined(), &resolve_function, &reject_function);
  51. if (vm.exception()) {
  52. vm.clear_exception();
  53. vm.stop_unwind();
  54. [[maybe_unused]] auto result = vm.call(reject_function, js_undefined(), completion_value);
  55. }
  56. return promise;
  57. }
  58. // 27.2.4.1 Promise.all, https://tc39.es/ecma262/#sec-promise.all
  59. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all)
  60. {
  61. TODO();
  62. }
  63. // 27.2.4.2 Promise.allSettled, https://tc39.es/ecma262/#sec-promise.allsettled
  64. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all_settled)
  65. {
  66. TODO();
  67. }
  68. // 27.2.4.3 Promise.any, https://tc39.es/ecma262/#sec-promise.any
  69. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::any)
  70. {
  71. TODO();
  72. }
  73. // 27.2.4.5 Promise.race, https://tc39.es/ecma262/#sec-promise.race
  74. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::race)
  75. {
  76. TODO();
  77. }
  78. // 27.2.4.6 Promise.reject, https://tc39.es/ecma262/#sec-promise.reject
  79. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::reject)
  80. {
  81. auto* constructor = vm.this_value(global_object).to_object(global_object);
  82. if (!constructor)
  83. return {};
  84. auto promise_capability = new_promise_capability(global_object, constructor);
  85. if (vm.exception())
  86. return {};
  87. auto reason = vm.argument(0);
  88. [[maybe_unused]] auto result = vm.call(*promise_capability.reject, js_undefined(), reason);
  89. return promise_capability.promise;
  90. }
  91. // 27.2.4.7 Promise.resolve, https://tc39.es/ecma262/#sec-promise.resolve
  92. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::resolve)
  93. {
  94. auto* constructor = vm.this_value(global_object).to_object(global_object);
  95. if (!constructor)
  96. return {};
  97. auto value = vm.argument(0);
  98. return promise_resolve(global_object, *constructor, value);
  99. }
  100. }