| 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 |
#include "vm/AsyncIteration.h" |
| 8 |
|
| 9 |
#include "builtin/Promise.h" // js::PromiseHandler, js::CreatePromiseObjectForAsyncGenerator, js::AsyncFromSyncIteratorMethod, js::ResolvePromiseInternal, js::RejectPromiseInternal, js::InternalAsyncGeneratorAwait |
| 10 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 11 |
#include "js/PropertySpec.h" |
| 12 |
#include "vm/CompletionKind.h" |
| 13 |
#include "vm/FunctionFlags.h" // js::FunctionFlags |
| 14 |
#include "vm/GeneratorObject.h" |
| 15 |
#include "vm/GlobalObject.h" |
| 16 |
#include "vm/Interpreter.h" |
| 17 |
#include "vm/PlainObject.h" // js::PlainObject |
| 18 |
#include "vm/PromiseObject.h" // js::PromiseObject |
| 19 |
#include "vm/Realm.h" |
| 20 |
#include "vm/SelfHosting.h" |
| 21 |
|
| 22 |
#include "vm/JSObject-inl.h" |
| 23 |
#include "vm/List-inl.h" |
| 24 |
|
| 25 |
using namespace js; |
| 26 |
|
| 27 |
// --------------- |
| 28 |
// Async generator |
| 29 |
// --------------- |
| 30 |
|
| 31 |
const JSClass AsyncGeneratorObject::class_ = { |
| 32 |
"AsyncGenerator", |
| 33 |
JSCLASS_HAS_RESERVED_SLOTS(AsyncGeneratorObject::Slots), |
| 34 |
&classOps_, |
| 35 |
}; |
| 36 |
|
| 37 |
const JSClassOps AsyncGeneratorObject::classOps_ = { |
| 38 |
nullptr, // addProperty |
| 39 |
nullptr, // delProperty |
| 40 |
nullptr, // enumerate |
| 41 |
nullptr, // newEnumerate |
| 42 |
nullptr, // resolve |
| 43 |
nullptr, // mayResolve |
| 44 |
nullptr, // finalize |
| 45 |
nullptr, // call |
| 46 |
nullptr, // construct |
| 47 |
CallTraceMethod<AbstractGeneratorObject>, // trace |
| 48 |
}; |
| 49 |
|
| 50 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 51 |
// |
| 52 |
// OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto |
| 53 |
// [ , internalSlotsList ] ) |
| 54 |
// https://tc39.es/ecma262/#sec-ordinarycreatefromconstructor |
| 55 |
// |
| 56 |
// specialized for AsyncGeneratorObjects. |
| 57 |
static AsyncGeneratorObject* OrdinaryCreateFromConstructorAsynGen( |
| 58 |
JSContext* cx, HandleFunction constructor) { |
| 59 |
// Step 1: Assert... |
| 60 |
// (implicit) |
| 61 |
|
| 62 |
// Step 2. Let proto be |
| 63 |
// ? GetPrototypeFromConstructor(constructor, intrinsicDefaultProto). |
| 64 |
RootedValue protoVal(cx); |
| 65 |
if (!GetProperty(cx, constructor, constructor, cx->names().prototype, |
| 66 |
&protoVal)) { |
| 67 |
return nullptr; |
| 68 |
} |
| 69 |
|
| 70 |
RootedObject proto(cx, protoVal.isObject() ? &protoVal.toObject() : nullptr); |
| 71 |
if (!proto) { |
| 72 |
proto = GlobalObject::getOrCreateAsyncGeneratorPrototype(cx, cx->global()); |
| 73 |
if (!proto) { |
| 74 |
return nullptr; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Step 3. Return ! OrdinaryObjectCreate(proto, internalSlotsList). |
| 79 |
return NewObjectWithGivenProto<AsyncGeneratorObject>(cx, proto); |
| 80 |
} |
| 81 |
|
| 82 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 83 |
// |
| 84 |
// AsyncGeneratorStart ( generator, generatorBody ) |
| 85 |
// https://tc39.es/ecma262/#sec-asyncgeneratorstart |
| 86 |
// |
| 87 |
// Steps 6-7. |
| 88 |
/* static */ |
| 89 |
AsyncGeneratorObject* AsyncGeneratorObject::create(JSContext* cx, |
| 90 |
HandleFunction asyncGen) { |
| 91 |
MOZ_ASSERT(asyncGen->isAsync() && asyncGen->isGenerator()); |
| 92 |
|
| 93 |
AsyncGeneratorObject* generator = |
| 94 |
OrdinaryCreateFromConstructorAsynGen(cx, asyncGen); |
| 95 |
if (!generator) { |
| 96 |
return nullptr; |
| 97 |
} |
| 98 |
|
| 99 |
// Step 6. Set generator.[[AsyncGeneratorState]] to suspendedStart. |
| 100 |
generator->setSuspendedStart(); |
| 101 |
|
| 102 |
// Step 7. Set generator.[[AsyncGeneratorQueue]] to a new empty List. |
| 103 |
generator->clearSingleQueueRequest(); |
| 104 |
|
| 105 |
generator->clearCachedRequest(); |
| 106 |
|
| 107 |
return generator; |
| 108 |
} |
| 109 |
|
| 110 |
/* static */ |
| 111 |
AsyncGeneratorRequest* AsyncGeneratorObject::createRequest( |
| 112 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 113 |
CompletionKind completionKind, HandleValue completionValue, |
| 114 |
Handle<PromiseObject*> promise) { |
| 115 |
if (!generator->hasCachedRequest()) { |
| 116 |
return AsyncGeneratorRequest::create(cx, completionKind, completionValue, |
| 117 |
promise); |
| 118 |
} |
| 119 |
|
| 120 |
AsyncGeneratorRequest* request = generator->takeCachedRequest(); |
| 121 |
request->init(completionKind, completionValue, promise); |
| 122 |
return request; |
| 123 |
} |
| 124 |
|
| 125 |
/* static */ [[nodiscard]] bool AsyncGeneratorObject::enqueueRequest( |
| 126 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 127 |
Handle<AsyncGeneratorRequest*> request) { |
| 128 |
if (generator->isSingleQueue()) { |
| 129 |
if (generator->isSingleQueueEmpty()) { |
| 130 |
generator->setSingleQueueRequest(request); |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
Rooted<ListObject*> queue(cx, ListObject::create(cx)); |
| 135 |
if (!queue) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
RootedValue requestVal(cx, ObjectValue(*generator->singleQueueRequest())); |
| 140 |
if (!queue->append(cx, requestVal)) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
requestVal = ObjectValue(*request); |
| 144 |
if (!queue->append(cx, requestVal)) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
generator->setQueue(queue); |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
Rooted<ListObject*> queue(cx, generator->queue()); |
| 153 |
RootedValue requestVal(cx, ObjectValue(*request)); |
| 154 |
return queue->append(cx, requestVal); |
| 155 |
} |
| 156 |
|
| 157 |
/* static */ |
| 158 |
AsyncGeneratorRequest* AsyncGeneratorObject::dequeueRequest( |
| 159 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator) { |
| 160 |
if (generator->isSingleQueue()) { |
| 161 |
AsyncGeneratorRequest* request = generator->singleQueueRequest(); |
| 162 |
generator->clearSingleQueueRequest(); |
| 163 |
return request; |
| 164 |
} |
| 165 |
|
| 166 |
Rooted<ListObject*> queue(cx, generator->queue()); |
| 167 |
return &queue->popFirstAs<AsyncGeneratorRequest>(cx); |
| 168 |
} |
| 169 |
|
| 170 |
/* static */ |
| 171 |
AsyncGeneratorRequest* AsyncGeneratorObject::peekRequest( |
| 172 |
Handle<AsyncGeneratorObject*> generator) { |
| 173 |
if (generator->isSingleQueue()) { |
| 174 |
return generator->singleQueueRequest(); |
| 175 |
} |
| 176 |
|
| 177 |
return &generator->queue()->getAs<AsyncGeneratorRequest>(0); |
| 178 |
} |
| 179 |
|
| 180 |
const JSClass AsyncGeneratorRequest::class_ = { |
| 181 |
"AsyncGeneratorRequest", |
| 182 |
JSCLASS_HAS_RESERVED_SLOTS(AsyncGeneratorRequest::Slots), |
| 183 |
}; |
| 184 |
|
| 185 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 186 |
// |
| 187 |
// AsyncGeneratorRequest Records |
| 188 |
// https://tc39.es/ecma262/#sec-asyncgeneratorrequest-records |
| 189 |
/* static */ |
| 190 |
AsyncGeneratorRequest* AsyncGeneratorRequest::create( |
| 191 |
JSContext* cx, CompletionKind completionKind, HandleValue completionValue, |
| 192 |
Handle<PromiseObject*> promise) { |
| 193 |
AsyncGeneratorRequest* request = |
| 194 |
NewObjectWithGivenProto<AsyncGeneratorRequest>(cx, nullptr); |
| 195 |
if (!request) { |
| 196 |
return nullptr; |
| 197 |
} |
| 198 |
|
| 199 |
request->init(completionKind, completionValue, promise); |
| 200 |
return request; |
| 201 |
} |
| 202 |
|
| 203 |
[[nodiscard]] static bool AsyncGeneratorResume( |
| 204 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 205 |
CompletionKind completionKind, HandleValue argument); |
| 206 |
|
| 207 |
[[nodiscard]] static bool AsyncGeneratorDrainQueue( |
| 208 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator); |
| 209 |
|
| 210 |
[[nodiscard]] static bool AsyncGeneratorCompleteStepNormal( |
| 211 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value, |
| 212 |
bool done); |
| 213 |
|
| 214 |
[[nodiscard]] static bool AsyncGeneratorCompleteStepThrow( |
| 215 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 216 |
HandleValue exception); |
| 217 |
|
| 218 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 219 |
// |
| 220 |
// AsyncGeneratorStart ( generator, generatorBody ) |
| 221 |
// https://tc39.es/ecma262/#sec-asyncgeneratorstart |
| 222 |
// |
| 223 |
// Steps 4.e-j. "return" case. |
| 224 |
[[nodiscard]] static bool AsyncGeneratorReturned( |
| 225 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 226 |
// Step 4.e. Set generator.[[AsyncGeneratorState]] to completed. |
| 227 |
generator->setCompleted(); |
| 228 |
|
| 229 |
// Step 4.g. If result.[[Type]] is return, set result to |
| 230 |
// NormalCompletion(result.[[Value]]). |
| 231 |
// (implicit) |
| 232 |
|
| 233 |
// Step 4.h. Perform ! AsyncGeneratorCompleteStep(generator, result, true). |
| 234 |
if (!AsyncGeneratorCompleteStepNormal(cx, generator, value, true)) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
// Step 4.i. Perform ! AsyncGeneratorDrainQueue(generator). |
| 239 |
// Step 4.j. Return undefined. |
| 240 |
return AsyncGeneratorDrainQueue(cx, generator); |
| 241 |
} |
| 242 |
|
| 243 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 244 |
// |
| 245 |
// AsyncGeneratorStart ( generator, generatorBody ) |
| 246 |
// https://tc39.es/ecma262/#sec-asyncgeneratorstart |
| 247 |
// |
| 248 |
// Steps 4.e-j. "throw" case. |
| 249 |
[[nodiscard]] static bool AsyncGeneratorThrown( |
| 250 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator) { |
| 251 |
// Step 4.e. Set generator.[[AsyncGeneratorState]] to completed. |
| 252 |
generator->setCompleted(); |
| 253 |
|
| 254 |
// Not much we can do about uncatchable exceptions, so just bail. |
| 255 |
if (!cx->isExceptionPending()) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
// Step 4.h. Perform ! AsyncGeneratorCompleteStep(generator, result, true). |
| 260 |
RootedValue value(cx); |
| 261 |
if (!GetAndClearException(cx, &value)) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
if (!AsyncGeneratorCompleteStepThrow(cx, generator, value)) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
// Step 4.i. Perform ! AsyncGeneratorDrainQueue(generator). |
| 269 |
// Step 4.j. Return undefined. |
| 270 |
return AsyncGeneratorDrainQueue(cx, generator); |
| 271 |
} |
| 272 |
|
| 273 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 274 |
// |
| 275 |
// AsyncGeneratorUnwrapYieldResumption ( resumptionValue ) |
| 276 |
// https://tc39.es/ecma262/#sec-asyncgeneratorunwrapyieldresumption |
| 277 |
// |
| 278 |
// Steps 4-5. |
| 279 |
[[nodiscard]] static bool AsyncGeneratorYieldReturnAwaitedFulfilled( |
| 280 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 281 |
MOZ_ASSERT(generator->isAwaitingYieldReturn(), |
| 282 |
"YieldReturn-Await fulfilled when not in " |
| 283 |
"'AwaitingYieldReturn' state"); |
| 284 |
|
| 285 |
// Step 4. Assert: awaited.[[Type]] is normal. |
| 286 |
// Step 5. Return Completion { [[Type]]: return, [[Value]]: |
| 287 |
// awaited.[[Value]], [[Target]]: empty }. |
| 288 |
return AsyncGeneratorResume(cx, generator, CompletionKind::Return, value); |
| 289 |
} |
| 290 |
|
| 291 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 292 |
// |
| 293 |
// AsyncGeneratorUnwrapYieldResumption ( resumptionValue ) |
| 294 |
// https://tc39.es/ecma262/#sec-asyncgeneratorunwrapyieldresumption |
| 295 |
// |
| 296 |
// Step 3. |
| 297 |
[[nodiscard]] static bool AsyncGeneratorYieldReturnAwaitedRejected( |
| 298 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 299 |
HandleValue reason) { |
| 300 |
MOZ_ASSERT( |
| 301 |
generator->isAwaitingYieldReturn(), |
| 302 |
"YieldReturn-Await rejected when not in 'AwaitingYieldReturn' state"); |
| 303 |
|
| 304 |
// Step 3. If awaited.[[Type]] is throw, return Completion(awaited). |
| 305 |
return AsyncGeneratorResume(cx, generator, CompletionKind::Throw, reason); |
| 306 |
} |
| 307 |
|
| 308 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 309 |
// |
| 310 |
// AsyncGeneratorYield ( value ) |
| 311 |
// https://tc39.es/ecma262/#sec-asyncgeneratoryield |
| 312 |
// |
| 313 |
// Stesp 10-13. |
| 314 |
[[nodiscard]] static bool AsyncGeneratorYield( |
| 315 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 316 |
// Step 10. Perform |
| 317 |
// ! AsyncGeneratorCompleteStep(generator, completion, false, |
| 318 |
// previousRealm). |
| 319 |
if (!AsyncGeneratorCompleteStepNormal(cx, generator, value, false)) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
// Step 13.a. |
| 324 |
generator->setSuspendedYield(); |
| 325 |
|
| 326 |
// Steps 11-13. |
| 327 |
return AsyncGeneratorDrainQueue(cx, generator); |
| 328 |
} |
| 329 |
|
| 330 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 331 |
// |
| 332 |
// Await in async function |
| 333 |
// https://tc39.es/ecma262/#await |
| 334 |
// |
| 335 |
// Steps 3.c-f. |
| 336 |
[[nodiscard]] static bool AsyncGeneratorAwaitedFulfilled( |
| 337 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 338 |
MOZ_ASSERT(generator->isExecuting(), |
| 339 |
"Await fulfilled when not in 'Executing' state"); |
| 340 |
|
| 341 |
// Step 3.c. Push asyncContext onto the execution context stack; asyncContext |
| 342 |
// is now the running execution context. |
| 343 |
// Step 3.d. Resume the suspended evaluation of asyncContext using |
| 344 |
// NormalCompletion(value) as the result of the operation that |
| 345 |
// suspended it. |
| 346 |
// Step 3.f. Return undefined. |
| 347 |
return AsyncGeneratorResume(cx, generator, CompletionKind::Normal, value); |
| 348 |
} |
| 349 |
|
| 350 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 351 |
// |
| 352 |
// Await in async function |
| 353 |
// https://tc39.es/ecma262/#await |
| 354 |
// |
| 355 |
// Steps 5.c-f. |
| 356 |
[[nodiscard]] static bool AsyncGeneratorAwaitedRejected( |
| 357 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 358 |
HandleValue reason) { |
| 359 |
MOZ_ASSERT(generator->isExecuting(), |
| 360 |
"Await rejected when not in 'Executing' state"); |
| 361 |
|
| 362 |
// Step 5.c. Push asyncContext onto the execution context stack; asyncContext |
| 363 |
// is now the running execution context. |
| 364 |
// Step 5.d. Resume the suspended evaluation of asyncContext using |
| 365 |
// ThrowCompletion(reason) as the result of the operation that |
| 366 |
// suspended it. |
| 367 |
// Step 5.f. Return undefined. |
| 368 |
return AsyncGeneratorResume(cx, generator, CompletionKind::Throw, reason); |
| 369 |
} |
| 370 |
|
| 371 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 372 |
// |
| 373 |
// Await in async function |
| 374 |
// https://tc39.es/ecma262/#await |
| 375 |
[[nodiscard]] static bool AsyncGeneratorAwait( |
| 376 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 377 |
return InternalAsyncGeneratorAwait( |
| 378 |
cx, generator, value, PromiseHandler::AsyncGeneratorAwaitedFulfilled, |
| 379 |
PromiseHandler::AsyncGeneratorAwaitedRejected); |
| 380 |
} |
| 381 |
|
| 382 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 383 |
// |
| 384 |
// AsyncGeneratorCompleteStep ( generator, completion, done [ , realm ] ) |
| 385 |
// https://tc39.es/ecma262/#sec-asyncgeneratorcompletestep |
| 386 |
// |
| 387 |
// "normal" case. |
| 388 |
[[nodiscard]] static bool AsyncGeneratorCompleteStepNormal( |
| 389 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value, |
| 390 |
bool done) { |
| 391 |
// Step 1. Let queue be generator.[[AsyncGeneratorQueue]]. |
| 392 |
// Step 2. Assert: queue is not empty. |
| 393 |
MOZ_ASSERT(!generator->isQueueEmpty()); |
| 394 |
|
| 395 |
// Step 3. Let next be the first element of queue. |
| 396 |
// Step 4. Remove the first element from queue. |
| 397 |
AsyncGeneratorRequest* next = |
| 398 |
AsyncGeneratorObject::dequeueRequest(cx, generator); |
| 399 |
if (!next) { |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
// Step 5. Let promiseCapability be next.[[Capability]]. |
| 404 |
Rooted<PromiseObject*> resultPromise(cx, next->promise()); |
| 405 |
|
| 406 |
generator->cacheRequest(next); |
| 407 |
|
| 408 |
// Step 6. Let value be completion.[[Value]]. |
| 409 |
// (passed by caller) |
| 410 |
|
| 411 |
// Step 7. If completion.[[Type]] is throw, then |
| 412 |
// Step 8. Else, |
| 413 |
// Step 8.a. Assert: completion.[[Type]] is normal. |
| 414 |
|
| 415 |
// Step 8.b. If realm is present, then |
| 416 |
// (skipped) |
| 417 |
// Step 8.c. Else, |
| 418 |
|
| 419 |
// Step 8.c.i. Let iteratorResult be ! CreateIterResultObject(value, done). |
| 420 |
JSObject* resultObj = CreateIterResultObject(cx, value, done); |
| 421 |
if (!resultObj) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
|
| 425 |
// Step 8.d. Perform |
| 426 |
// ! Call(promiseCapability.[[Resolve]], undefined, |
| 427 |
// « iteratorResult »). |
| 428 |
RootedValue resultValue(cx, ObjectValue(*resultObj)); |
| 429 |
return ResolvePromiseInternal(cx, resultPromise, resultValue); |
| 430 |
} |
| 431 |
|
| 432 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 433 |
// |
| 434 |
// AsyncGeneratorCompleteStep ( generator, completion, done [ , realm ] ) |
| 435 |
// https://tc39.es/ecma262/#sec-asyncgeneratorcompletestep |
| 436 |
// |
| 437 |
// "throw" case. |
| 438 |
[[nodiscard]] static bool AsyncGeneratorCompleteStepThrow( |
| 439 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 440 |
HandleValue exception) { |
| 441 |
// Step 1. Let queue be generator.[[AsyncGeneratorQueue]]. |
| 442 |
// Step 2. Assert: queue is not empty. |
| 443 |
MOZ_ASSERT(!generator->isQueueEmpty()); |
| 444 |
|
| 445 |
// Step 3. Let next be the first element of queue. |
| 446 |
// Step 4. Remove the first element from queue. |
| 447 |
AsyncGeneratorRequest* next = |
| 448 |
AsyncGeneratorObject::dequeueRequest(cx, generator); |
| 449 |
if (!next) { |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
// Step 5. Let promiseCapability be next.[[Capability]]. |
| 454 |
Rooted<PromiseObject*> resultPromise(cx, next->promise()); |
| 455 |
|
| 456 |
generator->cacheRequest(next); |
| 457 |
|
| 458 |
// Step 6. Let value be completion.[[Value]]. |
| 459 |
// (passed by caller) |
| 460 |
|
| 461 |
// Step 7. If completion.[[Type]] is throw, then |
| 462 |
// Step 7.a. Perform |
| 463 |
// ! Call(promiseCapability.[[Reject]], undefined, « value »). |
| 464 |
return RejectPromiseInternal(cx, resultPromise, exception); |
| 465 |
} |
| 466 |
|
| 467 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 468 |
// |
| 469 |
// AsyncGeneratorAwaitReturn ( generator ) |
| 470 |
// https://tc39.es/ecma262/#sec-asyncgeneratorawaitreturn |
| 471 |
// |
| 472 |
// Steps 7.a-e. |
| 473 |
[[nodiscard]] static bool AsyncGeneratorAwaitReturnFulfilled( |
| 474 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 475 |
MOZ_ASSERT(generator->isAwaitingReturn(), |
| 476 |
"AsyncGeneratorResumeNext-Return fulfilled when not in " |
| 477 |
"'AwaitingReturn' state"); |
| 478 |
|
| 479 |
// Step 7.a. Set generator.[[AsyncGeneratorState]] to completed. |
| 480 |
generator->setCompleted(); |
| 481 |
|
| 482 |
// Step 7.b. Let result be NormalCompletion(value). |
| 483 |
// Step 7.c. Perform ! AsyncGeneratorCompleteStep(generator, result, true). |
| 484 |
if (!AsyncGeneratorCompleteStepNormal(cx, generator, value, true)) { |
| 485 |
return false; |
| 486 |
} |
| 487 |
|
| 488 |
// Step 7.d. Perform ! AsyncGeneratorDrainQueue(generator). |
| 489 |
// Step 7.e. Return undefined. |
| 490 |
return AsyncGeneratorDrainQueue(cx, generator); |
| 491 |
} |
| 492 |
|
| 493 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 494 |
// |
| 495 |
// AsyncGeneratorAwaitReturn ( generator ) |
| 496 |
// https://tc39.es/ecma262/#sec-asyncgeneratorawaitreturn |
| 497 |
// |
| 498 |
// Steps 9.a-e. |
| 499 |
[[nodiscard]] static bool AsyncGeneratorAwaitReturnRejected( |
| 500 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue value) { |
| 501 |
MOZ_ASSERT(generator->isAwaitingReturn(), |
| 502 |
"AsyncGeneratorResumeNext-Return rejected when not in " |
| 503 |
"'AwaitingReturn' state"); |
| 504 |
|
| 505 |
// Step 9.a. Set generator.[[AsyncGeneratorState]] to completed. |
| 506 |
generator->setCompleted(); |
| 507 |
|
| 508 |
// Step 9.b. Let result be ThrowCompletion(reason). |
| 509 |
// Step 9.c. Perform ! AsyncGeneratorCompleteStep(generator, result, true). |
| 510 |
if (!AsyncGeneratorCompleteStepThrow(cx, generator, value)) { |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
// Step 9.d. Perform ! AsyncGeneratorDrainQueue(generator). |
| 515 |
// Step 9.e. Return undefined. |
| 516 |
return AsyncGeneratorDrainQueue(cx, generator); |
| 517 |
} |
| 518 |
|
| 519 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 520 |
// |
| 521 |
// AsyncGeneratorAwaitReturn ( generator ) |
| 522 |
// https://tc39.es/ecma262/#sec-asyncgeneratorawaitreturn |
| 523 |
[[nodiscard]] static bool AsyncGeneratorAwaitReturn( |
| 524 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, HandleValue next) { |
| 525 |
// Step 1. Let queue be generator.[[AsyncGeneratorQueue]]. |
| 526 |
// Step 2. Assert: queue is not empty. |
| 527 |
MOZ_ASSERT(!generator->isQueueEmpty()); |
| 528 |
|
| 529 |
// Step 3. Let next be the first element of queue. |
| 530 |
// (passed by caller) |
| 531 |
|
| 532 |
// Step 4. Let completion be next.[[Completion]]. |
| 533 |
// Step 5. Assert: completion.[[Type]] is return. |
| 534 |
// (implicit) |
| 535 |
|
| 536 |
// Steps 6-11. |
| 537 |
return InternalAsyncGeneratorAwait( |
| 538 |
cx, generator, next, PromiseHandler::AsyncGeneratorAwaitReturnFulfilled, |
| 539 |
PromiseHandler::AsyncGeneratorAwaitReturnRejected); |
| 540 |
} |
| 541 |
|
| 542 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 543 |
// |
| 544 |
// AsyncGeneratorDrainQueue ( generator ) |
| 545 |
// https://tc39.es/ecma262/#sec-asyncgeneratordrainqueue |
| 546 |
[[nodiscard]] static bool AsyncGeneratorDrainQueue( |
| 547 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator) { |
| 548 |
// Step 1. Assert: generator.[[AsyncGeneratorState]] is completed. |
| 549 |
MOZ_ASSERT(!generator->isExecuting()); |
| 550 |
MOZ_ASSERT(!generator->isAwaitingYieldReturn()); |
| 551 |
if (generator->isAwaitingReturn()) { |
| 552 |
return true; |
| 553 |
} |
| 554 |
|
| 555 |
// Step 2. Let queue be generator.[[AsyncGeneratorQueue]]. |
| 556 |
// Step 3. If queue is empty, return. |
| 557 |
if (generator->isQueueEmpty()) { |
| 558 |
return true; |
| 559 |
} |
| 560 |
|
| 561 |
// Step 4. Let done be false. |
| 562 |
// (implicit) |
| 563 |
|
| 564 |
// Step 5. Repeat, while done is false, |
| 565 |
while (true) { |
| 566 |
// Step 5.a. Let next be the first element of queue. |
| 567 |
Rooted<AsyncGeneratorRequest*> next( |
| 568 |
cx, AsyncGeneratorObject::peekRequest(generator)); |
| 569 |
if (!next) { |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
// Step 5.b. Let completion be next.[[Completion]]. |
| 574 |
CompletionKind completionKind = next->completionKind(); |
| 575 |
|
| 576 |
if (completionKind != CompletionKind::Normal) { |
| 577 |
if (generator->isSuspendedStart()) { |
| 578 |
generator->setCompleted(); |
| 579 |
} |
| 580 |
} |
| 581 |
if (!generator->isCompleted()) { |
| 582 |
MOZ_ASSERT(generator->isSuspendedStart() || |
| 583 |
generator->isSuspendedYield()); |
| 584 |
|
| 585 |
RootedValue argument(cx, next->completionValue()); |
| 586 |
|
| 587 |
if (completionKind == CompletionKind::Return) { |
| 588 |
generator->setAwaitingYieldReturn(); |
| 589 |
|
| 590 |
return InternalAsyncGeneratorAwait( |
| 591 |
cx, generator, argument, |
| 592 |
PromiseHandler::AsyncGeneratorYieldReturnAwaitedFulfilled, |
| 593 |
PromiseHandler::AsyncGeneratorYieldReturnAwaitedRejected); |
| 594 |
} |
| 595 |
|
| 596 |
return AsyncGeneratorResume(cx, generator, completionKind, argument); |
| 597 |
} |
| 598 |
|
| 599 |
// Step 5.c. If completion.[[Type]] is return, then |
| 600 |
if (completionKind == CompletionKind::Return) { |
| 601 |
RootedValue value(cx, next->completionValue()); |
| 602 |
|
| 603 |
// Step 5.c.i. Set generator.[[AsyncGeneratorState]] to awaiting-return. |
| 604 |
generator->setAwaitingReturn(); |
| 605 |
|
| 606 |
// Step 5.c.ii. Perform ! AsyncGeneratorAwaitReturn(generator). |
| 607 |
// Step 5.c.iii. Set done to true. |
| 608 |
return AsyncGeneratorAwaitReturn(cx, generator, value); |
| 609 |
} |
| 610 |
|
| 611 |
// Step 5.d. Else, |
| 612 |
if (completionKind == CompletionKind::Throw) { |
| 613 |
RootedValue value(cx, next->completionValue()); |
| 614 |
|
| 615 |
// Step 5.d.ii. Perform |
| 616 |
// ! AsyncGeneratorCompleteStep(generator, completion, true). |
| 617 |
if (!AsyncGeneratorCompleteStepThrow(cx, generator, value)) { |
| 618 |
return false; |
| 619 |
} |
| 620 |
} else { |
| 621 |
// Step 5.d.i. If completion.[[Type]] is normal, then |
| 622 |
// Step 5.d.i.1. Set completion to NormalCompletion(undefined). |
| 623 |
// Step 5.d.ii. Perform |
| 624 |
// ! AsyncGeneratorCompleteStep(generator, completion, true). |
| 625 |
if (!AsyncGeneratorCompleteStepNormal(cx, generator, UndefinedHandleValue, |
| 626 |
true)) { |
| 627 |
return false; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
MOZ_ASSERT(!generator->isExecuting()); |
| 632 |
MOZ_ASSERT(!generator->isAwaitingYieldReturn()); |
| 633 |
if (generator->isAwaitingReturn()) { |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
// Step 5.d.iii. If queue is empty, set done to true. |
| 638 |
if (generator->isQueueEmpty()) { |
| 639 |
return true; |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 645 |
// |
| 646 |
// AsyncGeneratorValidate ( generator, generatorBrand ) |
| 647 |
// https://tc39.es/ecma262/#sec-asyncgeneratorvalidate |
| 648 |
// |
| 649 |
// Testing part. |
| 650 |
[[nodiscard]] static bool IsAsyncGeneratorValid(HandleValue asyncGenVal) { |
| 651 |
// Step 1. Perform |
| 652 |
// ? RequireInternalSlot(generator, [[AsyncGeneratorContext]]). |
| 653 |
// Step 2. Perform |
| 654 |
// ? RequireInternalSlot(generator, [[AsyncGeneratorState]]). |
| 655 |
// Step 3. Perform |
| 656 |
// ? RequireInternalSlot(generator, [[AsyncGeneratorQueue]]). |
| 657 |
// Step 4. If generator.[[GeneratorBrand]] is not the same value as |
| 658 |
// generatorBrand, throw a TypeError exception. |
| 659 |
return asyncGenVal.isObject() && |
| 660 |
asyncGenVal.toObject().canUnwrapAs<AsyncGeneratorObject>(); |
| 661 |
} |
| 662 |
|
| 663 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 664 |
// |
| 665 |
// AsyncGeneratorValidate ( generator, generatorBrand ) |
| 666 |
// https://tc39.es/ecma262/#sec-asyncgeneratorvalidate |
| 667 |
// |
| 668 |
// Throwing part. |
| 669 |
[[nodiscard]] static bool AsyncGeneratorValidateThrow( |
| 670 |
JSContext* cx, MutableHandleValue result) { |
| 671 |
Rooted<PromiseObject*> resultPromise( |
| 672 |
cx, CreatePromiseObjectForAsyncGenerator(cx)); |
| 673 |
if (!resultPromise) { |
| 674 |
return false; |
| 675 |
} |
| 676 |
|
| 677 |
RootedValue badGeneratorError(cx); |
| 678 |
if (!GetTypeError(cx, JSMSG_NOT_AN_ASYNC_GENERATOR, &badGeneratorError)) { |
| 679 |
return false; |
| 680 |
} |
| 681 |
|
| 682 |
if (!RejectPromiseInternal(cx, resultPromise, badGeneratorError)) { |
| 683 |
return false; |
| 684 |
} |
| 685 |
|
| 686 |
result.setObject(*resultPromise); |
| 687 |
return true; |
| 688 |
} |
| 689 |
|
| 690 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 691 |
// |
| 692 |
// AsyncGeneratorEnqueue ( generator, completion, promiseCapability ) |
| 693 |
// https://tc39.es/ecma262/#sec-asyncgeneratorenqueue |
| 694 |
[[nodiscard]] static bool AsyncGeneratorEnqueue( |
| 695 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 696 |
CompletionKind completionKind, HandleValue completionValue, |
| 697 |
Handle<PromiseObject*> resultPromise) { |
| 698 |
// Step 1. Let request be |
| 699 |
// AsyncGeneratorRequest { [[Completion]]: completion, |
| 700 |
// [[Capability]]: promiseCapability }. |
| 701 |
Rooted<AsyncGeneratorRequest*> request( |
| 702 |
cx, AsyncGeneratorObject::createRequest(cx, generator, completionKind, |
| 703 |
completionValue, resultPromise)); |
| 704 |
if (!request) { |
| 705 |
return false; |
| 706 |
} |
| 707 |
|
| 708 |
// Step 2. Append request to the end of generator.[[AsyncGeneratorQueue]]. |
| 709 |
return AsyncGeneratorObject::enqueueRequest(cx, generator, request); |
| 710 |
} |
| 711 |
|
| 712 |
class MOZ_STACK_CLASS MaybeEnterAsyncGeneratorRealm { |
| 713 |
mozilla::Maybe<AutoRealm> ar_; |
| 714 |
|
| 715 |
public: |
| 716 |
MaybeEnterAsyncGeneratorRealm() = default; |
| 717 |
~MaybeEnterAsyncGeneratorRealm() = default; |
| 718 |
|
| 719 |
// Enter async generator's realm, and wrap the method's argument value if |
| 720 |
// necessary. |
| 721 |
[[nodiscard]] bool maybeEnterAndWrap(JSContext* cx, |
| 722 |
Handle<AsyncGeneratorObject*> generator, |
| 723 |
MutableHandleValue value) { |
| 724 |
if (generator->compartment() == cx->compartment()) { |
| 725 |
return true; |
| 726 |
} |
| 727 |
|
| 728 |
ar_.emplace(cx, generator); |
| 729 |
return cx->compartment()->wrap(cx, value); |
| 730 |
} |
| 731 |
|
| 732 |
// Leave async generator's realm, and wrap the method's result value if |
| 733 |
// necessary. |
| 734 |
[[nodiscard]] bool maybeLeaveAndWrap(JSContext* cx, |
| 735 |
MutableHandleValue result) { |
| 736 |
if (!ar_) { |
| 737 |
return true; |
| 738 |
} |
| 739 |
ar_.reset(); |
| 740 |
|
| 741 |
return cx->compartment()->wrap(cx, result); |
| 742 |
} |
| 743 |
}; |
| 744 |
|
| 745 |
[[nodiscard]] static bool AsyncGeneratorMethodSanityCheck( |
| 746 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator) { |
| 747 |
if (generator->isSuspendedStart() || generator->isSuspendedYield()) { |
| 748 |
// The spec assumes the queue is empty when async generator methods are |
| 749 |
// called with those state, but our debugger allows calling those methods |
| 750 |
// in unexpected state, such as before suspendedStart. |
| 751 |
if (MOZ_UNLIKELY(!generator->isQueueEmpty())) { |
| 752 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 753 |
JSMSG_SUSPENDED_QUEUE_NOT_EMPTY); |
| 754 |
return false; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
return true; |
| 759 |
} |
| 760 |
|
| 761 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 762 |
// |
| 763 |
// AsyncGenerator.prototype.next ( value ) |
| 764 |
// https://tc39.es/ecma262/#sec-asyncgenerator-prototype-next |
| 765 |
bool js::AsyncGeneratorNext(JSContext* cx, unsigned argc, Value* vp) { |
| 766 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 767 |
|
| 768 |
// Step 3. Let result be AsyncGeneratorValidate(generator, empty). |
| 769 |
// Step 4. IfAbruptRejectPromise(result, promiseCapability). |
| 770 |
// (reordered) |
| 771 |
if (!IsAsyncGeneratorValid(args.thisv())) { |
| 772 |
return AsyncGeneratorValidateThrow(cx, args.rval()); |
| 773 |
} |
| 774 |
|
| 775 |
// Step 1. Let generator be the this value. |
| 776 |
// (implicit) |
| 777 |
Rooted<AsyncGeneratorObject*> generator( |
| 778 |
cx, &args.thisv().toObject().unwrapAs<AsyncGeneratorObject>()); |
| 779 |
|
| 780 |
MaybeEnterAsyncGeneratorRealm maybeEnterRealm; |
| 781 |
|
| 782 |
RootedValue completionValue(cx, args.get(0)); |
| 783 |
if (!maybeEnterRealm.maybeEnterAndWrap(cx, generator, &completionValue)) { |
| 784 |
return false; |
| 785 |
} |
| 786 |
|
| 787 |
// Step 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). |
| 788 |
Rooted<PromiseObject*> resultPromise( |
| 789 |
cx, CreatePromiseObjectForAsyncGenerator(cx)); |
| 790 |
if (!resultPromise) { |
| 791 |
return false; |
| 792 |
} |
| 793 |
|
| 794 |
if (!AsyncGeneratorMethodSanityCheck(cx, generator)) { |
| 795 |
return false; |
| 796 |
} |
| 797 |
|
| 798 |
// Steps 5-10. |
| 799 |
if (!AsyncGeneratorEnqueue(cx, generator, CompletionKind::Normal, |
| 800 |
completionValue, resultPromise)) { |
| 801 |
return false; |
| 802 |
} |
| 803 |
if (!generator->isExecuting() && !generator->isAwaitingYieldReturn()) { |
| 804 |
if (!AsyncGeneratorDrainQueue(cx, generator)) { |
| 805 |
return false; |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
// Step 6.c. Return promiseCapability.[[Promise]]. |
| 810 |
// and |
| 811 |
// Step 11. Return promiseCapability.[[Promise]]. |
| 812 |
args.rval().setObject(*resultPromise); |
| 813 |
|
| 814 |
return maybeEnterRealm.maybeLeaveAndWrap(cx, args.rval()); |
| 815 |
} |
| 816 |
|
| 817 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 818 |
// |
| 819 |
// AsyncGenerator.prototype.return ( value ) |
| 820 |
// https://tc39.es/ecma262/#sec-asyncgenerator-prototype-return |
| 821 |
bool js::AsyncGeneratorReturn(JSContext* cx, unsigned argc, Value* vp) { |
| 822 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 823 |
|
| 824 |
// Step 3. Let result be AsyncGeneratorValidate(generator, empty). |
| 825 |
// Step 4. IfAbruptRejectPromise(result, promiseCapability). |
| 826 |
// (reordered) |
| 827 |
if (!IsAsyncGeneratorValid(args.thisv())) { |
| 828 |
return AsyncGeneratorValidateThrow(cx, args.rval()); |
| 829 |
} |
| 830 |
|
| 831 |
// Step 1. Let generator be the this value. |
| 832 |
Rooted<AsyncGeneratorObject*> generator( |
| 833 |
cx, &args.thisv().toObject().unwrapAs<AsyncGeneratorObject>()); |
| 834 |
|
| 835 |
MaybeEnterAsyncGeneratorRealm maybeEnterRealm; |
| 836 |
|
| 837 |
RootedValue completionValue(cx, args.get(0)); |
| 838 |
if (!maybeEnterRealm.maybeEnterAndWrap(cx, generator, &completionValue)) { |
| 839 |
return false; |
| 840 |
} |
| 841 |
|
| 842 |
// Step 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). |
| 843 |
Rooted<PromiseObject*> resultPromise( |
| 844 |
cx, CreatePromiseObjectForAsyncGenerator(cx)); |
| 845 |
if (!resultPromise) { |
| 846 |
return false; |
| 847 |
} |
| 848 |
|
| 849 |
if (!AsyncGeneratorMethodSanityCheck(cx, generator)) { |
| 850 |
return false; |
| 851 |
} |
| 852 |
|
| 853 |
// Step 5. Let completion be |
| 854 |
// Completion { [[Type]]: return, [[Value]]: value, |
| 855 |
// [[Target]]: empty }. |
| 856 |
// Step 6. Perform |
| 857 |
// ! AsyncGeneratorEnqueue(generator, completion, promiseCapability). |
| 858 |
if (!AsyncGeneratorEnqueue(cx, generator, CompletionKind::Return, |
| 859 |
completionValue, resultPromise)) { |
| 860 |
return false; |
| 861 |
} |
| 862 |
|
| 863 |
// Steps 7-10. |
| 864 |
if (!generator->isExecuting() && !generator->isAwaitingYieldReturn()) { |
| 865 |
if (!AsyncGeneratorDrainQueue(cx, generator)) { |
| 866 |
return false; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
// Step 11. Return promiseCapability.[[Promise]]. |
| 871 |
args.rval().setObject(*resultPromise); |
| 872 |
|
| 873 |
return maybeEnterRealm.maybeLeaveAndWrap(cx, args.rval()); |
| 874 |
} |
| 875 |
|
| 876 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 877 |
// |
| 878 |
// AsyncGenerator.prototype.throw ( exception ) |
| 879 |
// https://tc39.es/ecma262/#sec-asyncgenerator-prototype-throw |
| 880 |
bool js::AsyncGeneratorThrow(JSContext* cx, unsigned argc, Value* vp) { |
| 881 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 882 |
|
| 883 |
// Step 3. Let result be AsyncGeneratorValidate(generator, empty). |
| 884 |
// Step 4. IfAbruptRejectPromise(result, promiseCapability). |
| 885 |
// (reordered) |
| 886 |
if (!IsAsyncGeneratorValid(args.thisv())) { |
| 887 |
return AsyncGeneratorValidateThrow(cx, args.rval()); |
| 888 |
} |
| 889 |
|
| 890 |
// Step 1. Let generator be the this value. |
| 891 |
Rooted<AsyncGeneratorObject*> generator( |
| 892 |
cx, &args.thisv().toObject().unwrapAs<AsyncGeneratorObject>()); |
| 893 |
|
| 894 |
MaybeEnterAsyncGeneratorRealm maybeEnterRealm; |
| 895 |
|
| 896 |
RootedValue completionValue(cx, args.get(0)); |
| 897 |
if (!maybeEnterRealm.maybeEnterAndWrap(cx, generator, &completionValue)) { |
| 898 |
return false; |
| 899 |
} |
| 900 |
|
| 901 |
// Step 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). |
| 902 |
Rooted<PromiseObject*> resultPromise( |
| 903 |
cx, CreatePromiseObjectForAsyncGenerator(cx)); |
| 904 |
if (!resultPromise) { |
| 905 |
return false; |
| 906 |
} |
| 907 |
|
| 908 |
if (!AsyncGeneratorMethodSanityCheck(cx, generator)) { |
| 909 |
return false; |
| 910 |
} |
| 911 |
|
| 912 |
// Steps 5-11. |
| 913 |
if (!AsyncGeneratorEnqueue(cx, generator, CompletionKind::Throw, |
| 914 |
completionValue, resultPromise)) { |
| 915 |
return false; |
| 916 |
} |
| 917 |
if (!generator->isExecuting() && !generator->isAwaitingYieldReturn()) { |
| 918 |
if (!AsyncGeneratorDrainQueue(cx, generator)) { |
| 919 |
return false; |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
// Step 7.b. Return promiseCapability.[[Promise]]. |
| 924 |
// and |
| 925 |
// Step 12. Return promiseCapability.[[Promise]]. |
| 926 |
args.rval().setObject(*resultPromise); |
| 927 |
|
| 928 |
return maybeEnterRealm.maybeLeaveAndWrap(cx, args.rval()); |
| 929 |
} |
| 930 |
|
| 931 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 932 |
// |
| 933 |
// AsyncGeneratorResume ( generator, completion ) |
| 934 |
// https://tc39.es/ecma262/#sec-asyncgeneratorresume |
| 935 |
[[nodiscard]] static bool AsyncGeneratorResume( |
| 936 |
JSContext* cx, Handle<AsyncGeneratorObject*> generator, |
| 937 |
CompletionKind completionKind, HandleValue argument) { |
| 938 |
MOZ_ASSERT(!generator->isClosed(), |
| 939 |
"closed generator when resuming async generator"); |
| 940 |
MOZ_ASSERT(generator->isSuspended(), |
| 941 |
"non-suspended generator when resuming async generator"); |
| 942 |
|
| 943 |
// Step 1. Assert: generator.[[AsyncGeneratorState]] is either |
| 944 |
// suspendedStart or suspendedYield. |
| 945 |
// |
| 946 |
// NOTE: We're using suspend/resume also for await. and the state can be |
| 947 |
// anything. |
| 948 |
|
| 949 |
// Steps 2-4 are handled in generator. |
| 950 |
|
| 951 |
// Step 5. Set generator.[[AsyncGeneratorState]] to executing. |
| 952 |
generator->setExecuting(); |
| 953 |
|
| 954 |
// Step 6. Push genContext onto the execution context stack; genContext is |
| 955 |
// now the running execution context. |
| 956 |
// Step 7. Resume the suspended evaluation of genContext using completion as |
| 957 |
// the result of the operation that suspended it. Let result be the |
| 958 |
// completion record returned by the resumed computation. |
| 959 |
Handle<PropertyName*> funName = completionKind == CompletionKind::Normal |
| 960 |
? cx->names().AsyncGeneratorNext |
| 961 |
: completionKind == CompletionKind::Throw |
| 962 |
? cx->names().AsyncGeneratorThrow |
| 963 |
: cx->names().AsyncGeneratorReturn; |
| 964 |
FixedInvokeArgs<1> args(cx); |
| 965 |
args[0].set(argument); |
| 966 |
RootedValue thisOrRval(cx, ObjectValue(*generator)); |
| 967 |
if (!CallSelfHostedFunction(cx, funName, thisOrRval, args, &thisOrRval)) { |
| 968 |
// 25.5.3.2, steps 5.f, 5.g. |
| 969 |
if (!generator->isClosed()) { |
| 970 |
generator->setClosed(cx); |
| 971 |
} |
| 972 |
return AsyncGeneratorThrown(cx, generator); |
| 973 |
} |
| 974 |
|
| 975 |
// 6.2.3.1, steps 2-9. |
| 976 |
if (generator->isAfterAwait()) { |
| 977 |
return AsyncGeneratorAwait(cx, generator, thisOrRval); |
| 978 |
} |
| 979 |
|
| 980 |
// 25.5.3.7, steps 5-6, 9. |
| 981 |
if (generator->isAfterYield()) { |
| 982 |
return AsyncGeneratorYield(cx, generator, thisOrRval); |
| 983 |
} |
| 984 |
|
| 985 |
// 25.5.3.2, steps 5.d-g. |
| 986 |
return AsyncGeneratorReturned(cx, generator, thisOrRval); |
| 987 |
} |
| 988 |
|
| 989 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 990 |
/** |
| 991 |
* Explicit Resource Management Proposal |
| 992 |
* 27.1.3.1 %AsyncIteratorPrototype% [ @@asyncDispose ] ( ) |
| 993 |
* https://arai-a.github.io/ecma262-compare/?pr=3000&id=sec-%25asynciteratorprototype%25-%40%40asyncdispose |
| 994 |
*/ |
| 995 |
static bool AsyncIteratorDispose(JSContext* cx, unsigned argc, Value* vp) { |
| 996 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 997 |
|
| 998 |
// Step 1. Let O be the this value. |
| 999 |
JS::Rooted<JS::Value> O(cx, args.thisv()); |
| 1000 |
|
| 1001 |
// Step 2. Let promiseCapability be ! NewPromiseCapability(%Promise%). |
| 1002 |
JS::Rooted<PromiseObject*> promise(cx, |
| 1003 |
PromiseObject::createSkippingExecutor(cx)); |
| 1004 |
if (!promise) { |
| 1005 |
return false; |
| 1006 |
} |
| 1007 |
|
| 1008 |
// Step 3. Let return be Completion(GetMethod(O, "return")). |
| 1009 |
JS::Rooted<JS::Value> returnMethod(cx); |
| 1010 |
if (!GetProperty(cx, O, cx->names().return_, &returnMethod)) { |
| 1011 |
// Step 4. IfAbruptRejectPromise(return, promiseCapability). |
| 1012 |
return AbruptRejectPromise(cx, args, promise, nullptr); |
| 1013 |
} |
| 1014 |
|
| 1015 |
// Step 5. If return is undefined, then |
| 1016 |
// As per the spec GetMethod returns undefined if the property is either null |
| 1017 |
// or undefined thus here we check for both. |
| 1018 |
if (returnMethod.isNullOrUndefined()) { |
| 1019 |
// Step 5.a. Perform ! Call(promiseCapability.[[Resolve]], undefined, « |
| 1020 |
// undefined »). |
| 1021 |
if (!PromiseObject::resolve(cx, promise, JS::UndefinedHandleValue)) { |
| 1022 |
return false; |
| 1023 |
} |
| 1024 |
args.rval().setObject(*promise); |
| 1025 |
return true; |
| 1026 |
} |
| 1027 |
|
| 1028 |
// GetMethod also throws a TypeError exception if the function is not callable |
| 1029 |
// thus we perform that check here. |
| 1030 |
if (!IsCallable(returnMethod)) { |
| 1031 |
ReportIsNotFunction(cx, returnMethod); |
| 1032 |
return AbruptRejectPromise(cx, args, promise, nullptr); |
| 1033 |
} |
| 1034 |
|
| 1035 |
// Step 6. Else, |
| 1036 |
// Step 6.a. Let result be Completion(Call(return, O, « undefined »)). |
| 1037 |
JS::Rooted<JS::Value> rval(cx); |
| 1038 |
if (!Call(cx, returnMethod, O, JS::UndefinedHandleValue, &rval)) { |
| 1039 |
// Step 6.b. IfAbruptRejectPromise(result, promiseCapability). |
| 1040 |
return AbruptRejectPromise(cx, args, promise, nullptr); |
| 1041 |
} |
| 1042 |
|
| 1043 |
// Step 6.c-g. |
| 1044 |
if (!InternalAsyncIteratorDisposeAwait(cx, rval, promise)) { |
| 1045 |
return AbruptRejectPromise(cx, args, promise, nullptr); |
| 1046 |
} |
| 1047 |
|
| 1048 |
// Step 7. Return promiseCapability.[[Promise]]. |
| 1049 |
args.rval().setObject(*promise); |
| 1050 |
return true; |
| 1051 |
} |
| 1052 |
#endif |
| 1053 |
|
| 1054 |
static const JSFunctionSpec async_generator_methods[] = { |
| 1055 |
JS_FN("next", js::AsyncGeneratorNext, 1, 0), |
| 1056 |
JS_FN("throw", js::AsyncGeneratorThrow, 1, 0), |
| 1057 |
JS_FN("return", js::AsyncGeneratorReturn, 1, 0), |
| 1058 |
JS_FS_END, |
| 1059 |
}; |
| 1060 |
|
| 1061 |
static JSObject* CreateAsyncGeneratorFunction(JSContext* cx, JSProtoKey key) { |
| 1062 |
RootedObject proto(cx, &cx->global()->getFunctionConstructor()); |
| 1063 |
Handle<PropertyName*> name = cx->names().AsyncGeneratorFunction; |
| 1064 |
|
| 1065 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 1066 |
// |
| 1067 |
// The AsyncGeneratorFunction Constructor |
| 1068 |
// https://tc39.es/ecma262/#sec-asyncgeneratorfunction-constructor |
| 1069 |
return NewFunctionWithProto(cx, AsyncGeneratorConstructor, 1, |
| 1070 |
FunctionFlags::NATIVE_CTOR, nullptr, name, proto, |
| 1071 |
gc::AllocKind::FUNCTION, TenuredObject); |
| 1072 |
} |
| 1073 |
|
| 1074 |
static JSObject* CreateAsyncGeneratorFunctionPrototype(JSContext* cx, |
| 1075 |
JSProtoKey key) { |
| 1076 |
return NewTenuredObjectWithFunctionPrototype(cx, cx->global()); |
| 1077 |
} |
| 1078 |
|
| 1079 |
static bool AsyncGeneratorFunctionClassFinish(JSContext* cx, |
| 1080 |
HandleObject asyncGenFunction, |
| 1081 |
HandleObject asyncGenerator) { |
| 1082 |
Handle<GlobalObject*> global = cx->global(); |
| 1083 |
|
| 1084 |
// Change the "constructor" property to non-writable before adding any other |
| 1085 |
// properties, so it's still the last property and can be modified without a |
| 1086 |
// dictionary-mode transition. |
| 1087 |
MOZ_ASSERT(asyncGenerator->as<NativeObject>().getLastProperty().key() == |
| 1088 |
NameToId(cx->names().constructor)); |
| 1089 |
MOZ_ASSERT(!asyncGenerator->as<NativeObject>().inDictionaryMode()); |
| 1090 |
|
| 1091 |
RootedValue asyncGenFunctionVal(cx, ObjectValue(*asyncGenFunction)); |
| 1092 |
if (!DefineDataProperty(cx, asyncGenerator, cx->names().constructor, |
| 1093 |
asyncGenFunctionVal, JSPROP_READONLY)) { |
| 1094 |
return false; |
| 1095 |
} |
| 1096 |
MOZ_ASSERT(!asyncGenerator->as<NativeObject>().inDictionaryMode()); |
| 1097 |
|
| 1098 |
RootedObject asyncIterProto( |
| 1099 |
cx, GlobalObject::getOrCreateAsyncIteratorPrototype(cx, global)); |
| 1100 |
if (!asyncIterProto) { |
| 1101 |
return false; |
| 1102 |
} |
| 1103 |
|
| 1104 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 1105 |
// |
| 1106 |
// AsyncGenerator Objects |
| 1107 |
// https://tc39.es/ecma262/#sec-asyncgenerator-objects |
| 1108 |
RootedObject asyncGenProto(cx, GlobalObject::createBlankPrototypeInheriting( |
| 1109 |
cx, &PlainObject::class_, asyncIterProto)); |
| 1110 |
if (!asyncGenProto) { |
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
if (!DefinePropertiesAndFunctions(cx, asyncGenProto, nullptr, |
| 1114 |
async_generator_methods) || |
| 1115 |
!DefineToStringTag(cx, asyncGenProto, cx->names().AsyncGenerator)) { |
| 1116 |
return false; |
| 1117 |
} |
| 1118 |
|
| 1119 |
// ES2022 draft rev 193211a3d889a61e74ef7da1475dfa356e029f29 |
| 1120 |
// |
| 1121 |
// Properties of the AsyncGeneratorFunction Prototype Object |
| 1122 |
// https://tc39.es/ecma262/#sec-properties-of-asyncgeneratorfunction-prototype |
| 1123 |
if (!LinkConstructorAndPrototype(cx, asyncGenerator, asyncGenProto, |
| 1124 |
JSPROP_READONLY, JSPROP_READONLY) || |
| 1125 |
!DefineToStringTag(cx, asyncGenerator, |
| 1126 |
cx->names().AsyncGeneratorFunction)) { |
| 1127 |
return false; |
| 1128 |
} |
| 1129 |
|
| 1130 |
global->setAsyncGeneratorPrototype(asyncGenProto); |
| 1131 |
|
| 1132 |
return true; |
| 1133 |
} |
| 1134 |
|
| 1135 |
static const ClassSpec AsyncGeneratorFunctionClassSpec = { |
| 1136 |
CreateAsyncGeneratorFunction, |
| 1137 |
CreateAsyncGeneratorFunctionPrototype, |
| 1138 |
nullptr, |
| 1139 |
nullptr, |
| 1140 |
nullptr, |
| 1141 |
nullptr, |
| 1142 |
AsyncGeneratorFunctionClassFinish, |
| 1143 |
ClassSpec::DontDefineConstructor, |
| 1144 |
}; |
| 1145 |
|
| 1146 |
const JSClass js::AsyncGeneratorFunctionClass = { |
| 1147 |
"AsyncGeneratorFunction", |
| 1148 |
0, |
| 1149 |
JS_NULL_CLASS_OPS, |
| 1150 |
&AsyncGeneratorFunctionClassSpec, |
| 1151 |
}; |
| 1152 |
|
| 1153 |
[[nodiscard]] bool js::AsyncGeneratorPromiseReactionJob( |
| 1154 |
JSContext* cx, PromiseHandler handler, |
| 1155 |
Handle<AsyncGeneratorObject*> generator, HandleValue argument) { |
| 1156 |
// Await's handlers don't return a value, nor throw any exceptions. |
| 1157 |
// They fail only on OOM. |
| 1158 |
switch (handler) { |
| 1159 |
case PromiseHandler::AsyncGeneratorAwaitedFulfilled: |
| 1160 |
return AsyncGeneratorAwaitedFulfilled(cx, generator, argument); |
| 1161 |
|
| 1162 |
case PromiseHandler::AsyncGeneratorAwaitedRejected: |
| 1163 |
return AsyncGeneratorAwaitedRejected(cx, generator, argument); |
| 1164 |
|
| 1165 |
case PromiseHandler::AsyncGeneratorAwaitReturnFulfilled: |
| 1166 |
return AsyncGeneratorAwaitReturnFulfilled(cx, generator, argument); |
| 1167 |
|
| 1168 |
case PromiseHandler::AsyncGeneratorAwaitReturnRejected: |
| 1169 |
return AsyncGeneratorAwaitReturnRejected(cx, generator, argument); |
| 1170 |
|
| 1171 |
case PromiseHandler::AsyncGeneratorYieldReturnAwaitedFulfilled: |
| 1172 |
return AsyncGeneratorYieldReturnAwaitedFulfilled(cx, generator, argument); |
| 1173 |
|
| 1174 |
case PromiseHandler::AsyncGeneratorYieldReturnAwaitedRejected: |
| 1175 |
return AsyncGeneratorYieldReturnAwaitedRejected(cx, generator, argument); |
| 1176 |
|
| 1177 |
default: |
| 1178 |
MOZ_CRASH("Bad handler in AsyncGeneratorPromiseReactionJob"); |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
// --------------------- |
| 1183 |
// AsyncFromSyncIterator |
| 1184 |
// --------------------- |
| 1185 |
|
| 1186 |
const JSClass AsyncFromSyncIteratorObject::class_ = { |
| 1187 |
"AsyncFromSyncIteratorObject", |
| 1188 |
JSCLASS_HAS_RESERVED_SLOTS(AsyncFromSyncIteratorObject::Slots), |
| 1189 |
}; |
| 1190 |
|
| 1191 |
/* |
| 1192 |
* ES2024 draft rev 53454a9a596d90473d2152ef04656d605162cd4c |
| 1193 |
* |
| 1194 |
* CreateAsyncFromSyncIterator ( syncIteratorRecord ) |
| 1195 |
* https://tc39.es/ecma262/#sec-createasyncfromsynciterator |
| 1196 |
*/ |
| 1197 |
JSObject* js::CreateAsyncFromSyncIterator(JSContext* cx, HandleObject iter, |
| 1198 |
HandleValue nextMethod) { |
| 1199 |
// Steps 1-5. |
| 1200 |
return AsyncFromSyncIteratorObject::create(cx, iter, nextMethod); |
| 1201 |
} |
| 1202 |
|
| 1203 |
/* |
| 1204 |
* ES2024 draft rev 53454a9a596d90473d2152ef04656d605162cd4c |
| 1205 |
* |
| 1206 |
* CreateAsyncFromSyncIterator ( syncIteratorRecord ) |
| 1207 |
* https://tc39.es/ecma262/#sec-createasyncfromsynciterator |
| 1208 |
*/ |
| 1209 |
/* static */ |
| 1210 |
JSObject* AsyncFromSyncIteratorObject::create(JSContext* cx, HandleObject iter, |
| 1211 |
HandleValue nextMethod) { |
| 1212 |
// Step 1. Let asyncIterator be |
| 1213 |
// OrdinaryObjectCreate(%AsyncFromSyncIteratorPrototype%, « |
| 1214 |
// [[SyncIteratorRecord]] »). |
| 1215 |
RootedObject proto(cx, |
| 1216 |
GlobalObject::getOrCreateAsyncFromSyncIteratorPrototype( |
| 1217 |
cx, cx->global())); |
| 1218 |
if (!proto) { |
| 1219 |
return nullptr; |
| 1220 |
} |
| 1221 |
|
| 1222 |
AsyncFromSyncIteratorObject* asyncIter = |
| 1223 |
NewObjectWithGivenProto<AsyncFromSyncIteratorObject>(cx, proto); |
| 1224 |
if (!asyncIter) { |
| 1225 |
return nullptr; |
| 1226 |
} |
| 1227 |
|
| 1228 |
// Step 3. Let nextMethod be ! Get(asyncIterator, "next"). |
| 1229 |
// (done in caller) |
| 1230 |
|
| 1231 |
// Step 2. Set asyncIterator.[[SyncIteratorRecord]] to syncIteratorRecord. |
| 1232 |
// Step 4. Let iteratorRecord be the Iterator Record { [[Iterator]]: |
| 1233 |
// asyncIterator, [[NextMethod]]: nextMethod, [[Done]]: false }. |
| 1234 |
asyncIter->init(iter, nextMethod); |
| 1235 |
|
| 1236 |
// Step 5. Return iteratorRecord. |
| 1237 |
return asyncIter; |
| 1238 |
} |
| 1239 |
|
| 1240 |
/** |
| 1241 |
* ES2024 draft rev 53454a9a596d90473d2152ef04656d605162cd4c |
| 1242 |
* |
| 1243 |
* %AsyncFromSyncIteratorPrototype%.next ( [ value ] ) |
| 1244 |
* https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%.next |
| 1245 |
*/ |
| 1246 |
static bool AsyncFromSyncIteratorNext(JSContext* cx, unsigned argc, Value* vp) { |
| 1247 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1248 |
return AsyncFromSyncIteratorMethod(cx, args, CompletionKind::Normal); |
| 1249 |
} |
| 1250 |
|
| 1251 |
/** |
| 1252 |
* ES2024 draft rev 53454a9a596d90473d2152ef04656d605162cd4c |
| 1253 |
* |
| 1254 |
* %AsyncFromSyncIteratorPrototype%.return ( [ value ] ) |
| 1255 |
* https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%.return |
| 1256 |
*/ |
| 1257 |
static bool AsyncFromSyncIteratorReturn(JSContext* cx, unsigned argc, |
| 1258 |
Value* vp) { |
| 1259 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1260 |
return AsyncFromSyncIteratorMethod(cx, args, CompletionKind::Return); |
| 1261 |
} |
| 1262 |
|
| 1263 |
/** |
| 1264 |
* ES2024 draft rev 53454a9a596d90473d2152ef04656d605162cd4c |
| 1265 |
* |
| 1266 |
* %AsyncFromSyncIteratorPrototype%.throw ( [ value ] ) |
| 1267 |
* https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%.throw |
| 1268 |
*/ |
| 1269 |
static bool AsyncFromSyncIteratorThrow(JSContext* cx, unsigned argc, |
| 1270 |
Value* vp) { |
| 1271 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1272 |
return AsyncFromSyncIteratorMethod(cx, args, CompletionKind::Throw); |
| 1273 |
} |
| 1274 |
|
| 1275 |
static const JSFunctionSpec async_from_sync_iter_methods[] = { |
| 1276 |
JS_FN("next", AsyncFromSyncIteratorNext, 1, 0), |
| 1277 |
JS_FN("throw", AsyncFromSyncIteratorThrow, 1, 0), |
| 1278 |
JS_FN("return", AsyncFromSyncIteratorReturn, 1, 0), |
| 1279 |
JS_FS_END, |
| 1280 |
}; |
| 1281 |
|
| 1282 |
bool GlobalObject::initAsyncFromSyncIteratorProto( |
| 1283 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 1284 |
if (global->hasBuiltinProto(ProtoKind::AsyncFromSyncIteratorProto)) { |
| 1285 |
return true; |
| 1286 |
} |
| 1287 |
|
| 1288 |
RootedObject asyncIterProto( |
| 1289 |
cx, GlobalObject::getOrCreateAsyncIteratorPrototype(cx, global)); |
| 1290 |
if (!asyncIterProto) { |
| 1291 |
return false; |
| 1292 |
} |
| 1293 |
|
| 1294 |
// ES2024 draft rev 53454a9a596d90473d2152ef04656d605162cd4c |
| 1295 |
// |
| 1296 |
// The %AsyncFromSyncIteratorPrototype% Object |
| 1297 |
// https://tc39.es/ecma262/#sec-%asyncfromsynciteratorprototype%-object |
| 1298 |
RootedObject asyncFromSyncIterProto( |
| 1299 |
cx, GlobalObject::createBlankPrototypeInheriting(cx, &PlainObject::class_, |
| 1300 |
asyncIterProto)); |
| 1301 |
if (!asyncFromSyncIterProto) { |
| 1302 |
return false; |
| 1303 |
} |
| 1304 |
if (!DefinePropertiesAndFunctions(cx, asyncFromSyncIterProto, nullptr, |
| 1305 |
async_from_sync_iter_methods) || |
| 1306 |
!DefineToStringTag(cx, asyncFromSyncIterProto, |
| 1307 |
cx->names().Async_from_Sync_Iterator_)) { |
| 1308 |
return false; |
| 1309 |
} |
| 1310 |
|
| 1311 |
global->initBuiltinProto(ProtoKind::AsyncFromSyncIteratorProto, |
| 1312 |
asyncFromSyncIterProto); |
| 1313 |
return true; |
| 1314 |
} |
| 1315 |
|
| 1316 |
// ------------- |
| 1317 |
// AsyncIterator |
| 1318 |
// ------------- |
| 1319 |
|
| 1320 |
static const JSFunctionSpec async_iterator_proto_methods[] = { |
| 1321 |
JS_SELF_HOSTED_SYM_FN(asyncIterator, "AsyncIteratorIdentity", 0, 0), |
| 1322 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1323 |
JS_SYM_FN(asyncDispose, AsyncIteratorDispose, 0, 0), |
| 1324 |
#endif |
| 1325 |
JS_FS_END, |
| 1326 |
}; |
| 1327 |
|
| 1328 |
static const JSFunctionSpec async_iterator_proto_methods_with_helpers[] = { |
| 1329 |
JS_SELF_HOSTED_FN("map", "AsyncIteratorMap", 1, 0), |
| 1330 |
JS_SELF_HOSTED_FN("filter", "AsyncIteratorFilter", 1, 0), |
| 1331 |
JS_SELF_HOSTED_FN("take", "AsyncIteratorTake", 1, 0), |
| 1332 |
JS_SELF_HOSTED_FN("drop", "AsyncIteratorDrop", 1, 0), |
| 1333 |
JS_SELF_HOSTED_FN("asIndexedPairs", "AsyncIteratorAsIndexedPairs", 0, 0), |
| 1334 |
JS_SELF_HOSTED_FN("flatMap", "AsyncIteratorFlatMap", 1, 0), |
| 1335 |
JS_SELF_HOSTED_FN("reduce", "AsyncIteratorReduce", 1, 0), |
| 1336 |
JS_SELF_HOSTED_FN("toArray", "AsyncIteratorToArray", 0, 0), |
| 1337 |
JS_SELF_HOSTED_FN("forEach", "AsyncIteratorForEach", 1, 0), |
| 1338 |
JS_SELF_HOSTED_FN("some", "AsyncIteratorSome", 1, 0), |
| 1339 |
JS_SELF_HOSTED_FN("every", "AsyncIteratorEvery", 1, 0), |
| 1340 |
JS_SELF_HOSTED_FN("find", "AsyncIteratorFind", 1, 0), |
| 1341 |
JS_SELF_HOSTED_SYM_FN(asyncIterator, "AsyncIteratorIdentity", 0, 0), |
| 1342 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1343 |
JS_SYM_FN(asyncDispose, AsyncIteratorDispose, 0, 0), |
| 1344 |
#endif |
| 1345 |
JS_FS_END, |
| 1346 |
}; |
| 1347 |
|
| 1348 |
bool GlobalObject::initAsyncIteratorProto(JSContext* cx, |
| 1349 |
Handle<GlobalObject*> global) { |
| 1350 |
if (global->hasBuiltinProto(ProtoKind::AsyncIteratorProto)) { |
| 1351 |
return true; |
| 1352 |
} |
| 1353 |
|
| 1354 |
// 25.1.3 The %AsyncIteratorPrototype% Object |
| 1355 |
RootedObject asyncIterProto( |
| 1356 |
cx, GlobalObject::createBlankPrototype<PlainObject>(cx, global)); |
| 1357 |
if (!asyncIterProto) { |
| 1358 |
return false; |
| 1359 |
} |
| 1360 |
if (!DefinePropertiesAndFunctions(cx, asyncIterProto, nullptr, |
| 1361 |
async_iterator_proto_methods)) { |
| 1362 |
return false; |
| 1363 |
} |
| 1364 |
|
| 1365 |
global->initBuiltinProto(ProtoKind::AsyncIteratorProto, asyncIterProto); |
| 1366 |
return true; |
| 1367 |
} |
| 1368 |
|
| 1369 |
// https://tc39.es/proposal-iterator-helpers/#sec-asynciterator as of revision |
| 1370 |
// 8f10db5. |
| 1371 |
static bool AsyncIteratorConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 1372 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1373 |
|
| 1374 |
// Step 1. |
| 1375 |
if (!ThrowIfNotConstructing(cx, args, "AsyncIterator")) { |
| 1376 |
return false; |
| 1377 |
} |
| 1378 |
// Throw TypeError if NewTarget is the active function object, preventing the |
| 1379 |
// Iterator constructor from being used directly. |
| 1380 |
if (args.callee() == args.newTarget().toObject()) { |
| 1381 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1382 |
JSMSG_BOGUS_CONSTRUCTOR, "AsyncIterator"); |
| 1383 |
return false; |
| 1384 |
} |
| 1385 |
|
| 1386 |
// Step 2. |
| 1387 |
RootedObject proto(cx); |
| 1388 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_AsyncIterator, |
| 1389 |
&proto)) { |
| 1390 |
return false; |
| 1391 |
} |
| 1392 |
|
| 1393 |
JSObject* obj = NewObjectWithClassProto<AsyncIteratorObject>(cx, proto); |
| 1394 |
if (!obj) { |
| 1395 |
return false; |
| 1396 |
} |
| 1397 |
|
| 1398 |
args.rval().setObject(*obj); |
| 1399 |
return true; |
| 1400 |
} |
| 1401 |
|
| 1402 |
static const ClassSpec AsyncIteratorObjectClassSpec = { |
| 1403 |
GenericCreateConstructor<AsyncIteratorConstructor, 0, |
| 1404 |
gc::AllocKind::FUNCTION>, |
| 1405 |
GenericCreatePrototype<AsyncIteratorObject>, |
| 1406 |
nullptr, |
| 1407 |
nullptr, |
| 1408 |
async_iterator_proto_methods_with_helpers, |
| 1409 |
nullptr, |
| 1410 |
nullptr, |
| 1411 |
}; |
| 1412 |
|
| 1413 |
const JSClass AsyncIteratorObject::class_ = { |
| 1414 |
"AsyncIterator", |
| 1415 |
JSCLASS_HAS_CACHED_PROTO(JSProto_AsyncIterator), |
| 1416 |
JS_NULL_CLASS_OPS, |
| 1417 |
&AsyncIteratorObjectClassSpec, |
| 1418 |
}; |
| 1419 |
|
| 1420 |
const JSClass AsyncIteratorObject::protoClass_ = { |
| 1421 |
"AsyncIterator.prototype", |
| 1422 |
JSCLASS_HAS_CACHED_PROTO(JSProto_AsyncIterator), |
| 1423 |
JS_NULL_CLASS_OPS, |
| 1424 |
&AsyncIteratorObjectClassSpec, |
| 1425 |
}; |
| 1426 |
|
| 1427 |
// Iterator Helper proposal |
| 1428 |
static const JSFunctionSpec async_iterator_helper_methods[] = { |
| 1429 |
JS_SELF_HOSTED_FN("next", "AsyncIteratorHelperNext", 1, 0), |
| 1430 |
JS_SELF_HOSTED_FN("return", "AsyncIteratorHelperReturn", 1, 0), |
| 1431 |
JS_SELF_HOSTED_FN("throw", "AsyncIteratorHelperThrow", 1, 0), |
| 1432 |
JS_FS_END, |
| 1433 |
}; |
| 1434 |
|
| 1435 |
static const JSClass AsyncIteratorHelperPrototypeClass = { |
| 1436 |
"Async Iterator Helper", |
| 1437 |
0, |
| 1438 |
}; |
| 1439 |
|
| 1440 |
const JSClass AsyncIteratorHelperObject::class_ = { |
| 1441 |
"Async Iterator Helper", |
| 1442 |
JSCLASS_HAS_RESERVED_SLOTS(AsyncIteratorHelperObject::SlotCount), |
| 1443 |
}; |
| 1444 |
|
| 1445 |
/* static */ |
| 1446 |
NativeObject* GlobalObject::getOrCreateAsyncIteratorHelperPrototype( |
| 1447 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 1448 |
return MaybeNativeObject( |
| 1449 |
getOrCreateBuiltinProto(cx, global, ProtoKind::AsyncIteratorHelperProto, |
| 1450 |
initAsyncIteratorHelperProto)); |
| 1451 |
} |
| 1452 |
|
| 1453 |
/* static */ |
| 1454 |
bool GlobalObject::initAsyncIteratorHelperProto(JSContext* cx, |
| 1455 |
Handle<GlobalObject*> global) { |
| 1456 |
if (global->hasBuiltinProto(ProtoKind::AsyncIteratorHelperProto)) { |
| 1457 |
return true; |
| 1458 |
} |
| 1459 |
|
| 1460 |
RootedObject asyncIterProto( |
| 1461 |
cx, GlobalObject::getOrCreateAsyncIteratorPrototype(cx, global)); |
| 1462 |
if (!asyncIterProto) { |
| 1463 |
return false; |
| 1464 |
} |
| 1465 |
|
| 1466 |
RootedObject asyncIteratorHelperProto( |
| 1467 |
cx, GlobalObject::createBlankPrototypeInheriting( |
| 1468 |
cx, &AsyncIteratorHelperPrototypeClass, asyncIterProto)); |
| 1469 |
if (!asyncIteratorHelperProto) { |
| 1470 |
return false; |
| 1471 |
} |
| 1472 |
if (!DefinePropertiesAndFunctions(cx, asyncIteratorHelperProto, nullptr, |
| 1473 |
async_iterator_helper_methods)) { |
| 1474 |
return false; |
| 1475 |
} |
| 1476 |
|
| 1477 |
global->initBuiltinProto(ProtoKind::AsyncIteratorHelperProto, |
| 1478 |
asyncIteratorHelperProto); |
| 1479 |
return true; |
| 1480 |
} |
| 1481 |
|
| 1482 |
AsyncIteratorHelperObject* js::NewAsyncIteratorHelper(JSContext* cx) { |
| 1483 |
RootedObject proto(cx, GlobalObject::getOrCreateAsyncIteratorHelperPrototype( |
| 1484 |
cx, cx->global())); |
| 1485 |
if (!proto) { |
| 1486 |
return nullptr; |
| 1487 |
} |
| 1488 |
return NewObjectWithGivenProto<AsyncIteratorHelperObject>(cx, proto); |
| 1489 |
} |