13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "functionBuilder.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include <binder/binder.h>
193af6ab5fSopenharmony_ci#include <compiler/base/iterators.h>
203af6ab5fSopenharmony_ci#include <compiler/core/pandagen.h>
213af6ab5fSopenharmony_ci#include <ir/base/classDefinition.h>
223af6ab5fSopenharmony_ci#include <ir/base/scriptFunction.h>
233af6ab5fSopenharmony_ci#include <ir/statement.h>
243af6ab5fSopenharmony_ci#include <util/helpers.h>
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_cinamespace panda::es2panda::compiler {
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_ciFunctionBuilder::FunctionBuilder(PandaGen *pg, CatchTable *catchTable)
293af6ab5fSopenharmony_ci    : pg_(pg), catchTable_(catchTable), funcObj_(pg_->AllocReg())
303af6ab5fSopenharmony_ci{
313af6ab5fSopenharmony_ci}
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ciIteratorType FunctionBuilder::GeneratorKind() const
343af6ab5fSopenharmony_ci{
353af6ab5fSopenharmony_ci    return IteratorType::SYNC;
363af6ab5fSopenharmony_ci}
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_civoid FunctionBuilder::DirectReturn(const ir::AstNode *node) const
393af6ab5fSopenharmony_ci{
403af6ab5fSopenharmony_ci    pg_->NotifyConcurrentResult(node);
413af6ab5fSopenharmony_ci    pg_->EmitReturn(node);
423af6ab5fSopenharmony_ci}
433af6ab5fSopenharmony_ci
443af6ab5fSopenharmony_civoid FunctionBuilder::ImplicitReturn(const ir::AstNode *node) const
453af6ab5fSopenharmony_ci{
463af6ab5fSopenharmony_ci    const auto *rootNode = pg_->RootNode();
473af6ab5fSopenharmony_ci    pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION);
483af6ab5fSopenharmony_ci    if (!rootNode->IsScriptFunction() || !rootNode->AsScriptFunction()->IsConstructor()) {
493af6ab5fSopenharmony_ci        if (pg_->isDebuggerEvaluateExpressionMode() && rootNode->IsProgram()) {
503af6ab5fSopenharmony_ci            pg_->NotifyConcurrentResult(node);
513af6ab5fSopenharmony_ci            pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION);
523af6ab5fSopenharmony_ci            pg_->EmitReturn(node);
533af6ab5fSopenharmony_ci            return;
543af6ab5fSopenharmony_ci        }
553af6ab5fSopenharmony_ci        pg_->LoadConst(node, Constant::JS_UNDEFINED);
563af6ab5fSopenharmony_ci        pg_->NotifyConcurrentResult(node);
573af6ab5fSopenharmony_ci        pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION);
583af6ab5fSopenharmony_ci        pg_->EmitReturnUndefined(node);
593af6ab5fSopenharmony_ci        return;
603af6ab5fSopenharmony_ci    }
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ci    pg_->GetThis(node);
633af6ab5fSopenharmony_ci    if (rootNode->AsScriptFunction()->IsConstructor() &&
643af6ab5fSopenharmony_ci        util::Helpers::GetClassDefiniton(rootNode->AsScriptFunction())->Super()) {
653af6ab5fSopenharmony_ci            pg_->ThrowIfSuperNotCorrectCall(node, 0);
663af6ab5fSopenharmony_ci    }
673af6ab5fSopenharmony_ci
683af6ab5fSopenharmony_ci    pg_->NotifyConcurrentResult(node);
693af6ab5fSopenharmony_ci    pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION);
703af6ab5fSopenharmony_ci    pg_->EmitReturn(node);
713af6ab5fSopenharmony_ci}
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_civoid FunctionBuilder::ExplicitReturn(const ir::AstNode *node) const
743af6ab5fSopenharmony_ci{
753af6ab5fSopenharmony_ci    DirectReturn(node);
763af6ab5fSopenharmony_ci}
773af6ab5fSopenharmony_ci
783af6ab5fSopenharmony_civoid FunctionBuilder::AsyncYield(const ir::AstNode *node, VReg value, VReg completionType, VReg completionValue) const
793af6ab5fSopenharmony_ci{
803af6ab5fSopenharmony_ci    ASSERT(BuilderKind() == BuilderType::ASYNC_GENERATOR);
813af6ab5fSopenharmony_ci    RegScope rs(pg_);
823af6ab5fSopenharmony_ci    VReg done = pg_->AllocReg();
833af6ab5fSopenharmony_ci    // 27.6.3.8.6 Set generator.[[AsyncGeneratorState]] to suspendedYield.
843af6ab5fSopenharmony_ci    pg_->GeneratorYield(node, funcObj_);
853af6ab5fSopenharmony_ci    /** 27.6.3.8.7 Remove genContext from the execution context stack and restore the execution context that
863af6ab5fSopenharmony_ci     *  is at the top of the execution context stack as the running execution context.
873af6ab5fSopenharmony_ci     *  27.6.3.8.9 Return ! AsyncGeneratorResolve(generator, value, false).
883af6ab5fSopenharmony_ci     */
893af6ab5fSopenharmony_ci    pg_->StoreConst(node, done, Constant::JS_FALSE);
903af6ab5fSopenharmony_ci    pg_->AsyncGeneratorResolve(node, funcObj_, value, done);
913af6ab5fSopenharmony_ci
923af6ab5fSopenharmony_ci    resumeGenerator(node, completionType, completionValue);
933af6ab5fSopenharmony_ci}
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_civoid FunctionBuilder::SuspendResumeExecution(const ir::AstNode *node, VReg completionType, VReg completionValue) const
963af6ab5fSopenharmony_ci{
973af6ab5fSopenharmony_ci    ASSERT(BuilderKind() == BuilderType::ASYNC || BuilderKind() == BuilderType::ASYNC_GENERATOR ||
983af6ab5fSopenharmony_ci           BuilderKind() == BuilderType::GENERATOR);
993af6ab5fSopenharmony_ci
1003af6ab5fSopenharmony_ci    pg_->SuspendGenerator(node, funcObj_); // iterResult is in acc
1013af6ab5fSopenharmony_ci    resumeGenerator(node, completionType, completionValue);
1023af6ab5fSopenharmony_ci}
1033af6ab5fSopenharmony_ci
1043af6ab5fSopenharmony_civoid FunctionBuilder::resumeGenerator(const ir::AstNode *node, VReg completionType, VReg completionValue) const
1053af6ab5fSopenharmony_ci{
1063af6ab5fSopenharmony_ci    ASSERT(BuilderKind() == BuilderType::ASYNC || BuilderKind() == BuilderType::ASYNC_GENERATOR ||
1073af6ab5fSopenharmony_ci           BuilderKind() == BuilderType::GENERATOR);
1083af6ab5fSopenharmony_ci
1093af6ab5fSopenharmony_ci    pg_->ResumeGenerator(node, funcObj_);
1103af6ab5fSopenharmony_ci    pg_->StoreAccumulator(node, completionValue);
1113af6ab5fSopenharmony_ci    pg_->GetResumeMode(node, funcObj_);
1123af6ab5fSopenharmony_ci    pg_->StoreAccumulator(node, completionType);
1133af6ab5fSopenharmony_ci}
1143af6ab5fSopenharmony_ci
1153af6ab5fSopenharmony_ciVReg FunctionBuilder::FunctionReg(const ir::ScriptFunction *node) const
1163af6ab5fSopenharmony_ci{
1173af6ab5fSopenharmony_ci    binder::FunctionScope *scope = node->Scope();
1183af6ab5fSopenharmony_ci    auto res = scope->Find(binder::Binder::MANDATORY_PARAM_FUNC);
1193af6ab5fSopenharmony_ci    ASSERT(res.level == 0 && res.variable->IsLocalVariable());
1203af6ab5fSopenharmony_ci    return res.variable->AsLocalVariable()->Vreg();
1213af6ab5fSopenharmony_ci}
1223af6ab5fSopenharmony_ci
1233af6ab5fSopenharmony_civoid FunctionBuilder::Await(const ir::AstNode *node)
1243af6ab5fSopenharmony_ci{
1253af6ab5fSopenharmony_ci    if (BuilderKind() == BuilderType::NORMAL) {
1263af6ab5fSopenharmony_ci        PandaGen::Unimplemented();
1273af6ab5fSopenharmony_ci    }
1283af6ab5fSopenharmony_ci
1293af6ab5fSopenharmony_ci    ASSERT(BuilderKind() == BuilderType::ASYNC || BuilderKind() == BuilderType::ASYNC_GENERATOR);
1303af6ab5fSopenharmony_ci
1313af6ab5fSopenharmony_ci    RegScope rs(pg_);
1323af6ab5fSopenharmony_ci    VReg completionType = pg_->AllocReg();
1333af6ab5fSopenharmony_ci    VReg completionValue = pg_->AllocReg();
1343af6ab5fSopenharmony_ci
1353af6ab5fSopenharmony_ci    pg_->AsyncFunctionAwait(node, funcObj_);
1363af6ab5fSopenharmony_ci    SuspendResumeExecution(node, completionType, completionValue);
1373af6ab5fSopenharmony_ci
1383af6ab5fSopenharmony_ci    HandleCompletion(node, completionType, completionValue);
1393af6ab5fSopenharmony_ci}
1403af6ab5fSopenharmony_ci
1413af6ab5fSopenharmony_civoid FunctionBuilder::HandleCompletion(const ir::AstNode *node, VReg completionType, VReg completionValue)
1423af6ab5fSopenharmony_ci{
1433af6ab5fSopenharmony_ci    if (BuilderKind() == BuilderType::GENERATOR) {
1443af6ab5fSopenharmony_ci        // .return(value)
1453af6ab5fSopenharmony_ci        pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN));
1463af6ab5fSopenharmony_ci
1473af6ab5fSopenharmony_ci        auto *notRetLabel = pg_->AllocLabel();
1483af6ab5fSopenharmony_ci        pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notRetLabel);
1493af6ab5fSopenharmony_ci        if (!handleReturn_) {
1503af6ab5fSopenharmony_ci            handleReturn_ = true;
1513af6ab5fSopenharmony_ci            pg_->ControlFlowChangeReturn();
1523af6ab5fSopenharmony_ci            handleReturn_ = false;
1533af6ab5fSopenharmony_ci        }
1543af6ab5fSopenharmony_ci
1553af6ab5fSopenharmony_ci        pg_->LoadAccumulator(node, completionValue);
1563af6ab5fSopenharmony_ci        pg_->DirectReturn(node);
1573af6ab5fSopenharmony_ci
1583af6ab5fSopenharmony_ci        // .throw(value)
1593af6ab5fSopenharmony_ci        pg_->SetLabel(node, notRetLabel);
1603af6ab5fSopenharmony_ci    }
1613af6ab5fSopenharmony_ci
1623af6ab5fSopenharmony_ci    pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
1633af6ab5fSopenharmony_ci
1643af6ab5fSopenharmony_ci    auto *not_throw_label = pg_->AllocLabel();
1653af6ab5fSopenharmony_ci    pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, not_throw_label);
1663af6ab5fSopenharmony_ci    pg_->LoadAccumulator(node, completionValue);
1673af6ab5fSopenharmony_ci    pg_->EmitThrow(node);
1683af6ab5fSopenharmony_ci
1693af6ab5fSopenharmony_ci    // .next(value)
1703af6ab5fSopenharmony_ci    pg_->SetLabel(node, not_throw_label);
1713af6ab5fSopenharmony_ci    pg_->LoadAccumulator(node, completionValue);
1723af6ab5fSopenharmony_ci}
1733af6ab5fSopenharmony_ci
1743af6ab5fSopenharmony_civoid FunctionBuilder::YieldStar(const ir::AstNode *node)
1753af6ab5fSopenharmony_ci{
1763af6ab5fSopenharmony_ci    ASSERT(BuilderKind() == BuilderType::GENERATOR || BuilderKind() == BuilderType::ASYNC_GENERATOR);
1773af6ab5fSopenharmony_ci
1783af6ab5fSopenharmony_ci    RegScope rs(pg_);
1793af6ab5fSopenharmony_ci
1803af6ab5fSopenharmony_ci    auto *loopStart = pg_->AllocLabel();
1813af6ab5fSopenharmony_ci    auto *returnCompletion = pg_->AllocLabel();
1823af6ab5fSopenharmony_ci    auto *throwCompletion = pg_->AllocLabel();
1833af6ab5fSopenharmony_ci    auto *callMethod = pg_->AllocLabel();
1843af6ab5fSopenharmony_ci    auto *normalOrThrowCompletion = pg_->AllocLabel();
1853af6ab5fSopenharmony_ci    auto *iteratorComplete = pg_->AllocLabel();
1863af6ab5fSopenharmony_ci
1873af6ab5fSopenharmony_ci    // 4. Let iteratorRecord be ? GetIterator(value, generatorKind).
1883af6ab5fSopenharmony_ci    Iterator iterator(pg_, node, GeneratorKind());
1893af6ab5fSopenharmony_ci
1903af6ab5fSopenharmony_ci    // 6. Let received be NormalCompletion(undefined).
1913af6ab5fSopenharmony_ci    VReg receivedValue = iterator.NextResult();
1923af6ab5fSopenharmony_ci    VReg receivedType = pg_->AllocReg();
1933af6ab5fSopenharmony_ci    VReg nextMethod = pg_->AllocReg();
1943af6ab5fSopenharmony_ci    VReg exitReturn = pg_->AllocReg();
1953af6ab5fSopenharmony_ci    VReg iterValue = pg_->AllocReg();
1963af6ab5fSopenharmony_ci
1973af6ab5fSopenharmony_ci    pg_->StoreConst(node, receivedValue, Constant::JS_UNDEFINED);
1983af6ab5fSopenharmony_ci    pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::NEXT));
1993af6ab5fSopenharmony_ci    pg_->StoreAccumulator(node, receivedType);
2003af6ab5fSopenharmony_ci    pg_->MoveVreg(node, nextMethod, iterator.Method());
2013af6ab5fSopenharmony_ci
2023af6ab5fSopenharmony_ci    // 7. Repeat
2033af6ab5fSopenharmony_ci    pg_->SetLabel(node, loopStart);
2043af6ab5fSopenharmony_ci    pg_->StoreConst(node, exitReturn, Constant::JS_FALSE);
2053af6ab5fSopenharmony_ci
2063af6ab5fSopenharmony_ci    // a. If received.[[Type]] is normal, then
2073af6ab5fSopenharmony_ci    pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::NEXT));
2083af6ab5fSopenharmony_ci    pg_->Condition(node, lexer::TokenType::PUNCTUATOR_STRICT_EQUAL, receivedType, throwCompletion);
2093af6ab5fSopenharmony_ci    pg_->MoveVreg(node, iterator.Method(), nextMethod);
2103af6ab5fSopenharmony_ci    pg_->Branch(node, callMethod);
2113af6ab5fSopenharmony_ci
2123af6ab5fSopenharmony_ci    // b. Else if received.[[Type]] is throw, then
2133af6ab5fSopenharmony_ci    pg_->SetLabel(node, throwCompletion);
2143af6ab5fSopenharmony_ci    pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
2153af6ab5fSopenharmony_ci    pg_->Condition(node, lexer::TokenType::PUNCTUATOR_STRICT_EQUAL, receivedType, returnCompletion);
2163af6ab5fSopenharmony_ci
2173af6ab5fSopenharmony_ci    // i. Let throw be ? GetMethod(iterator, "throw").
2183af6ab5fSopenharmony_ci    iterator.GetMethod("throw");
2193af6ab5fSopenharmony_ci
2203af6ab5fSopenharmony_ci    // ii. If throw is not undefined, then
2213af6ab5fSopenharmony_ci    pg_->BranchIfNotUndefined(node, callMethod);
2223af6ab5fSopenharmony_ci
2233af6ab5fSopenharmony_ci    // iii. Else,
2243af6ab5fSopenharmony_ci    // 1. NOTE: If iterator does not have a throw method, this throw is going to terminate the yield* loop. But first we
2253af6ab5fSopenharmony_ci    // need to give iterator a chance to clean up.
2263af6ab5fSopenharmony_ci    // 2. Let closeCompletion be Completion { [[Type]]: normal, [[Value]]: empty, [[Target]]: empty }.
2273af6ab5fSopenharmony_ci    // 3. If generatorKind is async, perform ? AsyncIteratorClose(iteratorRecord, closeCompletion).
2283af6ab5fSopenharmony_ci    // 4. Else, perform ? IteratorClose(iteratorRecord, closeCompletion).
2293af6ab5fSopenharmony_ci    iterator.Close(false);
2303af6ab5fSopenharmony_ci    // 5. NOTE: The next step throws a TypeError to indicate that there was a yield* protocol violation: iterator does
2313af6ab5fSopenharmony_ci    // not have a throw method.
2323af6ab5fSopenharmony_ci    // 6. Throw a TypeError exception.
2333af6ab5fSopenharmony_ci    pg_->ThrowThrowNotExist(node);
2343af6ab5fSopenharmony_ci
2353af6ab5fSopenharmony_ci    // c. Else,
2363af6ab5fSopenharmony_ci    // i. Assert: received.[[Type]] is return.
2373af6ab5fSopenharmony_ci    pg_->SetLabel(node, returnCompletion);
2383af6ab5fSopenharmony_ci    pg_->StoreConst(node, exitReturn, Constant::JS_TRUE);
2393af6ab5fSopenharmony_ci    // ii. Let return be ? GetMethod(iterator, "return").
2403af6ab5fSopenharmony_ci    iterator.GetMethod("return");
2413af6ab5fSopenharmony_ci
2423af6ab5fSopenharmony_ci    // iii. If return is undefined, then
2433af6ab5fSopenharmony_ci    pg_->BranchIfNotUndefined(node, callMethod);
2443af6ab5fSopenharmony_ci
2453af6ab5fSopenharmony_ci    // 1. If generatorKind is async, set received.[[Value]] to ? Await(received.[[Value]]).
2463af6ab5fSopenharmony_ci    pg_->ControlFlowChangeReturn();
2473af6ab5fSopenharmony_ci    pg_->LoadAccumulator(node, receivedValue);
2483af6ab5fSopenharmony_ci
2493af6ab5fSopenharmony_ci    if (GeneratorKind() == IteratorType::ASYNC) {
2503af6ab5fSopenharmony_ci        Await(node);
2513af6ab5fSopenharmony_ci    }
2523af6ab5fSopenharmony_ci
2533af6ab5fSopenharmony_ci    // 2. Return Completion(received).
2543af6ab5fSopenharmony_ci    pg_->DirectReturn(node);
2553af6ab5fSopenharmony_ci
2563af6ab5fSopenharmony_ci    pg_->SetLabel(node, callMethod);
2573af6ab5fSopenharmony_ci    // i. Let innerResult be ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « received.[[Value]] »).
2583af6ab5fSopenharmony_ci    // 1. Let innerResult be ? Call(throw, iterator, « received.[[Value]] »).
2593af6ab5fSopenharmony_ci    // iv. Let innerReturnResult be ? Call(return, iterator, « received.[[Value]] »).
2603af6ab5fSopenharmony_ci    iterator.CallMethodWithValue();
2613af6ab5fSopenharmony_ci
2623af6ab5fSopenharmony_ci    // ii. ii. If generatorKind is async, set innerResult to ? Await(innerResult).
2633af6ab5fSopenharmony_ci    // 2. If generatorKind is async, set innerResult to ? Await(innerResult).
2643af6ab5fSopenharmony_ci    // v. If generatorKind is async, set innerReturnResult to ? Await(innerReturnResult).
2653af6ab5fSopenharmony_ci    if (GeneratorKind() == IteratorType::ASYNC) {
2663af6ab5fSopenharmony_ci        Await(node);
2673af6ab5fSopenharmony_ci    }
2683af6ab5fSopenharmony_ci
2693af6ab5fSopenharmony_ci    pg_->StoreAccumulator(node, receivedValue);
2703af6ab5fSopenharmony_ci
2713af6ab5fSopenharmony_ci    // ii. If Type(innerResult) is not Object, throw a TypeError exception.
2723af6ab5fSopenharmony_ci    // 4. If Type(innerResult) is not Object, throw a TypeError exception.
2733af6ab5fSopenharmony_ci    // vi. If Type(innerReturnResult) is not Object, throw a TypeError exception.
2743af6ab5fSopenharmony_ci    pg_->ThrowIfNotObject(node, receivedValue);
2753af6ab5fSopenharmony_ci
2763af6ab5fSopenharmony_ci    // iv. Let done be ? IteratorComplete(innerResult).
2773af6ab5fSopenharmony_ci    // v. Let done be ? IteratorComplete(innerResult).
2783af6ab5fSopenharmony_ci    // vii. Let done be ? IteratorComplete(innerReturnResult).
2793af6ab5fSopenharmony_ci    iterator.Complete();
2803af6ab5fSopenharmony_ci    pg_->BranchIfTrue(node, iteratorComplete);
2813af6ab5fSopenharmony_ci
2823af6ab5fSopenharmony_ci    pg_->LoadAccumulator(node, receivedValue);
2833af6ab5fSopenharmony_ci    // vi. If generatorKind is async, set received to AsyncGeneratorYield(? IteratorValue(innerResult)).
2843af6ab5fSopenharmony_ci    // 7. If generatorKind is async, set received to AsyncGeneratorYield(? IteratorValue(innerResult)).
2853af6ab5fSopenharmony_ci    // ix. If generatorKind is async, set received to AsyncGeneratorYield(? IteratorValue(innerReturnResult)).
2863af6ab5fSopenharmony_ci    if (GeneratorKind() == IteratorType::ASYNC) {
2873af6ab5fSopenharmony_ci        iterator.Value();
2883af6ab5fSopenharmony_ci        // 27.6.3.8 AsyncGeneratorYield
2893af6ab5fSopenharmony_ci        // 5. Set value to ? Await(value).
2903af6ab5fSopenharmony_ci        Await(node);
2913af6ab5fSopenharmony_ci        // 6. Set generator.[[AsyncGeneratorState]] to suspendedYield.
2923af6ab5fSopenharmony_ci        pg_->StoreAccumulator(node, iterValue);
2933af6ab5fSopenharmony_ci        AsyncYield(node, iterValue, receivedType, receivedValue);
2943af6ab5fSopenharmony_ci
2953af6ab5fSopenharmony_ci        // a. If resumptionValue.[[Type]] is not return
2963af6ab5fSopenharmony_ci        pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN));
2973af6ab5fSopenharmony_ci        pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, receivedType, loopStart);
2983af6ab5fSopenharmony_ci
2993af6ab5fSopenharmony_ci        // b. Let awaited be Await(resumptionValue.[[Value]]).
3003af6ab5fSopenharmony_ci        pg_->LoadAccumulator(node, receivedValue);
3013af6ab5fSopenharmony_ci        pg_->AsyncFunctionAwait(node, funcObj_);
3023af6ab5fSopenharmony_ci        SuspendResumeExecution(node, receivedType, receivedValue);
3033af6ab5fSopenharmony_ci
3043af6ab5fSopenharmony_ci        // c. If awaited.[[Type]] is throw, return Completion(awaited).
3053af6ab5fSopenharmony_ci        pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
3063af6ab5fSopenharmony_ci        // d. Assert: awaited.[[Type]] is normal.
3073af6ab5fSopenharmony_ci        // e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }.
3083af6ab5fSopenharmony_ci        pg_->Condition(node, lexer::TokenType::PUNCTUATOR_NOT_EQUAL, receivedType, loopStart);
3093af6ab5fSopenharmony_ci        pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN));
3103af6ab5fSopenharmony_ci        pg_->StoreAccumulator(node, receivedType);
3113af6ab5fSopenharmony_ci    } else {
3123af6ab5fSopenharmony_ci        // vii. Else, set received to GeneratorYield(innerResult).
3133af6ab5fSopenharmony_ci        // 8. Else, set received to GeneratorYield(innerResult).
3143af6ab5fSopenharmony_ci        // x. Else, set received to GeneratorYield(innerReturnResult).
3153af6ab5fSopenharmony_ci        SuspendResumeExecution(node, receivedType, receivedValue);
3163af6ab5fSopenharmony_ci    }
3173af6ab5fSopenharmony_ci
3183af6ab5fSopenharmony_ci    pg_->Branch(node, loopStart);
3193af6ab5fSopenharmony_ci
3203af6ab5fSopenharmony_ci    // v. If done is true, then
3213af6ab5fSopenharmony_ci    // 6. If done is true, then
3223af6ab5fSopenharmony_ci    // viii. If done is true, then
3233af6ab5fSopenharmony_ci    pg_->SetLabel(node, iteratorComplete);
3243af6ab5fSopenharmony_ci
3253af6ab5fSopenharmony_ci    pg_->LoadAccumulator(node, exitReturn);
3263af6ab5fSopenharmony_ci    pg_->BranchIfFalse(node, normalOrThrowCompletion);
3273af6ab5fSopenharmony_ci
3283af6ab5fSopenharmony_ci    // 1. Let value be ? IteratorValue(innerReturnResult).
3293af6ab5fSopenharmony_ci    iterator.Value();
3303af6ab5fSopenharmony_ci
3313af6ab5fSopenharmony_ci    if (pg_->CheckControlFlowChange()) {
3323af6ab5fSopenharmony_ci        pg_->StoreAccumulator(node, receivedValue);
3333af6ab5fSopenharmony_ci        pg_->ControlFlowChangeReturn();
3343af6ab5fSopenharmony_ci        pg_->LoadAccumulator(node, receivedValue);
3353af6ab5fSopenharmony_ci    }
3363af6ab5fSopenharmony_ci
3373af6ab5fSopenharmony_ci    // 2. Return Completion { [[Type]]: return, [[Value]]: value, [[Target]]: empty }.
3383af6ab5fSopenharmony_ci    pg_->DirectReturn(node);
3393af6ab5fSopenharmony_ci
3403af6ab5fSopenharmony_ci    pg_->SetLabel(node, normalOrThrowCompletion);
3413af6ab5fSopenharmony_ci    // 1. Return ? IteratorValue(innerResult).
3423af6ab5fSopenharmony_ci    // a. Return ? IteratorValue(innerResult).
3433af6ab5fSopenharmony_ci    iterator.Value();
3443af6ab5fSopenharmony_ci}
3453af6ab5fSopenharmony_ci
3463af6ab5fSopenharmony_ci}  // namespace panda::es2panda::compiler
347