| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef builtin_Promise_h |
| 8 |
#define builtin_Promise_h |
| 9 |
|
| 10 |
#include "jstypes.h" // JS_PUBLIC_API |
| 11 |
|
| 12 |
#include "js/RootingAPI.h" // JS::{,Mutable}Handle |
| 13 |
#include "js/TypeDecls.h" // JS::HandleObjectVector |
| 14 |
|
| 15 |
struct JS_PUBLIC_API JSContext; |
| 16 |
class JS_PUBLIC_API JSObject; |
| 17 |
|
| 18 |
namespace JS { |
| 19 |
class CallArgs; |
| 20 |
class Value; |
| 21 |
} // namespace JS |
| 22 |
|
| 23 |
namespace js { |
| 24 |
|
| 25 |
class AsyncFunctionGeneratorObject; |
| 26 |
class AsyncGeneratorObject; |
| 27 |
class PromiseObject; |
| 28 |
class SavedFrame; |
| 29 |
|
| 30 |
enum class CompletionKind : uint8_t; |
| 31 |
|
| 32 |
enum class PromiseHandler : uint32_t { |
| 33 |
Identity = 0, |
| 34 |
Thrower, |
| 35 |
|
| 36 |
// ES2022 draft rev d03c1ec6e235a5180fa772b6178727c17974cb14 |
| 37 |
// |
| 38 |
// Await in async function |
| 39 |
// https://tc39.es/ecma262/#await |
| 40 |
// |
| 41 |
// Step 3. fulfilledClosure Abstract Closure. |
| 42 |
// Step 5. rejectedClosure Abstract Closure. |
| 43 |
AsyncFunctionAwaitedFulfilled, |
| 44 |
AsyncFunctionAwaitedRejected, |
| 45 |
|
| 46 |
// Await in async generator |
| 47 |
// https://tc39.es/ecma262/#await |
| 48 |
// |
| 49 |
// Step 3. fulfilledClosure Abstract Closure. |
| 50 |
// Step 5. rejectedClosure Abstract Closure. |
| 51 |
AsyncGeneratorAwaitedFulfilled, |
| 52 |
AsyncGeneratorAwaitedRejected, |
| 53 |
|
| 54 |
// AsyncGeneratorAwaitReturn ( generator ) |
| 55 |
// https://tc39.es/ecma262/#sec-asyncgeneratorawaitreturn |
| 56 |
// |
| 57 |
// Step 7. fulfilledClosure Abstract Closure. |
| 58 |
// Step 9. rejectedClosure Abstract Closure. |
| 59 |
AsyncGeneratorAwaitReturnFulfilled, |
| 60 |
AsyncGeneratorAwaitReturnRejected, |
| 61 |
|
| 62 |
// AsyncGeneratorUnwrapYieldResumption |
| 63 |
// https://tc39.es/ecma262/#sec-asyncgeneratorunwrapyieldresumption |
| 64 |
// |
| 65 |
// Steps 3-5 for awaited.[[Type]] handling. |
| 66 |
AsyncGeneratorYieldReturnAwaitedFulfilled, |
| 67 |
AsyncGeneratorYieldReturnAwaitedRejected, |
| 68 |
|
| 69 |
// AsyncFromSyncIteratorContinuation ( result, promiseCapability ) |
| 70 |
// https://tc39.es/ecma262/#sec-asyncfromsynciteratorcontinuation |
| 71 |
// |
| 72 |
// Steps 7. unwrap Abstract Closure. |
| 73 |
AsyncFromSyncIteratorValueUnwrapDone, |
| 74 |
AsyncFromSyncIteratorValueUnwrapNotDone, |
| 75 |
|
| 76 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 77 |
// Explicit Resource Management Proposal |
| 78 |
// 27.1.3.1 %AsyncIteratorPrototype% [ @@asyncDispose ] ( ) |
| 79 |
// https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-%25asynciteratorprototype%25-%40%40asyncdispose |
| 80 |
// |
| 81 |
// Step 6.e. unwrap Abstract Closure |
| 82 |
AsyncIteratorDisposeAwaitFulfilled, |
| 83 |
#endif |
| 84 |
|
| 85 |
// One past the maximum allowed PromiseHandler value. |
| 86 |
Limit |
| 87 |
}; |
| 88 |
|
| 89 |
// Promise.prototype.then. |
| 90 |
extern bool Promise_then(JSContext* cx, unsigned argc, JS::Value* vp); |
| 91 |
|
| 92 |
// Promise[Symbol.species]. |
| 93 |
extern bool Promise_static_species(JSContext* cx, unsigned argc, JS::Value* vp); |
| 94 |
|
| 95 |
// Promise.resolve. |
| 96 |
extern bool Promise_static_resolve(JSContext* cx, unsigned argc, JS::Value* vp); |
| 97 |
|
| 98 |
/** |
| 99 |
* Unforgeable version of the JS builtin Promise.all. |
| 100 |
* |
| 101 |
* Takes a HandleValueVector of Promise objects and returns a promise that's |
| 102 |
* resolved with an array of resolution values when all those promises have |
| 103 |
* been resolved, or rejected with the rejection value of the first rejected |
| 104 |
* promise. |
| 105 |
* |
| 106 |
* Asserts that all objects in the `promises` vector are, maybe wrapped, |
| 107 |
* instances of `Promise` or a subclass of `Promise`. |
| 108 |
*/ |
| 109 |
[[nodiscard]] JSObject* GetWaitForAllPromise(JSContext* cx, |
| 110 |
JS::HandleObjectVector promises); |
| 111 |
|
| 112 |
/** |
| 113 |
* Enqueues resolve/reject reactions in the given Promise's reactions lists |
| 114 |
* as though by calling the original value of Promise.prototype.then, and |
| 115 |
* without regard to any Promise subclassing used in `promiseObj` itself. |
| 116 |
*/ |
| 117 |
[[nodiscard]] PromiseObject* OriginalPromiseThen( |
| 118 |
JSContext* cx, JS::Handle<JSObject*> promiseObj, |
| 119 |
JS::Handle<JSObject*> onFulfilled, JS::Handle<JSObject*> onRejected); |
| 120 |
|
| 121 |
enum class UnhandledRejectionBehavior { Ignore, Report }; |
| 122 |
|
| 123 |
/** |
| 124 |
* React to[0] `unwrappedPromise` (which may not be from the current realm) as |
| 125 |
* if by using a fresh promise created for the provided nullable fulfill/reject |
| 126 |
* IsCallable objects. |
| 127 |
* |
| 128 |
* However, no dependent Promise will be created, and mucking with `Promise`, |
| 129 |
* `Promise.prototype.then`, and `Promise[Symbol.species]` will not alter this |
| 130 |
* function's behavior. |
| 131 |
* |
| 132 |
* If `unwrappedPromise` rejects and `onRejected_` is null, handling is |
| 133 |
* determined by `behavior`. If `behavior == Report`, a fresh Promise will be |
| 134 |
* constructed and rejected on the fly (and thus will be reported as unhandled). |
| 135 |
* But if `behavior == Ignore`, the rejection is ignored and is not reported as |
| 136 |
* unhandled. |
| 137 |
* |
| 138 |
* Note: Reactions pushed using this function contain a null `promise` field. |
| 139 |
* That field is only ever used by devtools, which have to treat these reactions |
| 140 |
* specially. |
| 141 |
* |
| 142 |
* 0. The sense of "react" here is the sense of the term as defined by Web IDL: |
| 143 |
* https://heycam.github.io/webidl/#dfn-perform-steps-once-promise-is-settled |
| 144 |
*/ |
| 145 |
[[nodiscard]] extern bool ReactToUnwrappedPromise( |
| 146 |
JSContext* cx, JS::Handle<PromiseObject*> unwrappedPromise, |
| 147 |
JS::Handle<JSObject*> onFulfilled_, JS::Handle<JSObject*> onRejected_, |
| 148 |
UnhandledRejectionBehavior behavior); |
| 149 |
|
| 150 |
/** |
| 151 |
* PromiseResolve ( C, x ) |
| 152 |
* |
| 153 |
* The abstract operation PromiseResolve, given a constructor and a value, |
| 154 |
* returns a new promise resolved with that value. |
| 155 |
*/ |
| 156 |
[[nodiscard]] JSObject* PromiseResolve(JSContext* cx, |
| 157 |
JS::Handle<JSObject*> constructor, |
| 158 |
JS::Handle<JS::Value> value); |
| 159 |
|
| 160 |
/** |
| 161 |
* Reject |promise| with the value of the current pending exception. |
| 162 |
* |
| 163 |
* |promise| must be from the current realm. Callers must enter the realm of |
| 164 |
* |promise| if they are not already in it. |
| 165 |
*/ |
| 166 |
[[nodiscard]] bool RejectPromiseWithPendingError( |
| 167 |
JSContext* cx, JS::Handle<PromiseObject*> promise); |
| 168 |
|
| 169 |
/** |
| 170 |
* Create the promise object which will be used as the return value of an async |
| 171 |
* function. |
| 172 |
*/ |
| 173 |
[[nodiscard]] PromiseObject* CreatePromiseObjectForAsync(JSContext* cx); |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns true if the given object is a promise created by |
| 177 |
* either CreatePromiseObjectForAsync function or async generator's method. |
| 178 |
*/ |
| 179 |
[[nodiscard]] bool IsPromiseForAsyncFunctionOrGenerator(JSObject* promise); |
| 180 |
|
| 181 |
[[nodiscard]] bool AsyncFunctionReturned( |
| 182 |
JSContext* cx, JS::Handle<PromiseObject*> resultPromise, |
| 183 |
JS::Handle<JS::Value> value); |
| 184 |
|
| 185 |
[[nodiscard]] bool AsyncFunctionThrown( |
| 186 |
JSContext* cx, JS::Handle<PromiseObject*> resultPromise, |
| 187 |
JS::Handle<JS::Value> reason, |
| 188 |
JS::Handle<SavedFrame*> unwrappedRejectionStack = nullptr); |
| 189 |
|
| 190 |
// Start awaiting `value` in an async function (, but doesn't suspend the |
| 191 |
// async function's execution!). Returns the async function's result promise. |
| 192 |
[[nodiscard]] JSObject* AsyncFunctionAwait( |
| 193 |
JSContext* cx, JS::Handle<AsyncFunctionGeneratorObject*> genObj, |
| 194 |
JS::Handle<JS::Value> value); |
| 195 |
|
| 196 |
// If the await operation can be skipped and the resolution value for `val` can |
| 197 |
// be acquired, stored the resolved value to `resolved` and `true` to |
| 198 |
// `*canSkip`. Otherwise, stores `false` to `*canSkip`. |
| 199 |
[[nodiscard]] bool CanSkipAwait(JSContext* cx, JS::Handle<JS::Value> val, |
| 200 |
bool* canSkip); |
| 201 |
[[nodiscard]] bool ExtractAwaitValue(JSContext* cx, JS::Handle<JS::Value> val, |
| 202 |
JS::MutableHandle<JS::Value> resolved); |
| 203 |
|
| 204 |
bool AsyncFromSyncIteratorMethod(JSContext* cx, JS::CallArgs& args, |
| 205 |
CompletionKind completionKind); |
| 206 |
|
| 207 |
// Callback for describing promise reaction records, for use with |
| 208 |
// PromiseObject::getReactionRecords. |
| 209 |
struct PromiseReactionRecordBuilder { |
| 210 |
// A reaction record created by a call to 'then' or 'catch', with functions to |
| 211 |
// call on resolution or rejection, and the promise that will be settled |
| 212 |
// according to the result of calling them. |
| 213 |
// |
| 214 |
// Note that resolve, reject, and result may not be same-compartment with cx, |
| 215 |
// or with the promise we're inspecting. This function presents the values |
| 216 |
// exactly as they appear in the reaction record. They may also be wrapped or |
| 217 |
// unwrapped. |
| 218 |
// |
| 219 |
// Some reaction records refer to internal resolution or rejection functions |
| 220 |
// that are not naturally represented as debuggee JavaScript functions. In |
| 221 |
// this case, resolve and reject may be nullptr. |
| 222 |
[[nodiscard]] virtual bool then(JSContext* cx, JS::Handle<JSObject*> resolve, |
| 223 |
JS::Handle<JSObject*> reject, |
| 224 |
JS::Handle<JSObject*> result) = 0; |
| 225 |
|
| 226 |
// A reaction record created when one native promise is resolved to another. |
| 227 |
// The 'promise' argument is the promise that will be settled in the same way |
| 228 |
// the promise this reaction record is attached to is settled. |
| 229 |
// |
| 230 |
// Note that promise may not be same-compartment with cx. This function |
| 231 |
// presents the promise exactly as it appears in the reaction record. |
| 232 |
[[nodiscard]] virtual bool direct( |
| 233 |
JSContext* cx, JS::Handle<PromiseObject*> unwrappedPromise) = 0; |
| 234 |
|
| 235 |
// A reaction record that resumes an asynchronous function suspended at an |
| 236 |
// await expression. The 'generator' argument is the generator object |
| 237 |
// representing the call. |
| 238 |
// |
| 239 |
// Note that generator may not be same-compartment with cx. This function |
| 240 |
// presents the generator exactly as it appears in the reaction record. |
| 241 |
[[nodiscard]] virtual bool asyncFunction( |
| 242 |
JSContext* cx, |
| 243 |
JS::Handle<AsyncFunctionGeneratorObject*> unwrappedGenerator) = 0; |
| 244 |
|
| 245 |
// A reaction record that resumes an asynchronous generator suspended at an |
| 246 |
// await expression. The 'generator' argument is the generator object |
| 247 |
// representing the call. |
| 248 |
// |
| 249 |
// Note that generator may not be same-compartment with cx. This function |
| 250 |
// presents the generator exactly as it appears in the reaction record. |
| 251 |
[[nodiscard]] virtual bool asyncGenerator( |
| 252 |
JSContext* cx, JS::Handle<AsyncGeneratorObject*> unwrappedGenerator) = 0; |
| 253 |
}; |
| 254 |
|
| 255 |
[[nodiscard]] PromiseObject* CreatePromiseObjectForAsyncGenerator( |
| 256 |
JSContext* cx); |
| 257 |
|
| 258 |
// This implements "Let promiseCapability be ! NewPromiseCapability(%Promise%)". |
| 259 |
[[nodiscard]] PromiseObject* CreatePromiseObjectWithoutResolutionFunctions( |
| 260 |
JSContext* cx, int32_t extraFlags = 0); |
| 261 |
|
| 262 |
[[nodiscard]] bool ResolvePromiseInternal(JSContext* cx, |
| 263 |
JS::Handle<JSObject*> promise, |
| 264 |
JS::Handle<JS::Value> resolutionVal); |
| 265 |
[[nodiscard]] bool RejectPromiseInternal( |
| 266 |
JSContext* cx, JS::Handle<PromiseObject*> promise, |
| 267 |
JS::Handle<JS::Value> reason, |
| 268 |
JS::Handle<SavedFrame*> unwrappedRejectionStack = nullptr); |
| 269 |
|
| 270 |
[[nodiscard]] bool InternalAsyncGeneratorAwait( |
| 271 |
JSContext* cx, JS::Handle<AsyncGeneratorObject*> generator, |
| 272 |
JS::Handle<JS::Value> value, PromiseHandler onFulfilled, |
| 273 |
PromiseHandler onRejected); |
| 274 |
|
| 275 |
bool IsPromiseWithDefaultResolvingFunction(PromiseObject* promise); |
| 276 |
void SetAlreadyResolvedPromiseWithDefaultResolvingFunction( |
| 277 |
PromiseObject* promise); |
| 278 |
|
| 279 |
bool IsPromiseConstructor(const JSObject* obj); |
| 280 |
|
| 281 |
bool AbruptRejectPromise(JSContext* cx, JS::CallArgs& args, |
| 282 |
JS::Handle<JSObject*> promiseObj, |
| 283 |
JS::Handle<JSObject*> reject); |
| 284 |
|
| 285 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 286 |
[[nodiscard]] bool InternalAsyncIteratorDisposeAwait( |
| 287 |
JSContext* cx, JS::Handle<JS::Value> value, |
| 288 |
JS::Handle<JSObject*> resultPromise); |
| 289 |
#endif |
| 290 |
} // namespace js |
| 291 |
|
| 292 |
#endif // builtin_Promise_h |