• 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 CPP_ABCKIT_FILE_H
17 #define CPP_ABCKIT_FILE_H
18 
19 #include "base_classes.h"
20 #include "value.h"
21 #include "literal.h"
22 #include "literal_array.h"
23 
24 #include "core/module.h"
25 #include "arkts/module.h"
26 #include "js/module.h"
27 #include "config.h"
28 
29 #include <functional>
30 #include <memory>
31 #include <string>
32 #include <string_view>
33 #include <vector>
34 
35 namespace abckit {
36 
37 /**
38  * @brief File
39  */
40 class File final : public Resource<AbckitFile *> {
41     /// @brief To access private constructor
42     friend class abckit::DynamicIsa;
43     /// @brief To access private constructor
44     friend class abckit::Instruction;
45 
46 private:
47     class FileDeleter final : public IResourceDeleter {
48     public:
FileDeleter(const ApiConfig * conf,const File & file)49         FileDeleter(const ApiConfig *conf, const File &file) : conf_(conf), deleterFile_(file) {};
50         FileDeleter(const FileDeleter &other) = delete;
51         FileDeleter &operator=(const FileDeleter &other) = delete;
52         FileDeleter(FileDeleter &&other) = delete;
53         FileDeleter &operator=(FileDeleter &&other) = delete;
54         ~FileDeleter() override = default;
55 
DeleteResource()56         void DeleteResource() override
57         {
58             conf_->cApi_->closeFile(deleterFile_.GetResource());
59         }
60 
61     private:
62         const ApiConfig *conf_;
63         const File &deleterFile_;
64     };
65 
66 public:
67     /**
68      * @brief Constructor
69      * @param path
70      */
File(std::string_view path)71     explicit File(std::string_view path) : File(path, std::make_unique<DefaultErrorHandler>()) {}
72 
73     /**
74      * @brief Deleted constructor
75      * @param file
76      */
77     File(const File &file) = delete;
78 
79     /**
80      * @brief Deleted constructor
81      * @param file
82      * @return File
83      */
84     File &operator=(const File &file) = delete;
85 
86     /**
87      * @brief Deleted constructor
88      * @param file
89      */
90     File(File &&file) = delete;
91 
92     /**
93      * @brief Deleted constructor
94      * @param file
95      * @return File
96      */
97     File &operator=(File &&file) = delete;
98 
99     /**
100      * @brief Constructor
101      * @param path
102      * @param eh
103      */
File(std::string_view path,std::unique_ptr<IErrorHandler> eh)104     File(std::string_view path, std::unique_ptr<IErrorHandler> eh)
105         : Resource(AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0)->openAbc(path.data(), path.size())),
106           conf_(std::move(eh))
107     {
108         CheckError(&conf_);
109         SetDeleter(std::make_unique<FileDeleter>(&conf_, *this));
110     }
111     /**
112      * @brief Destructor
113      */
114     ~File() override = default;
115 
116     /**
117      * @brief Creates value item `AbckitValue` containing the given boolean value `value`.
118      * @param val
119      * @return `Value`
120      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
121      */
122     abckit::Value CreateValueU1(bool val) const;
123 
124     /**
125      * @brief Creates value item containing the given double value `value`.
126      * @param val
127      * @return `Value`
128      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
129      */
130     abckit::Value CreateValueDouble(double val) const;
131 
132     /**
133      * @brief Creates value item containing the given bool value `value`.
134      * @param val
135      * @return `Value`
136      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
137      */
138     abckit::Literal CreateLiteralBool(bool val) const;
139 
140     /**
141      * @brief Creates Literal containing the given byte value `value`.
142      * @return `Literal`.
143      * @param [ in ] value - Byte value that will be stored in the literal to be created.
144      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
145      * @note Allocates
146      */
147     abckit::Literal CreateLiteralU8(uint8_t value) const;
148 
149     /**
150      * @brief Creates Literal containing the given short value `value`.
151      * @return `Literal`.
152      * @param [ in ] value - Short value that will be stored in the literal to be created.
153      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
154      * @note Allocates
155      */
156     abckit::Literal CreateLiteralU16(uint16_t value) const;
157 
158     /**
159      * @brief Creates Literal containing the given integer value `value`.
160      * @return `Literal`.
161      * @param [ in ] value - Integer value that will be stored in the literal to be created.
162      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
163      * @note Allocates
164      */
165     abckit::Literal CreateLiteralU32(uint32_t value) const;
166 
167     /**
168      * @brief Creates Literal containing the given long value `value`.
169      * @return `Literal`.
170      * @param [ in ] value - Long value that will be stored in the literal to be created.
171      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
172      * @note Allocates
173      */
174     Literal CreateLiteralU64(uint64_t value) const;
175 
176     /**
177      * @brief Creates Literal containing the given float value `value`.
178      * @return `Literal`.
179      * @param [ in ] value - Float value that will be stored in the literal to be created.
180      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
181      * @note Allocates
182      */
183     Literal CreateLiteralFloat(float value) const;
184 
185     /**
186      * @brief Creates value item containing the given bool value `value`.
187      * @param val
188      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
189      * @return `Value`
190      */
191     abckit::Literal CreateLiteralDouble(double val) const;
192 
193     /**
194      * @brief Creates literal containing the given literal array `litarr`.
195      * @param val
196      * @return `Literal`
197      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
198      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'val' is false.
199      */
200     abckit::Literal CreateLiteralLiteralArray(const abckit::LiteralArray &val) const;
201 
202     /**
203      * @brief Creates literal array value item from the given value items array `literals`.
204      * @param literals
205      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
206      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if one or more elements from 'literals' are false.
207      * @return `LiteralArray`
208      */
209     abckit::LiteralArray CreateLiteralArray(const std::vector<abckit::Literal> &literals) const;
210 
211     /**
212      * @brief Creates literal containing the given string `val`.
213      * @return `Literal`.
214      * @param [ in ] val - string that will be stored in the literal to be created.
215      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
216      * @note Allocates
217      */
218     abckit::Literal CreateLiteralString(std::string_view val) const;
219 
220     /**
221      * @brief Creates Literal containing the given function `function`.
222      * @return `Literal`.
223      * @param [ in ] function - Function that will be stored in the literal to be created.
224      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
225      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if 'function' is false.
226      * @note Allocates
227      */
228     Literal CreateLiteralMethod(core::Function &function) const;
229 
230     /**
231      * @brief Writes `file` to the specified `path`.
232      * @param path - path to file
233      */
WriteAbc(std::string_view path)234     void WriteAbc(std::string_view path) const
235     {
236         GetApiConfig()->cApi_->writeAbc(GetResource(), path.data(), path.size());
237         CheckError(GetApiConfig());
238     }
239 
240     /**
241      * @brief Returns a vector of core::Module's in a particular file
242      * @return vector of `Module`.
243      */
244     std::vector<core::Module> GetModules() const;
245 
246     /**
247      * @brief Enumerates modules that are defined in binary file `file`, invoking callback `cb` for each of
248      * such modules.
249      * @return `false` if was early exited. Otherwise - `true`.
250      * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations
251      * should continue.
252      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `cb` is false.
253      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
254      */
255     bool EnumerateModules(const std::function<bool(core::Module)> &cb) const;
256 
257     /**
258      * @brief Enumerates modules that are defined in other binary file, but are referenced in binary file `file`,
259      * invoking callback `cb` for each of such modules.
260      * @return `false` if was early exited. Otherwise - `true`.
261      * @param [ in ] cb - Callback that will be invoked. Should return `false` on early exit and `true` when iterations
262      * should continue.
263      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `cb` is false.
264      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
265      */
266     bool EnumerateExternalModules(const std::function<bool(core::Module)> &cb) const;
267 
268     /**
269      * @brief Creates type according to type id `id`.
270      * @return `Type`
271      * @param [ in ] id - Type id corresponding to the type being created.
272      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
273      * @note Allocates
274      */
275     Type CreateType(enum AbckitTypeId id) const;
276 
277     /**
278      * @brief Creates reference type according to class `klass`.
279      * @return `Type`
280      * @param [ in ] klass - Class from which the type is created.
281      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
282      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `klass` is false.
283      * @note Allocates
284      */
285     Type CreateReferenceType(core::Class &klass) const;
286 
287     /**
288      * @brief Creates value item `Value` containing the given string `value`.
289      * @return `Value`.
290      * @param [ in ] value - string value from which value item is created.
291      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
292      * @note Allocates
293      */
294     Value CreateValueString(std::string_view value) const;
295 
296     /**
297      * @brief Creates literal array value item with size `size` from the given value items array `value`.
298      * @return `Value`
299      * @param [ in ] value - Value items from which literal array item is created.
300      * @param [ in ] size - Size of the literal array value item to be created.
301      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
302      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if one or more elements from 'value' are false.
303      */
304     Value CreateLiteralArrayValue(std::vector<Value> &value, size_t size) const;
305 
306     /**
307      * @brief Creates Literal containing the given оffset of method affiliate `value`.
308      * @return `Literal`.
309      * @param [ in ] value - Offset of method affiliate.
310      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if current File is false.
311      * @note Allocates
312      */
313     Literal CreateLiteralMethodAffiliate(uint16_t value) const;
314 
315     /**
316      * @brief Creates an external Arkts module with target `ABCKIT_TARGET_ARK_TS_V1` and adds it to the File.
317      * @return Newly created Module.
318      * @param [ in ] name - Data that is used to create the external module.
319      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false.
320      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `name` is NULL.
321      * @note Allocates
322      */
323     arkts::Module AddExternalModuleArktsV1(std::string_view name) const;
324 
325     /**
326      * @brief Creates an external Js module and adds it to the file `file`.
327      * @return `js::Module`
328      * @param [ in ] name - Data that is used to create the external module.
329      * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is NULL.
330      * @note Allocates
331      */
332     js::Module AddExternalModuleJs(std::string_view name) const;
333 
334 protected:
335     /**
336      * @brief Get api config
337      * @return ApiConfig
338      */
GetApiConfig()339     const ApiConfig *GetApiConfig() const override
340     {
341         return &conf_;
342     }
343 
344     /**
345      * @brief Struct for using in callbacks
346      */
347     template <typename D>
348     struct Payload {
349         /**
350          * @brief data
351          */
352         D data;
353         /**
354          * @brief config
355          */
356         const ApiConfig *config;
357         /**
358          * @brief resource
359          */
360         const File *resource;
361     };
362 
363 private:
364     const ApiConfig conf_;
365 };
366 
367 }  // namespace abckit
368 
369 #endif  // CPP_ABCKIT_FILE_H
370