• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "interfaces/inner_api/ace/declarative_module_preloader.h"
17 
18 #include <dlfcn.h>
19 
20 namespace OHOS::Ace {
21 
22 using CreateFunc = void (*)(void*);
23 constexpr char PRE_INIT_ACE_MODULE_FUNC[] = "OHOS_ACE_PreloadAceModule";
24 
InitAceModule(void * runtime)25 void InitAceModule(void* runtime)
26 {
27     void* handle = dlopen("libace.z.so", RTLD_LAZY);
28     if (handle == nullptr) {
29         return;
30     }
31 
32     auto entry = reinterpret_cast<CreateFunc>(dlsym(handle, PRE_INIT_ACE_MODULE_FUNC));
33     if (entry == nullptr) {
34         dlclose(handle);
35         return;
36     }
37 
38     entry(runtime);
39 }
40 
Preload(NativeEngine & runtime)41 void DeclarativeModulePreloader::Preload(NativeEngine& runtime)
42 {
43     InitAceModule(reinterpret_cast<void*>(&runtime));
44 }
45 
46 // ArkTsCard start
47 using CreateFuncCard = void (*)(void*);
48 constexpr char PRE_INIT_ACE_MODULE_FUNC_CARD[] = "OHOS_ACE_PreloadAceModuleCard";
49 
InitAceModuleCard(void * runtime)50 void InitAceModuleCard(void* runtime)
51 {
52     void* handle = dlopen("libace.z.so", RTLD_LAZY);
53     if (handle == nullptr) {
54         return;
55     }
56 
57     auto entry = reinterpret_cast<CreateFuncCard>(dlsym(handle, PRE_INIT_ACE_MODULE_FUNC_CARD));
58     if (entry == nullptr) {
59         dlclose(handle);
60         return;
61     }
62 
63     entry(runtime);
64 }
65 
PreloadCard(NativeEngine & runtime)66 void DeclarativeModulePreloader::PreloadCard(NativeEngine& runtime)
67 {
68     InitAceModuleCard(reinterpret_cast<void*>(&runtime));
69 }
70 // ArkTsCard end
71 } // namespace OHOS::Ace