• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 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 #ifndef PANDA_GLOBAL_ETSOBJECTTYPE_TEST_H
17 #define PANDA_GLOBAL_ETSOBJECTTYPE_TEST_H
18 
19 namespace ark::es2panda::gtests {
20 
21 class GlobalETSObjectTypeTest : public testing::Test {
22 public:
GlobalETSObjectTypeTest()23     GlobalETSObjectTypeTest()
24         : allocator_(std::make_unique<ArenaAllocator>(SpaceType::SPACE_TYPE_COMPILER)),
25           publicContext_ {std::make_unique<public_lib::Context>()},
26           program_ {parser::Program::NewProgram<varbinder::ETSBinder>(allocator_.get())},
27           es2pandaPath_ {test::utils::PandaExecutablePathGetter {}.Get()}
28     {
29     }
30 
31     ~GlobalETSObjectTypeTest() override = default;
32 
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         constexpr auto COMPILER_SIZE = operator""_MB(256ULL);
36         mem::MemConfig::Initialize(0, 0, COMPILER_SIZE, 0, 0, 0);
37         PoolManager::Initialize();
38     }
39 
Checker()40     checker::ETSChecker *Checker()
41     {
42         return &checker_;
43     }
44 
InitializeChecker(std::string_view fileName,std::string_view src)45     void InitializeChecker(std::string_view fileName, std::string_view src)
46     {
47         auto es2pandaPathPtr = es2pandaPath_.c_str();
48         ASSERT(es2pandaPathPtr);
49 
50         InitializeChecker<parser::ETSParser, varbinder::ETSBinder, checker::ETSChecker, checker::ETSAnalyzer,
51                           compiler::ETSCompiler, compiler::ETSGen, compiler::StaticRegSpiller,
52                           compiler::ETSFunctionEmitter, compiler::ETSEmitter>(&es2pandaPathPtr, fileName, src,
53                                                                               &checker_, &program_);
54     }
55 
56     template <typename Parser, typename VarBinder, typename Checker, typename Analyzer, typename AstCompiler,
57               typename CodeGen, typename RegSpiller, typename FunctionEmitter, typename Emitter>
InitializeChecker(const char ** argv,std::string_view fileName,std::string_view src,checker::ETSChecker * checker,parser::Program * program)58     void InitializeChecker(const char **argv, std::string_view fileName, std::string_view src,
59                            checker::ETSChecker *checker, parser::Program *program)
60     {
61         auto options = std::make_unique<ark::es2panda::util::Options>();
62         if (!options->Parse(1, argv)) {
63             std::cerr << options->ErrorMsg() << std::endl;
64             return;
65         }
66 
67         ark::Logger::ComponentMask mask {};
68         mask.set(ark::Logger::Component::ES2PANDA);
69         ark::Logger::InitializeStdLogging(ark::Logger::LevelFromString(options->LogLevel()), mask);
70 
71         Compiler compiler(options->Extension(), options->ThreadCount());
72         SourceFile input(fileName, src, options->ParseModule());
73         compiler::CompilationUnit unit {input, *options, 0, options->Extension()};
74         auto getPhases = compiler::GetPhaseList(ScriptExtension::ETS);
75 
76         program->MarkEntry();
77         auto parser =
78             Parser(program, unit.options.CompilerOptions(), static_cast<parser::ParserStatus>(unit.rawParserStatus));
79         auto analyzer = Analyzer(checker);
80         checker->SetAnalyzer(&analyzer);
81 
82         auto *varbinder = program->VarBinder();
83         varbinder->SetProgram(program);
84 
85         varbinder->SetContext(publicContext_.get());
86 
87         auto emitter = Emitter(publicContext_.get());
88 
89         auto config = public_lib::ConfigImpl {};
90         publicContext_->config = &config;
91         publicContext_->config->options = &unit.options;
92         publicContext_->sourceFile = &unit.input;
93         publicContext_->allocator = allocator_.get();
94         publicContext_->parser = &parser;
95         publicContext_->checker = checker;
96         publicContext_->analyzer = publicContext_->checker->GetAnalyzer();
97         publicContext_->emitter = &emitter;
98         publicContext_->parserProgram = program;
99 
100         parser.ParseScript(unit.input, unit.options.CompilerOptions().compilationMode == CompilationMode::GEN_STD_LIB);
101         for (auto *phase : getPhases) {
102             if (!phase->Apply(publicContext_.get(), program)) {
103                 return;
104             }
105         }
106     }
107 
108     NO_COPY_SEMANTIC(GlobalETSObjectTypeTest);
109     NO_MOVE_SEMANTIC(GlobalETSObjectTypeTest);
110 
111 private:
112     std::unique_ptr<ArenaAllocator> allocator_;
113     std::unique_ptr<public_lib::Context> publicContext_;
114     parser::Program program_;
115     std::string es2pandaPath_;
116     checker::ETSChecker checker_;
117 };
118 
119 }  // namespace ark::es2panda::gtests
120 #endif  // PANDA_GLOBAL_ETSOBJECTTYPE_TEST_H
121