1 /*
2 * Copyright (c) 2021-2022 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 "asyncFunctionBuilder.h"
17
18 #include <compiler/core/pandagen.h>
19 #include <compiler/base/catchTable.h>
20 #include <ir/base/scriptFunction.h>
21
22 namespace panda::es2panda::compiler {
23
DirectReturn(const ir::AstNode * node) const24 void AsyncFunctionBuilder::DirectReturn(const ir::AstNode *node) const
25 {
26 pg_->AsyncFunctionResolve(node, funcObj_); // retVal is in acc
27 pg_->NotifyConcurrentResult(node);
28 pg_->EmitReturn(node);
29 }
30
ImplicitReturn(const ir::AstNode * node) const31 void AsyncFunctionBuilder::ImplicitReturn(const ir::AstNode *node) const
32 {
33 pg_->LoadConst(node, Constant::JS_UNDEFINED);
34 DirectReturn(node);
35 }
36
ExplicitReturn(const ir::AstNode * node) const37 void AsyncFunctionBuilder::ExplicitReturn(const ir::AstNode *node) const
38 {
39 DirectReturn(node);
40 }
41
Prepare(const ir::ScriptFunction * node)42 void AsyncFunctionBuilder::Prepare(const ir::ScriptFunction *node)
43 {
44 pg_->AsyncFunctionEnter(node);
45 pg_->StoreAccumulator(node, funcObj_);
46 pg_->SetLabel(node, catchTable_->LabelSet().TryBegin());
47 }
48
CleanUp(const ir::ScriptFunction * node) const49 void AsyncFunctionBuilder::CleanUp(const ir::ScriptFunction *node) const
50 {
51 RegScope rs(pg_);
52 const auto &labelSet = catchTable_->LabelSet();
53
54 pg_->SetLabel(node, labelSet.TryEnd());
55 pg_->SetLabel(node, labelSet.CatchBegin());
56 VReg exception = pg_->AllocReg();
57 pg_->StoreAccumulator(node, exception);
58 pg_->AsyncFunctionReject(node, funcObj_);
59 pg_->NotifyConcurrentResult(node);
60 pg_->EmitReturn(node);
61 pg_->SetLabel(node, labelSet.CatchEnd());
62 }
63 } // namespace panda::es2panda::compiler
64