• 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 #include <iostream>
17 #include <ostream>
18 #include <string>
19 #include "public/es2panda_lib.h"
20 #include "os/library_loader.h"
21 
22 // NOLINTBEGIN
23 
24 static const char *LIBNAME = "es2panda-public";
25 static const int MIN_ARGC = 3;
26 static const int NULLPTR_IMPL_ERROR_CODE = 2;
27 
28 static es2panda_Impl *impl = nullptr;
29 
GetImpl()30 es2panda_Impl *GetImpl()
31 {
32     if (impl != nullptr) {
33         return impl;
34     }
35 
36     std::string soName = ark::os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(LIBNAME) +
37                          ark::os::library_loader::DYNAMIC_LIBRARY_SUFFIX;
38     auto libraryRes = ark::os::library_loader::Load(soName);
39     if (!libraryRes.HasValue()) {
40         std::cout << "Error in load lib" << std::endl;
41         return nullptr;
42     }
43 
44     auto library = std::move(libraryRes.Value());
45     auto getImpl = ark::os::library_loader::ResolveSymbol(library, "es2panda_GetImpl");
46     if (!getImpl.HasValue()) {
47         std::cout << "Error in load func get impl" << std::endl;
48         return nullptr;
49     }
50 
51     auto getImplFunc = reinterpret_cast<const es2panda_Impl *(*)(int)>(getImpl.Value());
52     if (getImplFunc != nullptr) {
53         return const_cast<es2panda_Impl *>(getImplFunc(ES2PANDA_LIB_VERSION));
54     }
55     return nullptr;
56 }
57 
CheckForErrors(std::string StateName,es2panda_Context * context)58 void CheckForErrors(std::string StateName, es2panda_Context *context)
59 {
60     if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) {
61         std::cout << "PROCEED TO " << StateName << " ERROR" << std::endl;
62         std::cout << impl->ContextErrorMessage << std::endl;
63     } else {
64         std::cout << "PROCEED TO " << StateName << " SUCCESS" << std::endl;
65     }
66 }
67 
main(int argc,char ** argv)68 int main(int argc, char **argv)
69 {
70     if (argc < MIN_ARGC) {
71         return 1;
72     }
73 
74     if (GetImpl() == nullptr) {
75         return NULLPTR_IMPL_ERROR_CODE;
76     }
77     impl = GetImpl();
78     std::cout << "LOAD SUCCESS" << std::endl;
79 
80     const char **args = const_cast<const char **>(&(argv[1]));
81     auto config = impl->CreateConfig(argc - 1, args);
82     auto context = impl->CreateContextFromFile(config, argv[argc - 1]);
83     if (context != nullptr) {
84         std::cout << "CREATE CONTEXT SUCCESS" << std::endl;
85     }
86 
87     impl->ProceedToState(context, ES2PANDA_STATE_PARSED);
88     CheckForErrors("PARSE", context);
89 
90     impl->ProceedToState(context, ES2PANDA_STATE_SCOPE_INITED);
91     CheckForErrors("SCOPE INITED", context);
92 
93     impl->ProceedToState(context, ES2PANDA_STATE_CHECKED);
94     CheckForErrors("CHECKED", context);
95 
96     impl->ProceedToState(context, ES2PANDA_STATE_LOWERED);
97     CheckForErrors("LOWERED", context);
98 
99     impl->ProceedToState(context, ES2PANDA_STATE_ASM_GENERATED);
100     CheckForErrors("ASM", context);
101 
102     impl->ProceedToState(context, ES2PANDA_STATE_BIN_GENERATED);
103     CheckForErrors("BIN", context);
104     impl->DestroyConfig(config);
105 
106     return 0;
107 }
108 
109 // NOLINTEND
110