13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 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 "asyncGeneratorFunctionBuilder.h" 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci#include "compiler/base/catchTable.h" 193af6ab5fSopenharmony_ci#include "compiler/core/pandagen.h" 203af6ab5fSopenharmony_ci#include "ir/base/scriptFunction.h" 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_cinamespace ark::es2panda::compiler { 233af6ab5fSopenharmony_civoid AsyncGeneratorFunctionBuilder::Prepare(const ir::ScriptFunction *node) const 243af6ab5fSopenharmony_ci{ 253af6ab5fSopenharmony_ci VReg callee = FunctionReg(node); 263af6ab5fSopenharmony_ci 273af6ab5fSopenharmony_ci pg_->CreateAsyncGeneratorObj(node, callee); 283af6ab5fSopenharmony_ci pg_->StoreAccumulator(node, funcObj_); 293af6ab5fSopenharmony_ci pg_->SuspendGenerator(node, funcObj_); 303af6ab5fSopenharmony_ci pg_->SetLabel(node, catchTable_->LabelSet().TryBegin()); 313af6ab5fSopenharmony_ci} 323af6ab5fSopenharmony_ci 333af6ab5fSopenharmony_civoid AsyncGeneratorFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const 343af6ab5fSopenharmony_ci{ 353af6ab5fSopenharmony_ci const auto &labelSet = catchTable_->LabelSet(); 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci pg_->SetLabel(node, labelSet.TryEnd()); 383af6ab5fSopenharmony_ci pg_->SetLabel(node, labelSet.CatchBegin()); 393af6ab5fSopenharmony_ci pg_->AsyncGeneratorReject(node, funcObj_); 403af6ab5fSopenharmony_ci pg_->EmitReturn(node); 413af6ab5fSopenharmony_ci pg_->SetLabel(node, labelSet.CatchEnd()); 423af6ab5fSopenharmony_ci} 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_civoid AsyncGeneratorFunctionBuilder::DirectReturn(const ir::AstNode *node) const 453af6ab5fSopenharmony_ci{ 463af6ab5fSopenharmony_ci pg_->AsyncGeneratorResolve(node, funcObj_); 473af6ab5fSopenharmony_ci pg_->EmitReturn(node); 483af6ab5fSopenharmony_ci} 493af6ab5fSopenharmony_ci 503af6ab5fSopenharmony_civoid AsyncGeneratorFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const 513af6ab5fSopenharmony_ci{ 523af6ab5fSopenharmony_ci pg_->LoadConst(node, Constant::JS_UNDEFINED); 533af6ab5fSopenharmony_ci DirectReturn(node); 543af6ab5fSopenharmony_ci} 553af6ab5fSopenharmony_ci 563af6ab5fSopenharmony_civoid AsyncGeneratorFunctionBuilder::Yield(const ir::AstNode *node) 573af6ab5fSopenharmony_ci{ 583af6ab5fSopenharmony_ci Await(node); 593af6ab5fSopenharmony_ci 603af6ab5fSopenharmony_ci RegScope rs(pg_); 613af6ab5fSopenharmony_ci VReg completionType = pg_->AllocReg(); 623af6ab5fSopenharmony_ci VReg completionValue = pg_->AllocReg(); 633af6ab5fSopenharmony_ci 643af6ab5fSopenharmony_ci AsyncYield(node, completionType, completionValue); 653af6ab5fSopenharmony_ci 663af6ab5fSopenharmony_ci auto *notReturnCompletion = pg_->AllocLabel(); 673af6ab5fSopenharmony_ci auto *normalCompletion = pg_->AllocLabel(); 683af6ab5fSopenharmony_ci auto *notThrowCompletion = pg_->AllocLabel(); 693af6ab5fSopenharmony_ci if (notReturnCompletion == nullptr || normalCompletion == nullptr || notThrowCompletion == nullptr) { 703af6ab5fSopenharmony_ci // NOTE(ipetrov): maybe need to change it 713af6ab5fSopenharmony_ci return; 723af6ab5fSopenharmony_ci } 733af6ab5fSopenharmony_ci 743af6ab5fSopenharmony_ci // 27.6.3.8.8.a. If resumptionValue.[[Type]] is not return 753af6ab5fSopenharmony_ci pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN)); 763af6ab5fSopenharmony_ci pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notReturnCompletion); 773af6ab5fSopenharmony_ci // 27.6.3.8.8.b. Let awaited be Await(resumptionValue.[[Value]]). 783af6ab5fSopenharmony_ci pg_->LoadAccumulator(node, completionValue); 793af6ab5fSopenharmony_ci pg_->AsyncFunctionAwait(node, funcObj_); 803af6ab5fSopenharmony_ci SuspendResumeExecution(node, completionType, completionValue); 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_ci // 27.6.3.8.8.c. If awaited.[[Type]] is throw, return Completion(awaited). 833af6ab5fSopenharmony_ci pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW)); 843af6ab5fSopenharmony_ci 853af6ab5fSopenharmony_ci pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, normalCompletion); 863af6ab5fSopenharmony_ci pg_->LoadAccumulator(node, completionValue); 873af6ab5fSopenharmony_ci pg_->EmitThrow(node); 883af6ab5fSopenharmony_ci 893af6ab5fSopenharmony_ci pg_->SetLabel(node, normalCompletion); 903af6ab5fSopenharmony_ci // 27.6.3.8.8.d. Assert: awaited.[[Type]] is normal. 913af6ab5fSopenharmony_ci // 27.6.3.8.8.e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }. 923af6ab5fSopenharmony_ci pg_->ControlFlowChangeBreak(); 933af6ab5fSopenharmony_ci pg_->LoadAccumulator(node, completionValue); 943af6ab5fSopenharmony_ci pg_->DirectReturn(node); 953af6ab5fSopenharmony_ci 963af6ab5fSopenharmony_ci pg_->SetLabel(node, notReturnCompletion); 973af6ab5fSopenharmony_ci // 27.6.3.8.8.a. return Completion(resumptionValue). 983af6ab5fSopenharmony_ci pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW)); 993af6ab5fSopenharmony_ci pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notThrowCompletion); 1003af6ab5fSopenharmony_ci pg_->LoadAccumulator(node, completionValue); 1013af6ab5fSopenharmony_ci pg_->EmitThrow(node); 1023af6ab5fSopenharmony_ci pg_->SetLabel(node, notThrowCompletion); 1033af6ab5fSopenharmony_ci pg_->LoadAccumulator(node, completionValue); 1043af6ab5fSopenharmony_ci} 1053af6ab5fSopenharmony_ci 1063af6ab5fSopenharmony_ciIteratorType AsyncGeneratorFunctionBuilder::GeneratorKind() const 1073af6ab5fSopenharmony_ci{ 1083af6ab5fSopenharmony_ci return IteratorType::ASYNC; 1093af6ab5fSopenharmony_ci} 1103af6ab5fSopenharmony_ci} // namespace ark::es2panda::compiler 111