• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 <algorithm>
17 #include <cstddef>
18 #include <iostream>
19 #include <ostream>
20 #include <string>
21 #include <vector>
22 #include "util.h"
23 #include "public/es2panda_lib.h"
24 
25 // NOLINTBEGIN
26 
27 static es2panda_Impl *impl = nullptr;
28 
CheckExternalSources(es2panda_Context * context,es2panda_Program * pgm)29 static bool CheckExternalSources(es2panda_Context *context, es2panda_Program *pgm)
30 {
31     size_t n = 0;
32     auto externalSources = impl->ProgramExternalSources(context, pgm, &n);
33     if (externalSources == nullptr) {
34         return false;
35     }
36     bool findImport = false;
37     for (size_t i = 0; i < n; i++) {
38         size_t j = 0;
39         auto extPrograms = impl->ExternalSourcePrograms(externalSources[i], &j);
40         for (size_t k = 0; k < j; k++) {
41             if (impl->ProgramIsStdLibConst(context, extPrograms[k])) {
42                 continue;
43             }
44             findImport |= std::string(impl->ProgramFileNameConst(context, extPrograms[k])) == "export" &&
45                           impl->ProgramKindConst(context, extPrograms[k]) == Es2pandaScriptKind::SCRIPT_KIND_SCRIPT;
46         }
47     }
48     return findImport;
49 }
50 
main(int argc,char ** argv)51 int main(int argc, char **argv)
52 {
53     if (argc < MIN_ARGC) {
54         return 1;
55     }
56 
57     if (GetImpl() == nullptr) {
58         return NULLPTR_IMPL_ERROR_CODE;
59     }
60     impl = GetImpl();
61     std::cout << "LOAD SUCCESS" << std::endl;
62     const char **args = const_cast<const char **>(&(argv[1]));
63     auto config = impl->CreateConfig(argc - 1, args);
64     auto context = impl->CreateContextFromFile(config, argv[argc - 1]);
65     if (context != nullptr) {
66         std::cout << "CREATE CONTEXT SUCCESS" << std::endl;
67     }
68 
69     impl->ProceedToState(context, ES2PANDA_STATE_PARSED);
70     CheckForErrors("PARSE", context);
71 
72     impl->ProceedToState(context, ES2PANDA_STATE_BOUND);
73     CheckForErrors("BOUND", context);
74 
75     impl->ProceedToState(context, ES2PANDA_STATE_CHECKED);
76     CheckForErrors("CHECKED", context);
77 
78     auto pgm = impl->ContextProgram(context);
79     if (!CheckExternalSources(context, pgm)) {
80         return TEST_ERROR_CODE;
81     }
82 
83     impl->AstNodeRecheck(context, impl->ProgramAst(context, pgm));
84 
85     impl->ProceedToState(context, ES2PANDA_STATE_LOWERED);
86     CheckForErrors("LOWERED", context);
87 
88     impl->ProceedToState(context, ES2PANDA_STATE_ASM_GENERATED);
89     CheckForErrors("ASM", context);
90 
91     impl->ProceedToState(context, ES2PANDA_STATE_BIN_GENERATED);
92     CheckForErrors("BIN", context);
93     if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) {
94         return PROCEED_ERROR_CODE;
95     }
96     impl->DestroyContext(context);
97     impl->DestroyConfig(config);
98 
99     return 0;
100 }
101 
102 // NOLINTEND
103