1 /**
2  * Copyright (c) 2021-2024 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 <gtest/gtest.h>
17 #include <algorithm>
18 #include "macros.h"
19 
20 #include "assembler/assembly-program.h"
21 #include "ir/astDump.h"
22 #include "ir/expressions/literals/stringLiteral.h"
23 
24 #include "bytecode_optimizer/bytecodeopt_options.h"
25 #include "compiler/compiler_logger.h"
26 #include "mem/arena_allocator.h"
27 #include "mem/pool_manager.h"
28 #include "es2panda.h"
29 #include "util/arktsconfig.h"
30 #include "util/generateBin.h"
31 #include "util/options.h"
32 #include "libpandabase/mem/mem.h"
33 #include "test/utils/panda_executable_path_getter.h"
34 
35 namespace {
36 
37 struct TestParams {
TestParams__anon16::TestParams38     explicit TestParams(std::string_view testSrc, std::string testArgsList = std::string {},
39                         int testArgsCount = ARGS_COUNT_DEFAULT, std::string_view testFileName = FILE_NAME_DEFAULT)
40         : src {testSrc}, argsList {std::move(testArgsList)}, argsCount {testArgsCount}, fileName {testFileName}
41     {
42     }
43 
44     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
45     std::string_view src;
46     std::string argsList;
47     int argsCount;
48     std::string_view fileName;
49 
50     static constexpr int ARGS_COUNT_DEFAULT = 1;
51     static constexpr std::string_view FILE_NAME_DEFAULT = "dummy.sts";
52     // NOLINTEND(misc-non-private-member-variables-in-classes)
53 };
54 
DumpJsonSimple()55 TestParams DumpJsonSimple()
56 {
57     static constexpr std::string_view SRC =
58         "\
59         function main(args: String[]): int {\
60             let a: int = 2;\
61             let b: int = 3;\
62             return a + b;\
63         }";
64 
65     return TestParams {SRC};
66 }
67 
DumpJsonUTF16Char()68 TestParams DumpJsonUTF16Char()
69 {
70     static constexpr std::string_view SRC =
71         "\
72         function main(args: String[]): int {\
73             let a: char = c'\\uDBFF';\
74             let b: char = c'\\uDC00';\
75             console.log(a);\
76             console.log(b);\
77             return 0;\
78         }";
79 
80     return TestParams {SRC};
81 }
82 
DumpEtsSrcSimple()83 TestParams DumpEtsSrcSimple()
84 {
85     static constexpr std::string_view SRC =
86         "\
87         function main(args: String[]): int {\
88             let a: int = 2;\
89             let b: int = 3;\
90             return a + b;\
91         }";
92 
93     auto es2pandaPath = test::utils::PandaExecutablePathGetter {}.Get();
94     auto argsList =
95         es2pandaPath +
96         "--extension=sts "
97         "--dump-ets-src-before-phases=\"plugins-after-parse:lambda-lowering:checker:plugins-after-check:generate-"
98         "ts-"
99         "declarations:op-assignment:tuple-lowering:union-property-access:plugins-after-lowering\"";
100 
101     return TestParams {SRC, argsList};
102 }
103 
104 }  // namespace
105 
106 class ASTDumperTest : public testing::TestWithParam<TestParams> {
107 public:
ASTDumperTest()108     ASTDumperTest()
109     {
110         constexpr auto COMPILER_SIZE = 268435456;
111 
112         ark::mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0);
113         ark::PoolManager::Initialize(ark::PoolType::MMAP);
114     }
115 
116     ~ASTDumperTest() override
117     {
118         ark::PoolManager::Finalize();
119         ark::mem::MemConfig::Finalize();
120     };
121 
GetProgram(std::string_view src, const char **argsList, int argsCount, std::string_view fileName)122     static ark::pandasm::Program *GetProgram(std::string_view src, const char **argsList, int argsCount,
123                                              std::string_view fileName)
124     {
125         auto options = std::make_unique<ark::es2panda::util::Options>();
126         if (!options->Parse(argsCount, argsList)) {
127             std::cerr << options->ErrorMsg() << std::endl;
128             return nullptr;
129         }
130 
131         ark::Logger::ComponentMask mask {};
132         mask.set(ark::Logger::Component::ES2PANDA);
133         ark::Logger::InitializeStdLogging(ark::Logger::LevelFromString(options->LogLevel()), mask);
134 
135         ark::es2panda::Compiler compiler(options->Extension(), options->ThreadCount());
136         ark::es2panda::SourceFile input(fileName, src, options->ParseModule());
137 
138         return compiler.Compile(input, *options);
139     }
140 
141     NO_COPY_SEMANTIC(ASTDumperTest);
142     NO_MOVE_SEMANTIC(ASTDumperTest);
143 };
144 
TEST_P(ASTDumperTest, CheckNoDump)145 TEST_P(ASTDumperTest, CheckNoDump)
146 {
147     auto param = GetParam();
148     if (param.argsList.empty()) {
149         param.argsList = test::utils::PandaExecutablePathGetter {}.Get();
150     }
151 
152     auto argsListPtr = param.argsList.c_str();
153 
154     auto program =
155         std::unique_ptr<ark::pandasm::Program> {GetProgram(param.src, &argsListPtr, param.argsCount, param.fileName)};
156     ASSERT(program);
157 
158     auto dumpStr = program->JsonDump();
159     ASSERT(!dumpStr.empty());
160 }
161 
162 INSTANTIATE_TEST_SUITE_P(ASTDumperTestParamList, ASTDumperTest,
163                          ::testing::Values(DumpJsonSimple(), DumpJsonUTF16Char(), DumpEtsSrcSimple()));
164