1 /*
2 * Copyright (c) 2024-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 #ifndef CPP_ABCKIT_FILE_IMPL_H
17 #define CPP_ABCKIT_FILE_IMPL_H
18
19 #include <string_view>
20 #include "file.h"
21 #include "js/module.h"
22 #include "arkts/module.h"
23
24 namespace abckit {
25
26 // CC-OFFNXT(G.FUD.06) perf critical
GetModules()27 inline std::vector<core::Module> File::GetModules() const
28 {
29 std::vector<core::Module> modules;
30 Payload<std::vector<core::Module> *> payload {&modules, GetApiConfig(), this};
31
32 GetApiConfig()->cIapi_->fileEnumerateModules(GetResource(), &payload, [](AbckitCoreModule *module, void *data) {
33 const auto &payload = *static_cast<Payload<std::vector<core::Module> *> *>(data);
34 payload.data->push_back(core::Module(module, payload.config, payload.resource));
35 return true;
36 });
37
38 CheckError(GetApiConfig());
39
40 return modules;
41 }
42
CreateLiteralArray(const std::vector<abckit::Literal> & literals)43 inline abckit::LiteralArray File::CreateLiteralArray(const std::vector<abckit::Literal> &literals) const
44 {
45 std::vector<AbckitLiteral *> litsImpl;
46 litsImpl.reserve(literals.size());
47 for (const auto &literal : literals) {
48 litsImpl.push_back(literal.GetView());
49 }
50 AbckitLiteralArray *litaIml =
51 GetApiConfig()->cMapi_->createLiteralArray(GetResource(), litsImpl.data(), litsImpl.size());
52 CheckError(GetApiConfig());
53 return abckit::LiteralArray(litaIml, GetApiConfig(), this);
54 }
55
EnumerateModules(const std::function<bool (core::Module)> & cb)56 inline bool File::EnumerateModules(const std::function<bool(core::Module)> &cb) const
57 {
58 Payload<const std::function<bool(core::Module)> &> payload {cb, GetApiConfig(), this};
59
60 auto isNormalExit =
61 GetApiConfig()->cIapi_->fileEnumerateModules(GetResource(), &payload, [](AbckitCoreModule *module, void *data) {
62 const auto &payload = *static_cast<Payload<const std::function<bool(core::Module)> &> *>(data);
63 return payload.data(core::Module(module, payload.config, payload.resource));
64 });
65
66 CheckError(GetApiConfig());
67 return isNormalExit;
68 }
69
EnumerateExternalModules(const std::function<bool (core::Module)> & cb)70 inline bool File::EnumerateExternalModules(const std::function<bool(core::Module)> &cb) const
71 {
72 Payload<const std::function<bool(core::Module)> &> payload {cb, GetApiConfig(), this};
73
74 auto isNormalExit = GetApiConfig()->cIapi_->fileEnumerateExternalModules(
75 GetResource(), &payload, [](AbckitCoreModule *module, void *data) {
76 const auto &payload = *static_cast<Payload<const std::function<bool(core::Module)> &> *>(data);
77 return payload.data(core::Module(module, payload.config, payload.resource));
78 });
79
80 CheckError(GetApiConfig());
81 return isNormalExit;
82 }
83
CreateType(enum AbckitTypeId id)84 inline Type File::CreateType(enum AbckitTypeId id) const
85 {
86 auto t = GetApiConfig()->cMapi_->createType(GetResource(), id);
87 CheckError(GetApiConfig());
88 return Type(t, GetApiConfig(), this);
89 }
90
CreateReferenceType(core::Class & klass)91 inline Type File::CreateReferenceType(core::Class &klass) const
92 {
93 auto rt = GetApiConfig()->cMapi_->createReferenceType(GetResource(), klass.GetView());
94 CheckError(GetApiConfig());
95 return Type(rt, GetApiConfig(), this);
96 }
97
CreateValueU1(bool val)98 inline abckit::Value File::CreateValueU1(bool val) const
99 {
100 AbckitValue *value = GetApiConfig()->cMapi_->createValueU1(GetResource(), val);
101 CheckError(GetApiConfig());
102 return abckit::Value(value, GetApiConfig(), this);
103 }
104
CreateValueDouble(double val)105 inline abckit::Value File::CreateValueDouble(double val) const
106 {
107 AbckitValue *value = GetApiConfig()->cMapi_->createValueDouble(GetResource(), val);
108 CheckError(GetApiConfig());
109 return abckit::Value(value, GetApiConfig(), this);
110 }
111
CreateLiteralBool(bool val)112 inline abckit::Literal File::CreateLiteralBool(bool val) const
113 {
114 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralBool(GetResource(), val);
115 CheckError(GetApiConfig());
116 return abckit::Literal(literal, GetApiConfig(), this);
117 }
118
CreateLiteralDouble(double val)119 inline abckit::Literal File::CreateLiteralDouble(double val) const
120 {
121 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralDouble(GetResource(), val);
122 CheckError(GetApiConfig());
123 return abckit::Literal(literal, GetApiConfig(), this);
124 }
125
CreateLiteralLiteralArray(const abckit::LiteralArray & val)126 inline abckit::Literal File::CreateLiteralLiteralArray(const abckit::LiteralArray &val) const
127 {
128 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralLiteralArray(GetResource(), val.GetView());
129 CheckError(GetApiConfig());
130 return abckit::Literal(literal, GetApiConfig(), this);
131 }
132
CreateLiteralString(std::string_view val)133 inline abckit::Literal File::CreateLiteralString(std::string_view val) const
134 {
135 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralString(GetResource(), val.data(), val.size());
136 CheckError(GetApiConfig());
137 return abckit::Literal(literal, GetApiConfig(), this);
138 }
139
CreateValueString(std::string_view value)140 inline Value File::CreateValueString(std::string_view value) const
141 {
142 auto val = GetApiConfig()->cMapi_->createValueString(GetResource(), value.data(), value.size());
143 CheckError(GetApiConfig());
144 return Value(val, GetApiConfig(), this);
145 }
146
CreateLiteralArrayValue(std::vector<Value> & value,size_t size)147 inline Value File::CreateLiteralArrayValue(std::vector<Value> &value, size_t size) const
148 {
149 std::vector<AbckitValue *> values(size);
150 for (auto &val : value) {
151 values.push_back(val.GetView());
152 }
153 auto arr = GetApiConfig()->cMapi_->createLiteralArrayValue(GetResource(), &values[0], size);
154 CheckError(GetApiConfig());
155 return Value(arr, GetApiConfig(), this);
156 }
157
CreateLiteralU8(uint8_t value)158 inline abckit::Literal File::CreateLiteralU8(uint8_t value) const
159 {
160 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralU8(GetResource(), value);
161 CheckError(GetApiConfig());
162 return abckit::Literal(literal, GetApiConfig(), this);
163 }
164
CreateLiteralU16(uint16_t value)165 inline abckit::Literal File::CreateLiteralU16(uint16_t value) const
166 {
167 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralU16(GetResource(), value);
168 CheckError(GetApiConfig());
169 return abckit::Literal(literal, GetApiConfig(), this);
170 }
171
CreateLiteralMethodAffiliate(uint16_t value)172 inline Literal File::CreateLiteralMethodAffiliate(uint16_t value) const
173 {
174 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralMethodAffiliate(GetResource(), value);
175 CheckError(GetApiConfig());
176 return abckit::Literal(literal, GetApiConfig(), this);
177 }
178
CreateLiteralU32(uint32_t value)179 inline Literal File::CreateLiteralU32(uint32_t value) const
180 {
181 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralU32(GetResource(), value);
182 CheckError(GetApiConfig());
183 return abckit::Literal(literal, GetApiConfig(), this);
184 }
185
CreateLiteralU64(uint64_t value)186 inline Literal File::CreateLiteralU64(uint64_t value) const
187 {
188 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralU64(GetResource(), value);
189 CheckError(GetApiConfig());
190 return abckit::Literal(literal, GetApiConfig(), this);
191 }
192
CreateLiteralFloat(float value)193 inline Literal File::CreateLiteralFloat(float value) const
194 {
195 AbckitLiteral *literal = GetApiConfig()->cMapi_->createLiteralFloat(GetResource(), value);
196 CheckError(GetApiConfig());
197 return abckit::Literal(literal, GetApiConfig(), this);
198 }
199
CreateLiteralMethod(core::Function & function)200 inline Literal File::CreateLiteralMethod(core::Function &function) const
201 {
202 auto func = GetApiConfig()->cMapi_->createLiteralMethod(GetResource(), function.GetView());
203 CheckError(GetApiConfig());
204 return Literal(func, GetApiConfig(), this);
205 }
206
AddExternalModuleArktsV1(std::string_view name)207 inline arkts::Module File::AddExternalModuleArktsV1(std::string_view name) const
208 {
209 const struct AbckitArktsV1ExternalModuleCreateParams params {
210 name.data()
211 };
212 auto mod = GetApiConfig()->cArktsMapi_->fileAddExternalModuleArktsV1(GetResource(), ¶ms);
213 CheckError(GetApiConfig());
214 auto coreMod = GetApiConfig()->cArktsIapi_->arktsModuleToCoreModule(mod);
215 CheckError(GetApiConfig());
216 return arkts::Module(core::Module(coreMod, GetApiConfig(), this));
217 }
218
AddExternalModuleJs(std::string_view name)219 inline js::Module File::AddExternalModuleJs(std::string_view name) const
220 {
221 const struct AbckitJsExternalModuleCreateParams params {
222 name.data()
223 };
224 auto jsMod = GetApiConfig()->cJsMapi_->fileAddExternalModule(GetResource(), ¶ms);
225 CheckError(GetApiConfig());
226 auto coreMod = GetApiConfig()->cJsIapi_->jsModuleToCoreModule(jsMod);
227 CheckError(GetApiConfig());
228 return js::Module(core::Module(coreMod, GetApiConfig(), this));
229 }
230
231 } // namespace abckit
232
233 #endif // CPP_ABCKIT_FILE_IMPL_H
234