PromiseConstructor.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/AbstractOperations.h>
  8. #include <LibJS/Runtime/Error.h>
  9. #include <LibJS/Runtime/FunctionObject.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Promise.h>
  12. #include <LibJS/Runtime/PromiseConstructor.h>
  13. #include <LibJS/Runtime/PromiseReaction.h>
  14. namespace JS {
  15. PromiseConstructor::PromiseConstructor(GlobalObject& global_object)
  16. : NativeFunction(vm().names.Promise.as_string(), *global_object.function_prototype())
  17. {
  18. }
  19. void PromiseConstructor::initialize(GlobalObject& global_object)
  20. {
  21. auto& vm = this->vm();
  22. NativeFunction::initialize(global_object);
  23. // 27.2.4.4 Promise.prototype, https://tc39.es/ecma262/#sec-promise.prototype
  24. define_direct_property(vm.names.prototype, global_object.promise_prototype(), 0);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. // TODO: Implement these functions below and uncomment this.
  27. // define_native_function(vm.names.all, all, 1, attr);
  28. // define_native_function(vm.names.allSettled, all_settled, 1, attr);
  29. // define_native_function(vm.names.any, any, 1, attr);
  30. // define_native_function(vm.names.race, race, 1, attr);
  31. define_native_function(vm.names.reject, reject, 1, attr);
  32. define_native_function(vm.names.resolve, resolve, 1, attr);
  33. define_native_accessor(*vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  34. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  35. }
  36. // 27.2.3.1 Promise ( executor ), https://tc39.es/ecma262/#sec-promise-executor
  37. Value PromiseConstructor::call()
  38. {
  39. auto& vm = this->vm();
  40. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.Promise);
  41. return {};
  42. }
  43. // 27.2.3.1 Promise ( executor ), https://tc39.es/ecma262/#sec-promise-executor
  44. Value PromiseConstructor::construct(FunctionObject& new_target)
  45. {
  46. auto& vm = this->vm();
  47. auto& global_object = this->global_object();
  48. auto executor = vm.argument(0);
  49. if (!executor.is_function()) {
  50. vm.throw_exception<TypeError>(global_object, ErrorType::PromiseExecutorNotAFunction);
  51. return {};
  52. }
  53. auto* promise = ordinary_create_from_constructor<Promise>(global_object, new_target, &GlobalObject::promise_prototype);
  54. if (vm.exception())
  55. return {};
  56. auto [resolve_function, reject_function] = promise->create_resolving_functions();
  57. (void)vm.call(executor.as_function(), js_undefined(), &resolve_function, &reject_function);
  58. if (auto* exception = vm.exception()) {
  59. vm.clear_exception();
  60. vm.stop_unwind();
  61. (void)vm.call(reject_function, js_undefined(), exception->value());
  62. }
  63. return promise;
  64. }
  65. // 27.2.4.1 Promise.all ( iterable ), https://tc39.es/ecma262/#sec-promise.all
  66. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all)
  67. {
  68. TODO();
  69. }
  70. // 27.2.4.2 Promise.allSettled ( iterable ), https://tc39.es/ecma262/#sec-promise.allsettled
  71. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::all_settled)
  72. {
  73. TODO();
  74. }
  75. // 27.2.4.3 Promise.any ( iterable ), https://tc39.es/ecma262/#sec-promise.any
  76. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::any)
  77. {
  78. TODO();
  79. }
  80. // 27.2.4.5 Promise.race ( iterable ), https://tc39.es/ecma262/#sec-promise.race
  81. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::race)
  82. {
  83. TODO();
  84. }
  85. // 27.2.4.6 Promise.reject ( r ), https://tc39.es/ecma262/#sec-promise.reject
  86. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::reject)
  87. {
  88. auto* constructor = vm.this_value(global_object).to_object(global_object);
  89. if (!constructor)
  90. return {};
  91. auto promise_capability = new_promise_capability(global_object, constructor);
  92. if (vm.exception())
  93. return {};
  94. auto reason = vm.argument(0);
  95. [[maybe_unused]] auto result = vm.call(*promise_capability.reject, js_undefined(), reason);
  96. return promise_capability.promise;
  97. }
  98. // 27.2.4.7 Promise.resolve ( x ), https://tc39.es/ecma262/#sec-promise.resolve
  99. JS_DEFINE_NATIVE_FUNCTION(PromiseConstructor::resolve)
  100. {
  101. auto* constructor = vm.this_value(global_object).to_object(global_object);
  102. if (!constructor)
  103. return {};
  104. auto value = vm.argument(0);
  105. return promise_resolve(global_object, *constructor, value);
  106. }
  107. // 27.2.4.8 get Promise [ @@species ], https://tc39.es/ecma262/#sec-get-promise-@@species
  108. JS_DEFINE_NATIVE_GETTER(PromiseConstructor::symbol_species_getter)
  109. {
  110. return vm.this_value(global_object);
  111. }
  112. }