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_IMPL_H
17 #define CPP_ABCKIT_JS_MODULE_IMPL_H
18
19 #include "module.h"
20
21 // NOLINTBEGIN(performance-unnecessary-value-param)
22 namespace abckit::js {
23
TargetCast()24 inline AbckitJsModule *Module::TargetCast() const
25 {
26 auto ret = GetApiConfig()->cJsIapi_->coreModuleToJsModule(GetView());
27 CheckError(GetApiConfig());
28 return ret;
29 }
30
Module(const core::Module & coreModule)31 inline Module::Module(const core::Module &coreModule) : core::Module(coreModule), targetChecker_(this) {}
32
AddImportFromJsToJs(Module imported,std::string_view name,std::string_view alias)33 inline ImportDescriptor Module::AddImportFromJsToJs(Module imported, std::string_view name,
34 std::string_view alias) const
35 {
36 const AbckitJsImportFromDynamicModuleCreateParams params {name.data(), alias.data()};
37 auto *jsid = GetApiConfig()->cJsMapi_->moduleAddImportFromJsToJs(TargetCast(), imported.TargetCast(), ¶ms);
38 CheckError(GetApiConfig());
39 auto *coreid = GetApiConfig()->cJsIapi_->jsImportDescriptorToCoreImportDescriptor(jsid);
40 CheckError(GetApiConfig());
41 return js::ImportDescriptor(core::ImportDescriptor(coreid, GetApiConfig(), GetResource()));
42 }
43
RemoveImport(ImportDescriptor desc)44 inline Module Module::RemoveImport(ImportDescriptor desc) const
45 {
46 GetApiConfig()->cJsMapi_->moduleRemoveImport(TargetCast(), desc.TargetCast());
47 CheckError(GetApiConfig());
48 return *this;
49 }
50
AddExportFromJsToJs(Module exported,std::string_view name,std::string_view alias)51 inline ExportDescriptor Module::AddExportFromJsToJs(Module exported, std::string_view name,
52 std::string_view alias) const
53 {
54 const AbckitJsDynamicModuleExportCreateParams params {name.data(), alias.data()};
55 auto *jsed = GetApiConfig()->cJsMapi_->moduleAddExportFromJsToJs(TargetCast(), exported.TargetCast(), ¶ms);
56 CheckError(GetApiConfig());
57 auto *coreed = GetApiConfig()->cJsIapi_->jsExportDescriptorToCoreExportDescriptor(jsed);
58 CheckError(GetApiConfig());
59 return js::ExportDescriptor(core::ExportDescriptor(coreed, GetApiConfig(), GetResource()));
60 }
61
RemoveExport(ExportDescriptor desc)62 inline Module Module::RemoveExport(ExportDescriptor desc) const
63 {
64 GetApiConfig()->cJsMapi_->moduleRemoveExport(TargetCast(), desc.TargetCast());
65 CheckError(GetApiConfig());
66 return *this;
67 }
68
69 } // namespace abckit::js
70 // NOLINTEND(performance-unnecessary-value-param)
71
72 #endif // CPP_ABCKIT_JS_MODULE_IMPL_H