• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "asyncGeneratorFunctionBuilder.h"
17 
18 #include "compiler/base/catchTable.h"
19 #include "compiler/core/pandagen.h"
20 #include "ir/base/scriptFunction.h"
21 
22 namespace panda::es2panda::compiler {
Prepare(const ir::ScriptFunction * node) const23 void AsyncGeneratorFunctionBuilder::Prepare(const ir::ScriptFunction *node) const
24 {
25     VReg callee = FunctionReg(node);
26 
27     pg_->CreateAsyncGeneratorObj(node, callee);
28     pg_->StoreAccumulator(node, funcObj_);
29     pg_->SuspendGenerator(node, funcObj_);
30     pg_->SetLabel(node, catchTable_->LabelSet().TryBegin());
31 }
32 
CleanUp(const ir::ScriptFunction * node) const33 void AsyncGeneratorFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const
34 {
35     const auto &labelSet = catchTable_->LabelSet();
36 
37     pg_->SetLabel(node, labelSet.TryEnd());
38     pg_->SetLabel(node, labelSet.CatchBegin());
39     pg_->AsyncGeneratorReject(node, funcObj_);
40     pg_->EmitReturn(node);
41     pg_->SetLabel(node, labelSet.CatchEnd());
42 }
43 
DirectReturn(const ir::AstNode * node) const44 void AsyncGeneratorFunctionBuilder::DirectReturn(const ir::AstNode *node) const
45 {
46     pg_->AsyncGeneratorResolve(node, funcObj_);
47     pg_->EmitReturn(node);
48 }
49 
ImplicitReturn(const ir::AstNode * node) const50 void AsyncGeneratorFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const
51 {
52     pg_->LoadConst(node, Constant::JS_UNDEFINED);
53     DirectReturn(node);
54 }
55 
Yield(const ir::AstNode * node)56 void AsyncGeneratorFunctionBuilder::Yield(const ir::AstNode *node)
57 {
58     Await(node);
59 
60     RegScope rs(pg_);
61     VReg completionType = pg_->AllocReg();
62     VReg completionValue = pg_->AllocReg();
63 
64     AsyncYield(node, completionType, completionValue);
65 
66     auto *notReturnCompletion = pg_->AllocLabel();
67     auto *normalCompletion = pg_->AllocLabel();
68     auto *notThrowCompletion = pg_->AllocLabel();
69 
70     // 27.6.3.8.8.a. If resumptionValue.[[Type]] is not return
71     pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::RETURN));
72     pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notReturnCompletion);
73     // 27.6.3.8.8.b. Let awaited be Await(resumptionValue.[[Value]]).
74     pg_->LoadAccumulator(node, completionValue);
75     pg_->AsyncFunctionAwait(node, funcObj_);
76     SuspendResumeExecution(node, completionType, completionValue);
77 
78     // 27.6.3.8.8.c. If awaited.[[Type]] is throw, return Completion(awaited).
79     pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
80 
81     pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, normalCompletion);
82     pg_->LoadAccumulator(node, completionValue);
83     pg_->EmitThrow(node);
84 
85     pg_->SetLabel(node, normalCompletion);
86     // 27.6.3.8.8.d. Assert: awaited.[[Type]] is normal.
87     // 27.6.3.8.8.e. Return Completion { [[Type]]: return, [[Value]]: awaited.[[Value]], [[Target]]: empty }.
88     pg_->ControlFlowChangeBreak();
89     pg_->LoadAccumulator(node, completionValue);
90     pg_->DirectReturn(node);
91 
92     pg_->SetLabel(node, notReturnCompletion);
93     // 27.6.3.8.8.a. return Completion(resumptionValue).
94     pg_->LoadAccumulatorInt(node, static_cast<int32_t>(ResumeMode::THROW));
95     pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notThrowCompletion);
96     pg_->LoadAccumulator(node, completionValue);
97     pg_->EmitThrow(node);
98     pg_->SetLabel(node, notThrowCompletion);
99     pg_->LoadAccumulator(node, completionValue);
100 }
101 
GeneratorKind() const102 IteratorType AsyncGeneratorFunctionBuilder::GeneratorKind() const
103 {
104     return IteratorType::ASYNC;
105 }
106 }  // namespace panda::es2panda::compiler
107