1 /**
2  * Copyright (c) 2021-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 #include "plugin.h"
16 #include "os/library_loader.h"
17 
18 namespace ark::es2panda::util {
19 
FullNameForProcedure(std::string const &shortName)20 std::string Plugin::FullNameForProcedure(std::string const &shortName)
21 {
22     return std::string(name_.Utf8()) + "_" + shortName;
23 }
24 
Plugin(util::StringView const &name)25 Plugin::Plugin(util::StringView const &name) : name_ {name}, err_ {0}, h_ {nullptr}
26 {
27     std::string soName =
28         os::library_loader::DYNAMIC_LIBRARY_PREFIX + std::string(name) + os::library_loader::DYNAMIC_LIBRARY_SUFFIX;
29     if (auto loadRes = os::library_loader::Load(soName); loadRes.HasValue()) {
30         h_ = std::move(loadRes.Value());
31     } else {
32         err_ = loadRes.Error();
33         ok_ = false;
34     }
35 
36     if (auto initRes = os::library_loader::ResolveSymbol(h_, FullNameForProcedure("Initialize")); initRes.HasValue()) {
37         initialize_ = reinterpret_cast<void (*)()>(initRes.Value());
38     }
39 
40     if (auto apRes = os::library_loader::ResolveSymbol(h_, FullNameForProcedure("AfterParse")); apRes.HasValue()) {
41         afterParse_ = reinterpret_cast<void (*)(es2panda_Context *)>(apRes.Value());
42     }
43 
44     if (auto acRes = os::library_loader::ResolveSymbol(h_, FullNameForProcedure("AfterCheck")); acRes.HasValue()) {
45         afterCheck_ = reinterpret_cast<void (*)(es2panda_Context *)>(acRes.Value());
46     }
47 
48     if (auto alRes = os::library_loader::ResolveSymbol(h_, FullNameForProcedure("AfterLowerings")); alRes.HasValue()) {
49         afterLowerings_ = reinterpret_cast<void (*)(es2panda_Context *)>(alRes.Value());
50     }
51 }
52 
53 }  // namespace ark::es2panda::util
54