• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 使用Node-API接口在主线程中进行模块加载
2
3## 场景介绍
4
5Node-API中的napi_load_module接口的功能是在主线程中进行模块的加载,当模块加载出来之后,可以使用函数napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数。
6
7## 函数说明
8
9```cpp
10napi_status napi_load_module(napi_env env,
11                             const char* path,
12                             napi_value* result);
13```
14
15| 参数            | 说明          |
16| :------------- | :----------------------------- |
17| env            | 当前的虚拟机环境       |
18| path          | 加载的文件路径或者模块名          |
19| result         | 加载的模块          |
20
21## 使用限制
22
23- 禁止在非主线程当中使用该接口。
24- 禁止在Init函数中使用该接口。
25- 禁止在线程安全函数的回调函数当中进行文件路径的加载。
26
27建议使用[napi_load_module_with_info](use-napi-load-module-with-info.md)来进行模块加载,该接口支持了更多的场景。
28
29## napi_load_module支持的场景
30| 场景            | 详细分类           | 说明                         |
31| :------------- | :----------------------------- | :--------------------------- |
32| 系统模块        |    加载@ohos.*或 @system.*  | -                            |
33| 本地工程模块   | 加载ets目录下文件中的模块      | 要求路径以ets开头             |
34| 本地工程模块   | 加载模块内文件路径       | 要求路径以moduleName开头             |
35| 本地工程模块   | 加载HAR模块名           | -                            |
36| 本地工程模块   | 加载HSP模块名           | -                            |
37| 远程包         | 加载远程HAR模块名        | -                            |
38| 远程包         | 加载ohpm包名            | -                            |
39| 模块Native库   | 加载libNativeLibrary.so | -                            |
40
41- **加载系统模块**
42
43    ```cpp
44    static napi_value loadModule(napi_env env, napi_callback_info info) {
45        // 1. 使用napi_load_module加载模块@ohos.hilog
46        napi_value result;
47        napi_status status = napi_load_module(env, "@ohos.hilog", &result);
48        if (status != napi_ok) {
49            return nullptr;
50        }
51
52        // 2. 使用napi_get_named_property获取info函数
53        napi_value infoFn;
54        napi_get_named_property(env, result, "info", &infoFn);
55
56        napi_value tag;
57        std::string formatStr = "test";
58        napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag);
59
60        napi_value outputString;
61        std::string str = "Hello OpenHarmony";
62        napi_create_string_utf8(env, str.c_str(), str.size(), &outputString);
63
64        napi_value flag;
65        napi_create_int32(env, 0, &flag);
66
67        napi_value args[3] = {flag, tag, outputString};
68        // 3. 使用napi_call_function调用info函数
69        napi_call_function(env, result, infoFn, 3, args, nullptr);
70        return result;
71    }
72    ```
73
74- **加载ets目录下文件中的模块**
75
76    当加载文件中的模块时,如以下ArkTS代码:
77
78    ```javascript
79    //./src/main/ets/Test.ets
80    let value = 123;
81    function test() {
82    console.log("Hello OpenHarmony");
83    }
84    export {value, test};
85    ```
86
871. 需要在工程的build-profile.json5文件中进行以下配置:
88
89    ```json
90    {
91        "buildOption" : {
92            "arkOptions" : {
93                "runtimeOnly" : {
94                    "sources": [
95                        "./src/main/ets/Test.ets"
96                    ]
97                }
98            }
99        }
100    }
101    ```
102
1032. 使用napi_load_module加载Test文件,调用函数test以及获取变量value:
104
105    ```cpp
106    static napi_value loadModule(napi_env env, napi_callback_info info) {
107        napi_value result;
108        // 1. 使用napi_load_module加载Test文件中的模块
109        napi_status status = napi_load_module(env, "ets/Test", &result);
110        if (status != napi_ok) {
111            return nullptr;
112        }
113
114        napi_value testFn;
115        // 2. 使用napi_get_named_property获取test函数
116        napi_get_named_property(env, result, "test", &testFn);
117        // 3. 使用napi_call_function调用函数test
118        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
119
120        napi_value value;
121        napi_value key;
122        std::string keyStr = "value";
123        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
124        // 4. 使用napi_get_property获取变量value
125        napi_get_property(env, result, key, &value);
126        return result;
127    }
128    ```
129- **加载模块内文件路径**
130
131    当加载文件中的模块时,如以下ArkTS代码:
132
133    ```javascript
134    //./src/main/ets/Test.ets
135    let value = 123;
136    function test() {
137    console.log("Hello OpenHarmony");
138    }
139    export {value, test};
140    ```
141
1421. 需要当前模块的build-profile.json5文件中进行以下配置:
143
144    ```json
145    {
146        "buildOption" : {
147            "arkOptions" : {
148                "runtimeOnly" : {
149                    "sources": [
150                        "./src/main/ets/Test.ets"
151                    ]
152                }
153            }
154        }
155    }
156    ```
157
1582. 使用napi_load_module加载Test文件,调用函数test以及获取变量value:
159
160    ```cpp
161    static napi_value loadModule(napi_env env, napi_callback_info info) {
162        napi_value result;
163        // 1. 使用napi_load_module加载Test文件中的模块
164        napi_status status = napi_load_module(env, "entry/src/main/ets/Test", &result);
165        if (status != napi_ok) {
166            return nullptr;
167        }
168
169        napi_value testFn;
170        // 2. 使用napi_get_named_property获取test函数
171        napi_get_named_property(env, result, "test", &testFn);
172        // 3. 使用napi_call_function调用函数test
173        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
174
175        napi_value value;
176        napi_value key;
177        std::string keyStr = "value";
178        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
179        // 4. 使用napi_get_property获取变量value
180        napi_get_property(env, result, key, &value);
181        return result;
182    }
183    ```
184
185- **加载HAR模块名**
186
187    HAR包Index.ets文件如下:
188
189    ```javascript
190    //library Index.ets
191    let value = 123;
192    function test() {
193    console.log("Hello OpenHarmony");
194    }
195    export {value, test};
196    ```
197
1981. 在当前模块下的oh-package.json5文件中配置dependencies项:
199
200    ```json
201    {
202        "dependencies": {
203            "library": "file:../library"
204        }
205    }
206    ```
207
2082. 在使用library的模块中,对build-profile.json5进行配置:
209
210    ```json
211    {
212        "buildOption" : {
213            "arkOptions" : {
214                "runtimeOnly" : {
215                    "packages": [
216                        "library"
217                    ]
218                }
219            }
220        }
221    }
222    ```
223
2243. 用napi_load_module加载library,调用函数test以及获取变量value:
225
226    ```cpp
227    static napi_value loadModule(napi_env env, napi_callback_info info) {
228        napi_value result;
229        // 1. 使用napi_load_module加载library
230        napi_status status = napi_load_module(env, "library", &result);
231        if (status != napi_ok) {
232            return nullptr;
233        }
234
235        napi_value testFn;
236        // 2. 使用napi_get_named_property获取test函数
237        napi_get_named_property(env, result, "test", &testFn);
238        // 3. 使用napi_call_function调用函数test
239        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
240
241        napi_value value;
242        napi_value key;
243        std::string keyStr = "value";
244        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
245        // 4. 使用napi_get_property获取变量value
246        napi_get_property(env, result, key, &value);
247        return result;
248    }
249    ```
250
251- **加载HSP模块名**
252
253    HSP包Index.ets文件如下:
254
255    ```javascript
256    //sharedlibrary Index.ets
257    let value = 123;
258    function test() {
259    console.log("Hello OpenHarmony");
260    }
261    export {value, test};
262    ```
263
2641. 在当前模块下的oh-package.json5文件中配置dependencies项:
265
266    ```json
267    {
268        "dependencies": {
269            "sharedlibrary": "file:../sharedlibrary"
270        }
271    }
272    ```
273
2742. 在使用library的模块中,对build-profile.json5进行配置:
275
276    ```json
277    {
278        "buildOption" : {
279            "arkOptions" : {
280                "runtimeOnly" : {
281                    "packages": [
282                        "sharedlibrary"
283                    ]
284                }
285            }
286        }
287    }
288    ```
289
2903. 用napi_load_module加载sharedlibrary,调用函数test以及获取变量value:
291
292    ```cpp
293    static napi_value loadModule(napi_env env, napi_callback_info info) {
294        napi_value result;
295        // 1. 使用napi_load_module加载sharedlibrary
296        napi_status status = napi_load_module(env, "sharedlibrary", &result);
297        if (status != napi_ok) {
298            return nullptr;
299        }
300
301        napi_value testFn;
302        // 2. 使用napi_get_named_property获取test函数
303        napi_get_named_property(env, result, "test", &testFn);
304        // 3. 使用napi_call_function调用函数test
305        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
306
307        napi_value value;
308        napi_value key;
309        std::string keyStr = "value";
310        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
311        // 4. 使用napi_get_property获取变量value
312        napi_get_property(env, result, key, &value);
313        return result;
314    }
315    ```
316
317- **加载远程HAR模块名**
318
3191. 在当前模块下的oh-package.json5文件中配置dependencies项:
320
321    ```json
322    {
323        "dependencies": {
324            "@ohos/hypium": "1.0.16"
325        }
326    }
327    ```
328
3292. 在使用@ohos/hypium的模块中,对build-profile.json5进行配置:
330
331    ```json
332    {
333        "buildOption" : {
334            "arkOptions" : {
335                "runtimeOnly" : {
336                    "packages": [
337                        "@ohos/hypium"
338                    ]
339                }
340            }
341        }
342    }
343    ```
344
3453. 用napi_load_module加载@ohos/hypium,获取DEFAULT变量:
346
347    ```cpp
348    static napi_value loadModule(napi_env env, napi_callback_info info) {
349        napi_value result;
350        // 1. 使用napi_load_module加载@ohos/hypium
351        napi_status status = napi_load_module(env, "@ohos/hypium", &result);
352        if (status != napi_ok) {
353            return nullptr;
354        }
355
356        napi_value key;
357        std::string keyStr = "DEFAULT";
358        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
359        // 2. 使用napi_get_property获取DEFAULT变量
360        napi_value defaultValue;
361        napi_get_property(env, result, key, &defaultValue);
362        return result;
363    }
364    ```
365
366- **加载ohpm包名**
367
3681. 在当前模块下的oh-package.json5文件中配置dependencies项:
369
370    ```json
371    {
372        "dependencies": {
373            "@ohos/axios": "2.2.4",
374        }
375    }
376    ```
377
3782. 在使用@ohos/axios的模块中,对build-profile.json5进行配置:
379
380    ```json
381    {
382        "buildOption" : {
383            "arkOptions" : {
384                "runtimeOnly" : {
385                    "packages": [
386                        "@ohos/axios"
387                    ]
388                }
389            }
390        }
391    }
392    ```
393
3943. 用napi_load_module加载@ohos/axios,获取VERSION变量:
395
396    ```cpp
397    static napi_value loadModule(napi_env env, napi_callback_info info) {
398        napi_value result;
399        // 1. 使用napi_load_module加载@ohos/axios
400        napi_status status = napi_load_module(env, "@ohos/axios", &result);
401        if (status != napi_ok) {
402            return nullptr;
403        }
404
405        napi_value key;
406        std::string keyStr = "VERSION";
407        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
408        // 2. 使用napi_get_property获取VERSION
409        napi_value defaultValue;
410        napi_get_property(env, result, key, &defaultValue);
411        return result;
412    }
413    ```
414
415- **加载Native库**
416
417    libentry.soindex.d.ts文件如下:
418
419    ```javascript
420    //index.d.ts
421    export const add: (a: number, b: number) => number;
422    ```
423
4241. 在当前模块下的oh-package.json5文件中配置dependencies项:
425
426    ```json
427    {
428        "dependencies": {
429            "libentry.so": "file:../src/main/cpp/types/libentry"
430        }
431    }
432    ```
433
4342. 在使用libentry.so的模块中,对build-profile.json5进行配置:
435
436    ```json
437    {
438        "buildOption" : {
439            "arkOptions" : {
440                "runtimeOnly" : {
441                    "packages": [
442                        "libentry.so"
443                    ]
444                }
445            }
446        }
447    }
448    ```
449
4503. 用napi_load_module加载libentry.so,调用函数add:
451
452    ```cpp
453    static napi_value loadModule(napi_env env, napi_callback_info info) {
454        napi_value result;
455        // 1. 使用napi_load_module加载libentry.so
456        napi_status status = napi_load_module(env, "libentry.so", &result);
457        if (status != napi_ok) {
458            return nullptr;
459        }
460
461        napi_value addFn;
462        // 2. 使用napi_get_named_property获取add函数
463        napi_get_named_property(env, result, "add", &addFn);
464
465        napi_value a;
466        napi_value b;
467        napi_create_int32(env, 2, &a);
468        napi_create_int32(env, 3, &b);
469        napi_value args[2] = {a, b};
470        // 3. 使用napi_call_function调用函数add
471        napi_value returnValue;
472        napi_call_function(env, result, addFn, 2, args, &returnValue);
473        return result;
474    }
475    ```
476
477- **HAR加载HAR模块名**
478
479    场景为har1加载har2,har2中的Index.ets文件如下:
480
481    ```javascript
482    //har2 Index.ets
483    let value = 123;
484    function test() {
485    console.log("Hello OpenHarmony");
486    }
487    export {value, test};
488    ```
489
4901. 在har1中的oh-package.json5文件中配置dependencies项:
491
492    ```json
493    {
494        "dependencies": {
495            "har2": "file:../har2"
496        }
497    }
498    ```
499
5002. 在har1的build-profile.json5文件中进行配置:
501
502    ```json
503    {
504        "buildOption" : {
505            "arkOptions" : {
506                "runtimeOnly" : {
507                    "packages": [
508                        "har2"
509                    ]
510                }
511            }
512        }
513    }
514    ```
5153. 在har1中用napi_load_module加载har2,调用函数test以及获取变量value:
516
517    ```cpp
518    static napi_value loadModule(napi_env env, napi_callback_info info) {
519        napi_value result;
520        // 1. 使用napi_load_module加载har2
521        napi_status status = napi_load_module(env, "har2", &result);
522        if (status != napi_ok) {
523            return nullptr;
524        }
525
526        napi_value testFn;
527        // 2. 使用napi_get_named_property获取test函数
528        napi_get_named_property(env, result, "test", &testFn);
529        // 3. 使用napi_call_function调用函数test
530        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
531
532        napi_value value;
533        napi_value key;
534        std::string keyStr = "value";
535        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
536        // 4. 使用napi_get_property获取变量value
537        napi_get_property(env, result, key, &value);
538        return result;
539    }
540    ```