• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用NAPI接口在C++新创建的线程中进行模块加载
2
3Node-API中的napi_load_module_with_info接口的功能是支持在C++新创建的线程中进行模块的加载,当模块加载出来之后,可以使用函数napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数
4
5## 函数说明
6```cpp
7napi_status napi_load_module_with_info(napi_env env,
8                                       const char* path,
9                                       const char* module_info,
10                                       napi_value* result);
11```
12| 参数            | 说明          |
13| :------------- | :----------------------------- |
14| env            | 当前的虚拟机环境       |
15| path          | 加载的文件路径或者模块名          |
16| module_info   | bundleName/moduleName的路径拼接       |
17| result         | 加载的模块          |
18
19注:
20
211. bundleName表示AppScope/app.json5中配置的工程名
222. moduleName指的是待加载模块所在的HAP下module.json5中配置的名字
23
24## napi_load_module_with_info支持的场景
25
26| 场景            | 详细分类           | 说明                         |
27| :------------- | :----------------------------- | :--------------------------- |
28| 本地工程模块   | 加载模块内文件路径       | 要求路径以moduleName开头             |
29| 本地工程模块   | 加载HAR模块名           | -                            |
30| 远程包         | 加载远程HAR模块名        | -                            |
31| 远程包         | 加载ohpm包名            | -                            |
32| API        |    加载@ohos.* 或 @system.*          | -                            |
33| 模块Native库   | 加载libNativeLibrary.so | -                            |
34
35## 使用示例
36
37- **加载模块内文件路径**
38
39当加载文件中的模块时,如以下ArkTS代码:
40```javascript
41//./src/main/ets/Test.ets
42let value = 123;
43function test() {
44  console.log("Hello OpenHarmony");
45}
46export {value, test};
47```
48
491. 需要在工程的build-profile.json5文件中进行以下配置
50```json
51{
52    "buildOption" : {
53        "arkOptions" : {
54            "runtimeOnly" : {
55                "sources": [
56                    "./src/main/ets/Test.ets"
57                ]
58            }
59        }
60    }
61}
62```
63
64
652. 使用napi_load_module_with_info加载Test文件,调用函数test以及获取变量value
66
67```cpp
68static napi_value loadModule(napi_env env, napi_callback_info info) {
69    napi_value result;
70    //1. 使用napi_load_module加载Test文件中的模块
71    napi_status status = napi_load_module_with_info(env, "entry/src/main/ets/Test", "com.example.application/entry", &result);
72
73    napi_value testFn;
74    //2. 使用napi_get_named_property获取test函数
75    napi_get_named_property(env, result, "test", &testFn);
76    //3. 使用napi_call_function调用函数test
77    napi_call_function(env, result, testFn, 0, nullptr, nullptr);
78
79    napi_value value;
80    napi_value key;
81    std::string keyStr = "value";
82    napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
83    //4. 使用napi_get_property获取变量value
84    napi_get_property(env, result, key, &value);
85    return result
86}
87```
88
89- **加载HAR模块名**
90
91HAR包Index.ets文件如下
92```javascript
93//library Index.ets
94let value = 123;
95function test() {
96  console.log("Hello OpenHarmony");
97}
98export {value, test};
99```
1001. 在加载本地HAR包时,首先需要在oh-package.json5文件中配置dependencies项
101```json
102{
103    "dependencies": {
104        "library": "file:../library"
105    }
106}
107```
1082. 其次,还需要在build-profile.json5中进项配置
109```json
110{
111    "buildOption" : {
112        "arkOptions" : {
113            "runtimeOnly" : {
114                "packages": [
115                    "library"
116                ]
117            }
118        }
119    }
120}
121```
122
1233. 用napi_load_module_with_info加载library,调用函数test以及获取变量value
124```cpp
125static napi_value loadModule(napi_env env, napi_callback_info info) {
126    napi_value result;
127    //1. 使用napi_load_module加载Test文件中的模块
128    napi_status status = napi_load_module_with_info(env, "library", "com.example.application/entry", &result);
129
130    napi_value testFn;
131    //2. 使用napi_get_named_property获取test函数
132    napi_get_named_property(env, result, "test", &testFn);
133    //3. 使用napi_call_function调用函数test
134    napi_call_function(env, result, testFn, 0, nullptr, nullptr);
135
136    napi_value value;
137    napi_value key;
138    std::string keyStr = "value";
139    napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
140    //4. 使用napi_get_property获取变量value
141    napi_get_property(env, result, key, &value);
142    return result
143}
144```
145
146- **加载远程HAR模块名**
1471. 在远程HAR模块名时,首先需要在oh-package.json5文件中配置dependencies项
148```json
149{
150    "dependencies": {
151        "@ohos/hypium": "1.0.16"
152    }
153}
154```
1552. 其次,还需要在build-profile.json5中进项配置
156```json
157{
158    "buildOption" : {
159        "arkOptions" : {
160            "runtimeOnly" : {
161                "packages": [
162                    "@ohos/hypium"
163                ]
164            }
165        }
166    }
167}
168```
1693. 用napi_load_module_with_info加载@ohos/hypium,获取DEFAULT变量
170```cpp
171static napi_value loadModule(napi_env env, napi_callback_info info) {
172    napi_value result;
173    //1. 使用napi_load_module加载@ohos/hypium
174    napi_status status = napi_load_module_with_info(env, "@ohos/hypium", "com.example.application/entry", &result);
175
176    napi_value key;
177    std::string keyStr = "DEFAULT";
178    napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
179    //2. 使用napi_get_property获取DEFAULT变量
180    napi_value defaultValue;
181    napi_get_property(env, result, key, &defaultValue);
182    return result
183}
184```
185
186- **加载ohpm包名**
1871. 在加载ohpm包时,首先需要在oh-package.json5文件中配置dependencies项
188```json
189{
190    "dependencies": {
191        "json5": "^2.2.3"
192    }
193}
194```
1952. 其次,还需要在build-profile.json5中进项配置
196```json
197{
198    "buildOption" : {
199        "arkOptions" : {
200            "runtimeOnly" : {
201                "packages": [
202                    "json5"
203                ]
204            }
205        }
206    }
207}
208```
209
2103. 用napi_load_module_with_info加载json5,调用函数stringify
211```cpp
212static napi_value loadModule(napi_env env, napi_callback_info info) {
213    napi_value result;
214    //1. 使用napi_load_module加载json5
215    napi_status status = napi_load_module_with_info(env, "json5", "com.example.application/entry", &result);
216
217    napi_value key;
218    std::string keyStr = "default";
219    napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
220    //2. 使用napi_get_property获取default对象
221    napi_value defaultValue;
222    napi_get_property(env, result, key, &defaultValue);
223
224    napi_value stringifyFn;
225    //3. 使用napi_get_named_property获取stringify函数
226    napi_get_named_property(env, defaultValue, "stringify", &stringifyFn);
227    //4. 使用napi_call_function调用函数stringify
228    napi_value argStr;
229    std::string info = "call json5 stringify";
230    napi_create_string_utf8(env, info.c_str(), info.size(), &argStr);
231    napi_value args[1] = {argStr};
232
233    napi_value returnValue;
234    napi_call_function(env, result, stringifyFn, 1, args, &returnValue);
235    return result
236}
237```
238
239- **加载API模块**
240
241```cpp
242static napi_value loadModule(napi_env env, napi_callback_info info) {
243    //1. 使用napi_load_module加载模块@ohos.hilog
244    napi_value result;
245    napi_status status = napi_load_module_with_info(env, "@ohos.hilog", nullptr, &result);
246
247    //2. 使用napi_get_named_property获取info函数
248    napi_value infoFn;
249    napi_get_named_property(env, result, "info", &infoFn);
250
251    napi_value tag;
252    std::string formatStr = "test";
253    napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag);
254
255    napi_value outputString;
256    std::string str = "Hello OpenHarmony";
257    napi_create_string_utf8(env, str.c_str(), str.size(), &outputString);
258
259    napi_value flag;
260    napi_create_int32(env, 0, &flag);
261
262    napi_value args[3] = {flag, tag, outputString};
263    //3. 使用napi_call_function调用info函数
264    napi_call_function(env, result, infoFn, 3, args, nullptr);
265    return result
266}
267```
268
269- **模块Native库**
270libentry.soindex.d.ts文件如下
271```javascript
272//index.d.ts
273export const add: (a: number, b: number) => number;
274
275```
2761. 在加载本地so库时,首先需要在oh-package.json5文件中配置dependencies项
277```json
278{
279    "dependencies": {
280        "libentry.so": "file../src/main/cpp/types/libentry"
281    }
282}
283```
2842. 其次,还需要在build-profile.json5中进项配置
285```json
286{
287    "buildOption" : {
288        "arkOptions" : {
289            "runtimeOnly" : {
290                "packages": [
291                    "libentry.so"
292                ]
293            }
294        }
295    }
296}
297```
298
2993. 用napi_load_module_with_info加载libentry.so,调用函数add
300```cpp
301static napi_value loadModule(napi_env env, napi_callback_info info) {
302    napi_value result;
303    //1. 使用napi_load_module加载libentry.so
304    napi_status status = napi_load_module_with_info(env, "libentry.so", "com.example.application/entry", &result);
305
306    napi_value addFn;
307    //2. 使用napi_get_named_property获取add函数
308    napi_get_named_property(env, result, "add", &result);
309
310    napi_value a;
311    napi_value b;
312    napi_create_int32(env, 2, &a);
313    napi_create_int32(env, 3, &b);
314    napi_value args[1] = {a, b};
315    //3. 使用napi_call_function调用函数add
316    napi_value returnValue;
317    napi_call_function(env, result, addFn, 2, args, &returnValue);
318    return result
319}
320```