PromiseConstructor.cpp 3.9 KB

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