• 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 #ifndef PANDA_PLUGINS_ETS_RUNTIME_ANI_OPTIONS_PARSER_H
16 #define PANDA_PLUGINS_ETS_RUNTIME_ANI_OPTIONS_PARSER_H
17 
18 #include <string>
19 #include <vector>
20 
21 #include "ani.h"
22 #ifdef PANDA_TARGET_OHOS
23 #include "plugins/ets/runtime/platform/ohos/logger.h"
24 #endif
25 
26 namespace ark::ets::ani {
27 
28 /**
29  * @brief Class represents ANI options parser
30  *
31  * Class unites panda runtime options and ANI specific options parsing
32  *
33  */
34 class ANIOptionsParser {
35 public:
36     using LoggerCallback = void (*)(FILE *stream, int level, const char *component, const char *message);
37 
ANIOptionsParser(size_t optionsSize,const ani_option * inputOptions)38     ANIOptionsParser(size_t optionsSize, const ani_option *inputOptions)
39         : optionsSize_(optionsSize), inputOptions_(inputOptions)
40     {
41         Parse();
42     }
43 
GetRuntimeOptions()44     const std::vector<std::string> &GetRuntimeOptions()
45     {
46         return runtimeOptions_;
47     }
48 
GetANIOptions()49     const std::vector<ani_option> &GetANIOptions()
50     {
51         return aniSpecificOptions_;
52     }
53 
IsInteropMode()54     bool IsInteropMode() const
55     {
56         return isInteropMode_;
57     }
58 
GetInteropEnv()59     void *GetInteropEnv() const
60     {
61         return interopEnv_;
62     }
63 
GetLoggerCallback()64     LoggerCallback GetLoggerCallback()
65     {
66 #ifdef PANDA_TARGET_OHOS
67         return loggerCallback_ == nullptr ? ohos::DefaultLogger : loggerCallback_;
68 #else
69         return loggerCallback_;
70 #endif
71     }
72 
73 private:
Parse()74     void Parse()
75     {
76         const std::string depricatedPref = "--ext:--";
77         const std::string prefix = "--ext:";
78 
79         for (size_t i = 0; i < optionsSize_; ++i) {
80             // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
81             const ani_option opt = inputOptions_[i];
82             std::string option(opt.option);
83 
84             if (option == "--logger") {
85                 loggerCallback_ = reinterpret_cast<LoggerCallback>(opt.extra);
86                 continue;
87             }
88 
89             // NOTE(konstanting, #23205): this explicit comparison was requested by v.cherkashin
90             // for better readability. To be refactored.
91             if (option == "--ext:interop") {
92                 isInteropMode_ = true;
93                 interopEnv_ = opt.extra;
94                 continue;
95             }
96 
97             // NOTE: need to skip forbidden options, such as "--load-runtimes"
98             if (option.size() >= depricatedPref.size() && option.substr(0, depricatedPref.size()) == depricatedPref) {
99                 runtimeOptions_.push_back("--" + option.substr(depricatedPref.size()));
100             } else if (option.size() >= prefix.size() && option.substr(0, prefix.size()) == prefix) {
101                 runtimeOptions_.push_back("--" + option.substr(prefix.size()));
102             }
103         }
104     }
105 
106     void *interopEnv_ {nullptr};
107     bool isInteropMode_ {false};
108     LoggerCallback loggerCallback_ = nullptr;
109 
110     size_t optionsSize_;
111     const ani_option *inputOptions_;
112     std::vector<ani_option> aniSpecificOptions_;
113     std::vector<std::string> runtimeOptions_;
114 };
115 }  // namespace ark::ets::ani
116 
117 #endif  // PANDA_PLUGINS_ETS_RUNTIME_ANI_OPTIONS_PARSER_H
118