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_JS_MODULE_H 17 #define CPP_ABCKIT_JS_MODULE_H 18 19 #include "../core/module.h" 20 #include "../base_concepts.h" 21 #include "./import_descriptor.h" 22 #include "./export_descriptor.h" 23 24 #include <string_view> 25 26 namespace abckit::js { 27 28 /** 29 * @brief Module 30 */ 31 class Module final : public core::Module { 32 // To access private constructor. 33 // We restrict constructors in order to prevent C/C++ API mix-up by user. 34 /// @brief to access private constructor 35 friend class abckit::File; 36 /// @brief abckit::DefaultHash<Module> 37 friend class abckit::DefaultHash<Module>; 38 /// @brief to access private TargetCast 39 friend class abckit::traits::TargetCheckCast<Module>; 40 41 public: 42 /** 43 * @brief Constructor Arkts API Module from the Core API with compatibility check 44 * @param coreModule - Core API Module 45 */ 46 explicit Module(const core::Module &coreModule); 47 48 /** 49 * @brief Construct a new Module object 50 * @param other 51 */ 52 Module(const Module &other) = default; 53 54 /** 55 * @brief Constructor 56 * @param other 57 * @return Module& 58 */ 59 Module &operator=(const Module &other) = default; 60 61 /** 62 * @brief Construct a new Module object 63 * @param other 64 */ 65 Module(Module &&other) = default; 66 67 /** 68 * @brief Constructor 69 * @param other 70 * @return Module& 71 */ 72 Module &operator=(Module &&other) = default; 73 74 /** 75 * @brief Destroy the Module object 76 */ 77 ~Module() override = default; 78 79 /** 80 * @brief Adds import from one Js module to another Js module. 81 * @return Newly created import descriptor. 82 * @param [ in ] imported - The module the `importing` module imports from. 83 * @param [ in ] name - Name of created import. 84 * @param [ in ] alias - Alias of created import. 85 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `imported` is false. 86 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 87 * @note Allocates 88 */ 89 ImportDescriptor AddImportFromJsToJs(Module imported, std::string_view name, std::string_view alias) const; 90 91 /** 92 * @brief Removes import `id` from Module. 93 * @param [ in ] desc - Import to remove from the Module. 94 * @return New state of Module. 95 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 96 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `id` is false. 97 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if Module does not have the import descriptor `id`. 98 */ 99 Module RemoveImport(ImportDescriptor desc) const; 100 101 /** 102 * @brief Adds export to the Js module. 103 * @return Newly created export descriptor. 104 * @param [ in ] exported - The module the entity is exported from. 105 * @param [ in ] name - Name of created export. 106 * @param [ in ] alias - Alias of created export. 107 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `exported` is false. 108 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 109 * @note Allocates 110 */ 111 ExportDescriptor AddExportFromJsToJs(Module exported, std::string_view name, std::string_view alias) const; 112 113 /** 114 * @brief Removes export `ed` from Module. 115 * @param [ in ] desc - Export to remove from the Module. 116 * @return New state of Module. 117 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if view itself is false. 118 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if `ed` is false. 119 * @note Set `ABCKIT_STATUS_BAD_ARGUMENT` error if module `m` does not have the export descriptor `ed`. 120 */ 121 Module RemoveExport(ExportDescriptor desc) const; 122 123 private: 124 /** 125 * @brief Converts underlying module from Core to Js target 126 * @return AbckitJsModule* - converted module 127 * @note Set `ABCKIT_STATUS_WRONG_TARGET` error if `this` does not have `ABCKIT_TARGET_JS` 128 */ 129 AbckitJsModule *TargetCast() const; 130 131 ABCKIT_NO_UNIQUE_ADDRESS traits::TargetCheckCast<Module> targetChecker_; 132 }; 133 134 } // namespace abckit::js 135 136 #endif // CPP_ABCKIT_JS_MODULE_H 137