AsyncGenerator.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AsyncGenerator.h>
  8. #include <LibJS/Runtime/AsyncGeneratorPrototype.h>
  9. #include <LibJS/Runtime/AsyncGeneratorRequest.h>
  10. #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/PromiseConstructor.h>
  13. namespace JS {
  14. ThrowCompletionOr<NonnullGCPtr<AsyncGenerator>> AsyncGenerator::create(Realm& realm, Value initial_value, ECMAScriptFunctionObject* generating_function, ExecutionContext execution_context, Bytecode::CallFrame frame)
  15. {
  16. auto& vm = realm.vm();
  17. // This is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
  18. auto generating_function_prototype = TRY(generating_function->get(vm.names.prototype));
  19. auto generating_function_prototype_object = TRY(generating_function_prototype.to_object(vm));
  20. auto object = realm.heap().allocate<AsyncGenerator>(realm, realm, generating_function_prototype_object, move(execution_context));
  21. object->m_generating_function = generating_function;
  22. object->m_frame = move(frame);
  23. object->m_previous_value = initial_value;
  24. return object;
  25. }
  26. AsyncGenerator::AsyncGenerator(Realm&, Object& prototype, ExecutionContext context)
  27. : Object(ConstructWithPrototypeTag::Tag, prototype)
  28. , m_async_generator_context(move(context))
  29. {
  30. }
  31. void AsyncGenerator::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. for (auto const& request : m_async_generator_queue) {
  35. if (request.completion.value().has_value())
  36. visitor.visit(*request.completion.value());
  37. visitor.visit(request.capability);
  38. }
  39. m_async_generator_context.visit_edges(visitor);
  40. visitor.visit(m_generating_function);
  41. visitor.visit(m_previous_value);
  42. if (m_frame.has_value())
  43. m_frame->visit_edges(visitor);
  44. visitor.visit(m_current_promise);
  45. }
  46. // 27.6.3.4 AsyncGeneratorEnqueue ( generator, completion, promiseCapability ), https://tc39.es/ecma262/#sec-asyncgeneratorenqueue
  47. void AsyncGenerator::async_generator_enqueue(Completion completion, NonnullGCPtr<PromiseCapability> promise_capability)
  48. {
  49. // 1. Let request be AsyncGeneratorRequest { [[Completion]]: completion, [[Capability]]: promiseCapability }.
  50. auto request = AsyncGeneratorRequest { .completion = move(completion), .capability = promise_capability };
  51. // 2. Append request to generator.[[AsyncGeneratorQueue]].
  52. m_async_generator_queue.append(move(request));
  53. // 3. Return unused.
  54. }
  55. void AsyncGenerator::set_async_generator_state(Badge<AsyncGeneratorPrototype>, AsyncGenerator::State value)
  56. {
  57. m_async_generator_state = value;
  58. }
  59. // 27.7.5.3 Await ( value ), https://tc39.es/ecma262/#await
  60. ThrowCompletionOr<void> AsyncGenerator::await(Value value)
  61. {
  62. auto& vm = this->vm();
  63. auto& realm = *vm.current_realm();
  64. // 1. Let asyncContext be the running execution context.
  65. auto& async_context = vm.running_execution_context();
  66. // 2. Let promise be ? PromiseResolve(%Promise%, value).
  67. auto* promise_object = TRY(promise_resolve(vm, realm.intrinsics().promise_constructor(), value));
  68. // 3. Let fulfilledClosure be a new Abstract Closure with parameters (v) that captures asyncContext and performs the
  69. // following steps when called:
  70. auto fulfilled_closure = [this, &async_context](VM& vm) -> ThrowCompletionOr<Value> {
  71. auto value = vm.argument(0);
  72. // a. Let prevContext be the running execution context.
  73. auto& prev_context = vm.running_execution_context();
  74. // FIXME: b. Suspend prevContext.
  75. // c. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  76. TRY(vm.push_execution_context(async_context, {}));
  77. // d. Resume the suspended evaluation of asyncContext using NormalCompletion(v) as the result of the operation that
  78. // suspended it.
  79. execute(vm, normal_completion(value));
  80. // e. Assert: When we reach this step, asyncContext has already been removed from the execution context stack and
  81. // prevContext is the currently running execution context.
  82. VERIFY(&vm.running_execution_context() == &prev_context);
  83. // f. Return undefined.
  84. return js_undefined();
  85. };
  86. // 4. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 1, "", « »).
  87. auto on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 1, "");
  88. // 5. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures asyncContext and performs the
  89. // following steps when called:
  90. auto rejected_closure = [this, &async_context](VM& vm) -> ThrowCompletionOr<Value> {
  91. auto reason = vm.argument(0);
  92. // a. Let prevContext be the running execution context.
  93. auto& prev_context = vm.running_execution_context();
  94. // FIXME: b. Suspend prevContext.
  95. // c. Push asyncContext onto the execution context stack; asyncContext is now the running execution context.
  96. TRY(vm.push_execution_context(async_context, {}));
  97. // d. Resume the suspended evaluation of asyncContext using ThrowCompletion(reason) as the result of the operation that
  98. // suspended it.
  99. execute(vm, throw_completion(reason));
  100. // e. Assert: When we reach this step, asyncContext has already been removed from the execution context stack and
  101. // prevContext is the currently running execution context.
  102. VERIFY(&vm.running_execution_context() == &prev_context);
  103. // f. Return undefined.
  104. return js_undefined();
  105. };
  106. // 6. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
  107. auto on_rejected = NativeFunction::create(realm, move(rejected_closure), 1, "");
  108. // 7. Perform PerformPromiseThen(promise, onFulfilled, onRejected).
  109. m_current_promise = verify_cast<Promise>(promise_object);
  110. m_current_promise->perform_then(on_fulfilled, on_rejected, {});
  111. // 8. Remove asyncContext from the execution context stack and restore the execution context that is at the top of the
  112. // execution context stack as the running execution context.
  113. vm.pop_execution_context();
  114. // NOTE: None of these are necessary. 10-12 are handled by step d of the above lambdas.
  115. // 9. Let callerContext be the running execution context.
  116. // 10. Resume callerContext passing empty. If asyncContext is ever resumed again, let completion be the Completion Record with which it is resumed.
  117. // 11. Assert: If control reaches here, then asyncContext is the running execution context again.
  118. // 12. Return completion.
  119. return {};
  120. }
  121. void AsyncGenerator::execute(VM& vm, Completion completion)
  122. {
  123. while (true) {
  124. // Loosely based on step 4 of https://tc39.es/ecma262/#sec-asyncgeneratorstart
  125. VERIFY(completion.value().has_value());
  126. auto generated_value = [](Value value) -> Value {
  127. if (value.is_object())
  128. return value.as_object().get_without_side_effects("result");
  129. return value.is_empty() ? js_undefined() : value;
  130. };
  131. auto generated_continuation = [&](Value value) -> Bytecode::BasicBlock const* {
  132. if (value.is_object()) {
  133. auto number_value = value.as_object().get_without_side_effects("continuation");
  134. return reinterpret_cast<Bytecode::BasicBlock const*>(static_cast<u64>(number_value.as_double()));
  135. }
  136. return nullptr;
  137. };
  138. auto generated_is_await = [](Value value) -> bool {
  139. if (value.is_object())
  140. return value.as_object().get_without_side_effects("isAwait").as_bool();
  141. return false;
  142. };
  143. auto& realm = *vm.current_realm();
  144. auto completion_object = Object::create(realm, nullptr);
  145. completion_object->define_direct_property(vm.names.type, Value(to_underlying(completion.type())), default_attributes);
  146. completion_object->define_direct_property(vm.names.value, completion.value().value(), default_attributes);
  147. auto& bytecode_interpreter = vm.bytecode_interpreter();
  148. auto const* next_block = generated_continuation(m_previous_value);
  149. // We should never enter `execute` again after the generator is complete.
  150. VERIFY(next_block);
  151. VERIFY(!m_generating_function->bytecode_executable()->basic_blocks.find_if([next_block](auto& block) { return block == next_block; }).is_end());
  152. Bytecode::CallFrame* frame = nullptr;
  153. if (m_frame.has_value())
  154. frame = &m_frame.value();
  155. if (frame)
  156. frame->registers[0] = completion_object;
  157. else
  158. bytecode_interpreter.accumulator() = completion_object;
  159. auto next_result = bytecode_interpreter.run_and_return_frame(*m_generating_function->bytecode_executable(), next_block, frame);
  160. if (!m_frame.has_value())
  161. m_frame = move(*next_result.frame);
  162. auto result_value = move(next_result.value);
  163. if (!result_value.is_throw_completion()) {
  164. m_previous_value = result_value.release_value();
  165. auto value = generated_value(m_previous_value);
  166. bool is_await = generated_is_await(m_previous_value);
  167. if (is_await) {
  168. auto await_result = this->await(value);
  169. if (await_result.is_throw_completion()) {
  170. completion = await_result.release_error();
  171. continue;
  172. }
  173. return;
  174. }
  175. }
  176. bool done = result_value.is_throw_completion() || generated_continuation(m_previous_value) == nullptr;
  177. if (!done) {
  178. // 27.6.3.8 AsyncGeneratorYield ( value ), https://tc39.es/ecma262/#sec-asyncgeneratoryield
  179. // 1. Let genContext be the running execution context.
  180. // 2. Assert: genContext is the execution context of a generator.
  181. // 3. Let generator be the value of the Generator component of genContext.
  182. // 4. Assert: GetGeneratorKind() is async.
  183. // NOTE: genContext is `m_async_generator_context`, generator is `this`.
  184. // 5. Let completion be NormalCompletion(value).
  185. auto value = generated_value(m_previous_value);
  186. auto yield_completion = normal_completion(value);
  187. // 6. Assert: The execution context stack has at least two elements.
  188. VERIFY(vm.execution_context_stack().size() >= 2);
  189. // 7. Let previousContext be the second to top element of the execution context stack.
  190. auto& previous_context = vm.execution_context_stack().at(vm.execution_context_stack().size() - 2);
  191. // 8. Let previousRealm be previousContext's Realm.
  192. auto previous_realm = previous_context->realm;
  193. // 9. Perform AsyncGeneratorCompleteStep(generator, completion, false, previousRealm).
  194. complete_step(yield_completion, false, previous_realm.ptr());
  195. // 10. Let queue be generator.[[AsyncGeneratorQueue]].
  196. auto& queue = m_async_generator_queue;
  197. // 11. If queue is not empty, then
  198. if (!queue.is_empty()) {
  199. // a. NOTE: Execution continues without suspending the generator.
  200. // b. Let toYield be the first element of queue.
  201. auto& to_yield = queue.first();
  202. // c. Let resumptionValue be Completion(toYield.[[Completion]]).
  203. completion = Completion(to_yield.completion);
  204. // d. Return ? AsyncGeneratorUnwrapYieldResumption(resumptionValue).
  205. // NOTE: AsyncGeneratorUnwrapYieldResumption is performed inside the continuation block inside the generator,
  206. // so we just need to enter the generator again.
  207. continue;
  208. }
  209. // 12. Else,
  210. else {
  211. // a. Set generator.[[AsyncGeneratorState]] to suspendedYield.
  212. m_async_generator_state = State::SuspendedYield;
  213. // b. Remove genContext from the execution context stack and restore the execution context that is at the top of the
  214. // execution context stack as the running execution context.
  215. vm.pop_execution_context();
  216. // c. Let callerContext be the running execution context.
  217. // d. Resume callerContext passing undefined. If genContext is ever resumed again, let resumptionValue be the Completion Record with which it is resumed.
  218. // e. Assert: If control reaches here, then genContext is the running execution context again.
  219. // f. Return ? AsyncGeneratorUnwrapYieldResumption(resumptionValue).
  220. // NOTE: e-f are performed whenever someone calls `execute` again.
  221. return;
  222. }
  223. }
  224. // 27.6.3.2 AsyncGeneratorStart ( generator, generatorBody ), https://tc39.es/ecma262/#sec-asyncgeneratorstart
  225. // 4.e. Assert: If we return here, the async generator either threw an exception or performed either an implicit or explicit return.
  226. // 4.f. Remove acGenContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
  227. vm.pop_execution_context();
  228. // 4.g. Set acGenerator.[[AsyncGeneratorState]] to completed.
  229. m_async_generator_state = State::Completed;
  230. // 4.h. If result.[[Type]] is normal, set result to NormalCompletion(undefined).
  231. // 4.i. If result.[[Type]] is return, set result to NormalCompletion(result.[[Value]]).
  232. Completion result;
  233. if (!result_value.is_throw_completion()) {
  234. result = normal_completion(generated_value(m_previous_value));
  235. } else {
  236. result = result_value.release_error();
  237. }
  238. // 4.j. Perform AsyncGeneratorCompleteStep(acGenerator, result, true).
  239. complete_step(result, true);
  240. // 4.k. Perform AsyncGeneratorDrainQueue(acGenerator).
  241. drain_queue();
  242. // 4.l. Return undefined.
  243. return;
  244. }
  245. }
  246. // 27.6.3.6 AsyncGeneratorResume ( generator, completion ), https://tc39.es/ecma262/#sec-asyncgeneratorresume
  247. ThrowCompletionOr<void> AsyncGenerator::resume(VM& vm, Completion completion)
  248. {
  249. // 1. Assert: generator.[[AsyncGeneratorState]] is either suspendedStart or suspendedYield.
  250. VERIFY(m_async_generator_state == State::SuspendedStart || m_async_generator_state == State::SuspendedYield);
  251. // 2. Let genContext be generator.[[AsyncGeneratorContext]].
  252. auto& generator_context = m_async_generator_context;
  253. // 3. Let callerContext be the running execution context.
  254. auto const& caller_context = vm.running_execution_context();
  255. // FIXME: 4. Suspend callerContext.
  256. // 5. Set generator.[[AsyncGeneratorState]] to executing.
  257. m_async_generator_state = State::Executing;
  258. // 6. Push genContext onto the execution context stack; genContext is now the running execution context.
  259. TRY(vm.push_execution_context(generator_context, {}));
  260. // 7. Resume the suspended evaluation of genContext using completion as the result of the operation that suspended
  261. // it. Let result be the Completion Record returned by the resumed computation.
  262. // 8. Assert: result is never an abrupt completion.
  263. execute(vm, completion);
  264. // 9. Assert: When we return here, genContext has already been removed from the execution context stack and
  265. // callerContext is the currently running execution context.
  266. VERIFY(&vm.running_execution_context() == &caller_context);
  267. // 10. Return unused.
  268. return {};
  269. }
  270. // 27.6.3.9 AsyncGeneratorAwaitReturn ( generator ), https://tc39.es/ecma262/#sec-asyncgeneratorawaitreturn
  271. // With unmerged broken promise fixup from https://github.com/tc39/ecma262/pull/2683
  272. void AsyncGenerator::await_return()
  273. {
  274. auto& vm = this->vm();
  275. auto& realm = *vm.current_realm();
  276. // 1. Let queue be generator.[[AsyncGeneratorQueue]].
  277. auto& queue = m_async_generator_queue;
  278. // 2. Assert: queue is not empty.
  279. VERIFY(!queue.is_empty());
  280. // 3. Let next be the first element of queue.
  281. auto& next = m_async_generator_queue.first();
  282. // 4. Let completion be Completion(next.[[Completion]]).
  283. auto completion = Completion(next.completion);
  284. // 5. Assert: completion.[[Type]] is return.
  285. VERIFY(completion.type() == Completion::Type::Return);
  286. // 6. Let promiseCompletion be Completion(PromiseResolve(%Promise%, _completion_.[[Value]])).
  287. auto promise_completion = promise_resolve(vm, realm.intrinsics().promise_constructor(), completion.value().value());
  288. // 7. If promiseCompletion is an abrupt completion, then
  289. if (promise_completion.is_throw_completion()) {
  290. // a. Set generator.[[AsyncGeneratorState]] to completed.
  291. m_async_generator_state = State::Completed;
  292. // b. Perform AsyncGeneratorCompleteStep(generator, promiseCompletion, true).
  293. complete_step(promise_completion.release_error(), true);
  294. // c. Perform AsyncGeneratorDrainQueue(generator).
  295. drain_queue();
  296. // d. Return unused.
  297. return;
  298. }
  299. // 8. Assert: promiseCompletion.[[Type]] is normal.
  300. VERIFY(!promise_completion.is_throw_completion());
  301. // 9. Let promise be promiseCompletion.[[Value]].
  302. auto* promise = promise_completion.release_value();
  303. // 10. Let fulfilledClosure be a new Abstract Closure with parameters (value) that captures generator and performs
  304. // the following steps when called:
  305. auto fulfilled_closure = [this](VM& vm) -> ThrowCompletionOr<Value> {
  306. // a. Set generator.[[AsyncGeneratorState]] to completed.
  307. m_async_generator_state = State::Completed;
  308. // b. Let result be NormalCompletion(value).
  309. auto result = normal_completion(vm.argument(0));
  310. // c. Perform AsyncGeneratorCompleteStep(generator, result, true).
  311. complete_step(result, true);
  312. // d. Perform AsyncGeneratorDrainQueue(generator).
  313. drain_queue();
  314. // e. Return undefined.
  315. return js_undefined();
  316. };
  317. // 11. Let onFulfilled be CreateBuiltinFunction(fulfilledClosure, 1, "", « »).
  318. auto on_fulfilled = NativeFunction::create(realm, move(fulfilled_closure), 1, "");
  319. // 12. Let rejectedClosure be a new Abstract Closure with parameters (reason) that captures generator and performs
  320. // the following steps when called:
  321. auto rejected_closure = [this](VM& vm) -> ThrowCompletionOr<Value> {
  322. // a. Set generator.[[AsyncGeneratorState]] to completed.
  323. m_async_generator_state = State::Completed;
  324. // b. Let result be ThrowCompletion(reason).
  325. auto result = throw_completion(vm.argument(0));
  326. // c. Perform AsyncGeneratorCompleteStep(generator, result, true).
  327. complete_step(result, true);
  328. // d. Perform AsyncGeneratorDrainQueue(generator).
  329. drain_queue();
  330. // e. Return undefined.
  331. return js_undefined();
  332. };
  333. // 13. Let onRejected be CreateBuiltinFunction(rejectedClosure, 1, "", « »).
  334. auto on_rejected = NativeFunction::create(realm, move(rejected_closure), 1, "");
  335. // 14. Perform PerformPromiseThen(promise, onFulfilled, onRejected).
  336. // NOTE: await_return should only be called when the generator is in SuspendedStart or Completed state,
  337. // so an await shouldn't be running currently, so it should be safe to overwrite m_current_promise.
  338. m_current_promise = verify_cast<Promise>(promise);
  339. m_current_promise->perform_then(on_fulfilled, on_rejected, {});
  340. // 15. Return unused.
  341. return;
  342. }
  343. // 27.6.3.5 AsyncGeneratorCompleteStep ( generator, completion, done [ , realm ] ), https://tc39.es/ecma262/#sec-asyncgeneratorcompletestep
  344. void AsyncGenerator::complete_step(Completion completion, bool done, Realm* realm)
  345. {
  346. auto& vm = this->vm();
  347. // 1. Assert: generator.[[AsyncGeneratorQueue]] is not empty.
  348. VERIFY(!m_async_generator_queue.is_empty());
  349. // 2. Let next be the first element of generator.[[AsyncGeneratorQueue]].
  350. // 3. Remove the first element from generator.[[AsyncGeneratorQueue]].
  351. auto next = m_async_generator_queue.take_first();
  352. // 4. Let promiseCapability be next.[[Capability]].
  353. auto promise_capability = next.capability;
  354. // 5. Let value be completion.[[Value]].
  355. auto value = completion.value().value();
  356. // 6. If completion.[[Type]] is throw, then
  357. if (completion.type() == Completion::Type::Throw) {
  358. // a. Perform ! Call(promiseCapability.[[Reject]], undefined, « value »).
  359. MUST(call(vm, *promise_capability->reject(), js_undefined(), value));
  360. }
  361. // 7. Else,
  362. else {
  363. // a. Assert: completion.[[Type]] is normal.
  364. VERIFY(completion.type() == Completion::Type::Normal);
  365. GCPtr<Object> iterator_result;
  366. // b. If realm is present, then
  367. if (realm) {
  368. // i. Let oldRealm be the running execution context's Realm.
  369. auto old_realm = vm.running_execution_context().realm;
  370. // ii. Set the running execution context's Realm to realm.
  371. vm.running_execution_context().realm = realm;
  372. // iii. Let iteratorResult be CreateIterResultObject(value, done).
  373. iterator_result = create_iterator_result_object(vm, value, done);
  374. // iv. Set the running execution context's Realm to oldRealm.
  375. vm.running_execution_context().realm = old_realm;
  376. }
  377. // c. Else,
  378. else {
  379. // i. Let iteratorResult be CreateIterResultObject(value, done).
  380. iterator_result = create_iterator_result_object(vm, value, done);
  381. }
  382. VERIFY(iterator_result);
  383. // d. Perform ! Call(promiseCapability.[[Resolve]], undefined, « iteratorResult »).
  384. MUST(call(vm, *promise_capability->resolve(), js_undefined(), iterator_result));
  385. }
  386. // 8. Return unused.
  387. }
  388. // 27.6.3.10 AsyncGeneratorDrainQueue ( generator ), https://tc39.es/ecma262/#sec-asyncgeneratordrainqueue
  389. void AsyncGenerator::drain_queue()
  390. {
  391. // 1. Assert: generator.[[AsyncGeneratorState]] is completed.
  392. VERIFY(m_async_generator_state == State::Completed);
  393. // 2. Let queue be generator.[[AsyncGeneratorQueue]].
  394. auto& queue = m_async_generator_queue;
  395. // 3. If queue is empty, return unused.
  396. if (queue.is_empty())
  397. return;
  398. // 4. Let done be false.
  399. bool done = false;
  400. // 5. Repeat, while done is false,
  401. while (!done) {
  402. // a. Let next be the first element of queue.
  403. auto& next = m_async_generator_queue.first();
  404. // b. Let completion be Completion(next.[[Completion]]).
  405. auto completion = Completion(next.completion);
  406. // c. If completion.[[Type]] is return, then
  407. if (completion.type() == Completion::Type::Return) {
  408. // i. Set generator.[[AsyncGeneratorState]] to awaiting-return.
  409. m_async_generator_state = State::AwaitingReturn;
  410. // ii. Perform AsyncGeneratorAwaitReturn(generator).
  411. await_return();
  412. // iii. Set done to true.
  413. done = true;
  414. }
  415. // d. Else,
  416. else {
  417. // i. If completion.[[Type]] is normal, then
  418. if (completion.type() == Completion::Type::Normal) {
  419. // 1. Set completion to NormalCompletion(undefined).
  420. completion = normal_completion(js_undefined());
  421. }
  422. // ii. Perform AsyncGeneratorCompleteStep(generator, completion, true).
  423. complete_step(completion, true);
  424. // iii. If queue is empty, set done to true.
  425. if (queue.is_empty())
  426. done = true;
  427. }
  428. }
  429. // 6. Return unused.
  430. }
  431. }