文档管理中心
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
指南与API参考指南NDK开发代码开发使用Node-API实现ArkTS/JS与C/C++语言交互Node-API典型使用场景使用Node-API接口在主线程中进行模块加载

使用Node-API接口在主线程中进行模块加载

场景介绍

Node-API中的napi_load_module接口的功能是在主线程中进行模块的加载,当模块加载出来之后,可以使用函数napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数。

函数说明

收起
自动换行
深色代码主题
复制
  1. napi_status napi_load_module(napi_env env, const char* path, napi_value* result);
展开
参数 说明
env 当前的虚拟机环境
path 加载的文件路径或者模块名
result 加载的模块

使用限制

  • 禁止在非主线程当中使用该接口。
  • 禁止在Init函数中使用该接口。
  • 禁止在线程安全函数的回调函数当中进行文件路径的加载。
  • 在信号函数中调用不安全,直接调用可能导致栈溢出。

建议使用napi_load_module_with_info来进行模块加载,该接口支持了更多的场景。

napi_load_module支持的场景

展开
场景 详细分类 说明
系统模块 加载@ohos.或 @system. -
本地工程模块 加载ets目录下文件中的模块 要求路径以ets开头
本地工程模块 加载模块内文件路径 要求路径以moduleName开头
本地工程模块 加载HAR模块名 -
本地工程模块 加载HSP模块名 -
远程包 加载远程HAR模块名 -
远程包 加载ohpm包名 -
模块Native库 加载Native模块(.so文件) -
  • 加载系统模块

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info)
    2. {
    3. // 1. 使用napi_load_module加载模块@ohos.hilog
    4. napi_value result;
    5. napi_status status = napi_load_module(env, "@ohos.hilog", &result);
    6. if (status != napi_ok) {
    7. return nullptr;
    8. }
    9. // 2. 使用napi_get_named_property获取info函数
    10. napi_value infoFn;
    11. status = napi_get_named_property(env, result, "info", &infoFn);
    12. if (status != napi_ok) {
    13. return nullptr;
    14. }
    15. napi_value tag;
    16. std::string formatStr = "test";
    17. napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag);
    18. napi_value outputString;
    19. std::string str = "Hello HarmonyOS";
    20. napi_create_string_utf8(env, str.c_str(), str.size(), &outputString);
    21. napi_value flag;
    22. napi_create_int32(env, 0, &flag);
    23. napi_value args[3] = {flag, tag, outputString};
    24. // 3. 使用napi_call_function调用info函数
    25. status = napi_call_function(env, result, infoFn, 3, args, nullptr);
    26. if (status != napi_ok) {
    27. return nullptr;
    28. }
    29. return result;
    30. }
  • 加载ets目录下文件中的模块

    当加载文件中的模块时,如以下ArkTS代码:

    收起
    自动换行
    深色代码主题
    复制
    1. let value = 123;
    2. function test() {
    3. console.info('Hello HarmonyOS');
    4. }
    5. export {value, test};
  1. 需要在模块的build-profile.json5文件中进行以下配置:

    收起
    自动换行
    深色代码主题
    复制
    1. "buildOption": {
    2. "arkOptions" : {
    3. "runtimeOnly" : {
    4. "sources": [
    5. "./src/main/ets/Test.ets"
    6. ],
    7. "packages": [
    8. "library",
    9. "sharedlibrary",
    10. "@ohos/hypium",
    11. "@ohos/axios",
    12. "libentry7.so"
    13. ]
    14. }
    15. },
    16. // ...
    17. },
  2. 使用napi_load_module加载Test文件,调用函数test以及获取变量value:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info)
    2. {
    3. napi_value result;
    4. // 1. 使用napi_load_module加载Test文件中的模块
    5. napi_status status = napi_load_module(env, "ets/Test", &result);
    6. if (status != napi_ok) {
    7. return nullptr;
    8. }
    9. napi_value testFn;
    10. // 2. 使用napi_get_named_property获取test函数
    11. status = napi_get_named_property(env, result, "test", &testFn);
    12. if (status != napi_ok) {
    13. return nullptr;
    14. }
    15. // 3. 使用napi_call_function调用函数test
    16. status = napi_call_function(env, result, testFn, 0, nullptr, nullptr);
    17. if (status != napi_ok) {
    18. return nullptr;
    19. }
    20. napi_value value;
    21. napi_value key;
    22. std::string keyStr = "value";
    23. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    24. // 4. 使用napi_get_property获取变量value
    25. status = napi_get_property(env, result, key, &value);
    26. if (status != napi_ok) {
    27. return nullptr;
    28. }
    29. return result;
    30. }
  • 加载模块内文件路径

    当加载文件中的模块时,如以下ArkTS代码:

    收起
    自动换行
    深色代码主题
    复制
    1. //./src/main/ets/Test.ets
    2. let value = 123;
    3. function test() {
    4. console.info("Hello HarmonyOS");
    5. }
    6. export {value, test};
  1. 需要在当前模块的build-profile.json5文件中进行以下配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "sources": [
    6. "./src/main/ets/Test.ets"
    7. ]
    8. }
    9. }
    10. }
    11. }
  2. 使用napi_load_module加载Test文件,调用函数test以及获取变量value:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载Test文件中的模块
    4. napi_status status = napi_load_module(env, "entry/src/main/ets/Test", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value testFn;
    9. // 2. 使用napi_get_named_property获取test函数
    10. status = napi_get_named_property(env, result, "test", &testFn);
    11. if (status != napi_ok) {
    12. return nullptr;
    13. }
    14. // 3. 使用napi_call_function调用函数test
    15. status = napi_call_function(env, result, testFn, 0, nullptr, nullptr);
    16. if (status != napi_ok) {
    17. return nullptr;
    18. }
    19. napi_value value;
    20. napi_value key;
    21. std::string keyStr = "value";
    22. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    23. // 4. 使用napi_get_property获取变量value
    24. status = napi_get_property(env, result, key, &value);
    25. if (status != napi_ok) {
    26. return nullptr;
    27. }
    28. return result;
    29. }
  • 加载HAR模块名

    HAR包Index.ets文件如下:

    收起
    自动换行
    深色代码主题
    复制
    1. //library Index.ets
    2. let value = 123;
    3. function test() {
    4. console.info("Hello HarmonyOS");
    5. }
    6. export {value, test};
  1. 在当前模块下的oh-package.json5文件中配置dependencies项:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "dependencies": {
    3. "library": "file:../library"
    4. }
    5. }
  2. 在使用library的模块中,对build-profile.json5进行配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "packages": [
    6. "library"
    7. ]
    8. }
    9. }
    10. }
    11. }
  3. 用napi_load_module加载library,调用函数test以及获取变量value:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载library
    4. napi_status status = napi_load_module(env, "library", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value testFn;
    9. // 2. 使用napi_get_named_property获取test函数
    10. status = napi_get_named_property(env, result, "test", &testFn);
    11. if (status != napi_ok) {
    12. return nullptr;
    13. }
    14. // 3. 使用napi_call_function调用函数test
    15. status = napi_call_function(env, result, testFn, 0, nullptr, nullptr);
    16. if (status != napi_ok) {
    17. return nullptr;
    18. }
    19. napi_value value;
    20. napi_value key;
    21. std::string keyStr = "value";
    22. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    23. // 4. 使用napi_get_property获取变量value
    24. status = napi_get_property(env, result, key, &value);
    25. if (status != napi_ok) {
    26. return nullptr;
    27. }
    28. return result;
    29. }
  • 加载HSP模块名

    HSP包Index.ets文件如下:

    收起
    自动换行
    深色代码主题
    复制
    1. //sharedlibrary Index.ets
    2. let value = 123;
    3. function test() {
    4. console.info("Hello HarmonyOS");
    5. }
    6. export {value, test};
  1. 在当前模块下的oh-package.json5文件中配置dependencies项:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "dependencies": {
    3. "sharedlibrary": "file:../sharedlibrary"
    4. }
    5. }
  2. 在使用library的模块中,对build-profile.json5进行配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "packages": [
    6. "sharedlibrary"
    7. ]
    8. }
    9. }
    10. }
    11. }
  3. 用napi_load_module加载sharedlibrary,调用函数test以及获取变量value:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载sharedlibrary
    4. napi_status status = napi_load_module(env, "sharedlibrary", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value testFn;
    9. // 2. 使用napi_get_named_property获取test函数
    10. status = napi_get_named_property(env, result, "test", &testFn);
    11. if (status != napi_ok) {
    12. return nullptr;
    13. }
    14. // 3. 使用napi_call_function调用函数test
    15. status = napi_call_function(env, result, testFn, 0, nullptr, nullptr);
    16. if (status != napi_ok) {
    17. return nullptr;
    18. }
    19. napi_value value;
    20. napi_value key;
    21. std::string keyStr = "value";
    22. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    23. // 4. 使用napi_get_property获取变量value
    24. status = napi_get_property(env, result, key, &value);
    25. if (status != napi_ok) {
    26. return nullptr;
    27. }
    28. return result;
    29. }
  • 加载远程HAR模块名
  1. 在当前模块下的oh-package.json5文件中配置dependencies项:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "dependencies": {
    3. "@ohos/hypium": "1.0.16"
    4. }
    5. }
  2. 在使用@ohos/hypium的模块中,对build-profile.json5进行配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "packages": [
    6. "@ohos/hypium"
    7. ]
    8. }
    9. }
    10. }
    11. }
  3. 用napi_load_module加载@ohos/hypium,获取DEFAULT变量:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载@ohos/hypium
    4. napi_status status = napi_load_module(env, "@ohos/hypium", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value key;
    9. std::string keyStr = "DEFAULT";
    10. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    11. // 2. 使用napi_get_property获取DEFAULT变量
    12. napi_value defaultValue;
    13. status = napi_get_property(env, result, key, &defaultValue);
    14. if (status != napi_ok) {
    15. return nullptr;
    16. }
    17. return result;
    18. }
  • 加载ohpm包名
  1. 在当前模块下的oh-package.json5文件中配置dependencies项:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "dependencies": {
    3. "@ohos/axios": "2.2.4",
    4. }
    5. }
  2. 在使用@ohos/axios的模块中,对build-profile.json5进行配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "packages": [
    6. "@ohos/axios"
    7. ]
    8. }
    9. }
    10. }
    11. }
  3. 用napi_load_module加载@ohos/axios,获取VERSION变量:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载@ohos/axios
    4. napi_status status = napi_load_module(env, "@ohos/axios", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value key;
    9. std::string keyStr = "VERSION";
    10. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    11. // 2. 使用napi_get_property获取VERSION
    12. napi_value defaultValue;
    13. status = napi_get_property(env, result, key, &defaultValue);
    14. if (status != napi_ok) {
    15. return nullptr;
    16. }
    17. return result;
    18. }
  • 加载Native库

    libentry.so的index.d.ts文件如下:

    收起
    自动换行
    深色代码主题
    复制
    1. //index.d.ts
    2. export const add: (a: number, b: number) => number;
  1. 在当前模块下的oh-package.json5文件中配置dependencies项:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "dependencies": {
    3. "libentry.so": "file:../src/main/cpp/types/libentry"
    4. }
    5. }
  2. 在使用libentry.so的模块中,对build-profile.json5进行配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "packages": [
    6. "libentry.so"
    7. ]
    8. }
    9. }
    10. }
    11. }
  3. 用napi_load_module加载libentry.so,调用函数add:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载libentry.so
    4. napi_status status = napi_load_module(env, "libentry.so", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value addFn;
    9. // 2. 使用napi_get_named_property获取add函数
    10. status = napi_get_named_property(env, result, "add", &addFn);
    11. if (status != napi_ok) {
    12. return nullptr;
    13. }
    14. napi_value a;
    15. napi_value b;
    16. napi_create_int32(env, 2, &a);
    17. napi_create_int32(env, 3, &b);
    18. napi_value args[2] = {a, b};
    19. // 3. 使用napi_call_function调用函数add
    20. napi_value returnValue;
    21. status = napi_call_function(env, result, addFn, 2, args, &returnValue);
    22. if (status != napi_ok) {
    23. return nullptr;
    24. }
    25. return result;
    26. }
  • HAR加载HAR模块名

    场景为har1加载har2,har2中的Index.ets文件如下:

    收起
    自动换行
    深色代码主题
    复制
    1. //har2 Index.ets
    2. let value = 123;
    3. function test() {
    4. console.info("Hello HarmonyOS");
    5. }
    6. export {value, test};
  1. 在har1中的oh-package.json5文件中配置dependencies项:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "dependencies": {
    3. "har2": "file:../har2"
    4. }
    5. }
  2. 在har1的build-profile.json5文件中进行配置:

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "packages": [
    6. "har2"
    7. ]
    8. }
    9. }
    10. }
    11. }
  3. 在har1中用napi_load_module加载har2,调用函数test以及获取变量value:

    收起
    自动换行
    深色代码主题
    复制
    1. static napi_value loadModule(napi_env env, napi_callback_info info) {
    2. napi_value result;
    3. // 1. 使用napi_load_module加载har2
    4. napi_status status = napi_load_module(env, "har2", &result);
    5. if (status != napi_ok) {
    6. return nullptr;
    7. }
    8. napi_value testFn;
    9. // 2. 使用napi_get_named_property获取test函数
    10. status = napi_get_named_property(env, result, "test", &testFn);
    11. if (status != napi_ok) {
    12. return nullptr;
    13. }
    14. // 3. 使用napi_call_function调用函数test
    15. status = napi_call_function(env, result, testFn, 0, nullptr, nullptr);
    16. if (status != napi_ok) {
    17. return nullptr;
    18. }
    19. napi_value value;
    20. napi_value key;
    21. std::string keyStr = "value";
    22. napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
    23. // 4. 使用napi_get_property获取变量value
    24. status = napi_get_property(env, result, key, &value);
    25. if (status != napi_ok) {
    26. return nullptr;
    27. }
    28. return result;
    29. }