文档管理中心
指南NDK开发代码开发使用Node-API实现ArkTS/JS与C/C++语言交互Node-API典型使用场景使用扩展的Node-API接口在当前线程中创建、切换和销毁上下文环境

使用扩展的Node-API接口在当前线程中创建、切换和销毁上下文环境

在应用被拉起时,应用的主线程即为一个ArkTS线程,该线程中存在一个由系统管理的上下文环境,当ArkTS需要和C/C++交互时,在C/C++侧,napi_env即代表该上下文环境,每个上下文环境中存在着独立的globalThis对象。

开发者可以通过使用Node-API中的扩展接口napi_create_ark_context和napi_destroy_ark_context在当前线程中创建和销毁新的上下文环境,这些新创建的上下文环境和线程中原始的上下文环境共用一个运行时虚拟机。

需要注意的是napi_create_ark_context接口仅仅是创建新的上下文环境,而不是创建一个新的运行时,同时通过该接口创建上下文环境,需要通过napi_destroy_ark_context接口销毁,否则会造成内存泄漏。

当然ArkTS线程的原始上下文环境不能通过napi_destroy_ark_context接口销毁。

当需要切换到指定的上下文环境时,可以调用Node-API中的扩展接口napi_switch_ark_context来切换到指定的上下文环境。

开发者可以在一个新的上下文环境中访问globalThis上的某些属性方法,也可以在访问完之后,切回到原先的上下文环境保证上下文环境的隔离。

场景介绍

开发者可以通过napi_create_ark_context接口在当前的线程中创建新的上下文环境,该上下文环境拥有独立的globalThis对象。

这意味着当前线程上原始的上下文环境和新创建的上下文环境是相互隔离的,即上下文环境中的globalThis对象不同。

开发者可以通过新创建的上下文环境进行模块化加载,加载生成的模块化对象将挂载在当前上下文环境中的globalThis对象上,不同的模块加载在不同的上下文环境中,可以避免一个模块对globalThis对象上的属性的修改影响到另一个模块对globalThis对象上属性的访问。

当然部分的Node-API标准接口和扩展接口也进行上下文的适配,这意味着调用这些Node-API的接口,上下文环境会根据接口入参的napi_env来主动进行上下文的切换动作,开发者不需要主动调用napi_switch_ark_context来进行上下文环境的切换。

需要注意的是从C++/C侧回到ArkTS侧时,要主动调用napi_switch_ark_context接口将上下文环境切回到对应的上下文环境,否则会造成ArkTS侧的代码执行在另外一个上下文环境导致不可预知的稳定性问题。

支持多运行时上下文环境调用的NAPI接口

下列表格中的NAPI接口都支持在多上下文的环境中执行,其中部分NAPI接口会主动进行上下文的切换。

即使调用方没有主动调用napi_switch_ark_context来切换运行时上下文,这部分接口仍然可以通过比较当前运行的上下文环境和接口指定的运行时环境是否一致来决定是否切换上下文环境。

如果上下文环境不一致,这些NAPI接口会将当前的运行时环境切换成接口参数指定的上下文环境。

当然不涉及主动切换上下文环境的接口意味着这部分接口和运行时上下文无关,使用任意一个有效的上下文环境都能正常执行。

展开
接口 是否会主动进行上下文切换
napi_module_register
napi_fatal_error
napi_async_init
napi_async_destroy
napi_make_callback
napi_create_buffer
napi_create_external_buffer
napi_create_buffer_copy
napi_is_buffer
napi_get_buffer_info
napi_create_async_work
napi_delete_async_work
napi_queue_async_work
napi_cancel_async_work
napi_get_node_version
napi_fatal_exception
napi_add_env_cleanup_hook
napi_remove_env_cleanup_hook
napi_open_callback_scope
napi_close_callback_scope
napi_create_threadsafe_function
napi_get_threadsafe_function_context
napi_call_threadsafe_function
napi_acquire_threadsafe_function
napi_release_threadsafe_function
napi_unref_threadsafe_function
napi_ref_threadsafe_function
napi_add_async_cleanup_hook
napi_remove_async_cleanup_hook
node_api_get_module_file_name
napi_get_last_error_info
napi_get_undefined
napi_get_null
napi_get_global
napi_get_boolean
napi_create_object
napi_create_array
napi_create_array_with_length
napi_create_double
napi_create_int32
napi_create_uint32
napi_create_int64
napi_create_string_latin1
napi_create_string_utf8
napi_create_string_utf16
napi_create_external_string_ascii
napi_create_external_string_utf16
napi_create_symbol
napi_create_function
napi_create_error
napi_create_type_error
napi_create_range_error
napi_typeof
napi_get_value_double
napi_get_value_int32
napi_get_value_uint32
napi_get_value_int64
napi_get_value_bool
napi_get_value_string_latin1
napi_get_value_string_utf8
napi_get_value_string_utf16
napi_coerce_to_bool
napi_coerce_to_number
napi_coerce_to_object
napi_coerce_to_string
napi_get_prototype
napi_get_property_names
napi_set_property
napi_has_property
napi_get_property
napi_delete_property
napi_has_own_property
napi_set_named_property
napi_has_named_property
napi_get_named_property
napi_set_element
napi_has_element
napi_get_element
napi_delete_element
napi_define_properties
napi_is_array
napi_get_array_length
napi_strict_equals
napi_call_function
napi_new_instance
napi_instanceof
napi_get_cb_info
napi_get_new_target
napi_define_class
napi_wrap
napi_unwrap
napi_remove_wrap
napi_create_external
napi_get_value_external
napi_create_reference
napi_delete_reference
napi_reference_ref
napi_reference_unref
napi_get_reference_value
napi_open_handle_scope
napi_close_handle_scope
napi_open_escapable_handle_scope
napi_close_escapable_handle_scope
napi_escape_handle
napi_throw
napi_throw_error
napi_throw_type_error
napi_throw_range_error
napi_is_error
napi_is_exception_pending
napi_get_and_clear_last_exception
napi_is_arraybuffer
napi_create_arraybuffer
napi_create_external_arraybuffer
napi_get_arraybuffer_info
napi_is_typedarray
napi_create_typedarray
napi_get_typedarray_info
napi_create_dataview
napi_is_dataview
napi_get_dataview_info
napi_get_version
napi_create_promise
napi_resolve_deferred
napi_reject_deferred
napi_is_promise
napi_run_script
napi_create_date
napi_is_date
napi_get_date_value
napi_add_finalizer
napi_create_bigint_int64
napi_create_bigint_uint64
napi_create_bigint_words
napi_get_value_bigint_int64
napi_get_value_bigint_uint64
napi_get_value_bigint_words
napi_get_all_property_names
napi_set_instance_data
napi_get_instance_data
napi_detach_arraybuffer
napi_is_detached_arraybuffer
napi_type_tag_object
napi_check_object_type_tag
napi_object_freeze
napi_object_seal
napi_run_script_path
napi_queue_async_work_with_qos
napi_load_module
napi_create_object_with_properties
napi_create_object_with_named_properties
napi_coerce_to_native_binding_object
napi_load_module_with_info
napi_create_ark_runtime
napi_destroy_ark_runtime
napi_serialize
napi_deserialize
napi_delete_serialization_data
napi_call_threadsafe_function_with_priority
napi_wrap_enhance
napi_open_critical_scope
napi_close_critical_scope
napi_get_buffer_string_utf16_in_critical_scope
napi_create_strong_reference
napi_delete_strong_reference
napi_get_strong_reference_value
napi_throw_business_error

不支持多运行时上下文环境调用的NAPI接口

展开
接口 多运行时上下文环境调用返回值
napi_define_sendable_class napi_invalid_arg
napi_is_sendable napi_invalid_arg
napi_create_sendable_object_with_properties napi_invalid_arg
napi_wrap_sendable napi_invalid_arg
napi_wrap_sendable_with_size napi_invalid_arg
napi_unwrap_sendable napi_invalid_arg
napi_remove_wrap_sendable napi_invalid_arg
napi_create_sendable_array napi_invalid_arg
napi_create_sendable_array_with_length napi_invalid_arg
napi_create_sendable_arraybuffer napi_invalid_arg
napi_create_sendable_typedarray napi_invalid_arg
napi_run_event_loop napi_invalid_arg
napi_stop_event_loop napi_invalid_arg
napi_get_uv_event_loop napi_invalid_arg
napi_create_strong_sendable_reference napi_invalid_arg
napi_delete_strong_sendable_reference napi_invalid_arg
napi_get_strong_sendable_reference_value napi_invalid_arg

示例代码

  • 模块注册

    收起
    自动换行
    深色代码主题
    复制
    1. // napi_init.cpp
    2. #include "napi/native_api.h"
    3. #include "hilog/log.h"
    4. static napi_value NAPI_Global_callFunctionInContext(napi_env env, napi_callback_info info)
    5. {
    6. napi_status status = napi_ok;
    7. size_t argc = 1;
    8. napi_value args[1] = {nullptr};
    9. if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) {
    10. return nullptr;
    11. }
    12. // 在原始上下文中加载模块plugin1.ets
    13. napi_value plugin1 = nullptr;
    14. status = napi_load_module_with_info(env, "entry/src/main/ets/pages/plugin1", "com.example.myapplication/entry", &plugin1);
    15. if (status != napi_ok) {
    16. OH_LOG_INFO(LOG_APP, "load plugin1 failed");
    17. }
    18. // 获取模块plugin1中的方法GetLocation
    19. napi_value getLocation1 = nullptr;
    20. status = napi_get_named_property(env, plugin1, "GetLocation", &getLocation1);
    21. if (status != napi_ok) {
    22. OH_LOG_INFO(LOG_APP, "obtain GetLocation from plugin1 failed");
    23. }
    24. // 创建新的上下文环境newEnv2
    25. napi_env newEnv2 = nullptr;
    26. status = napi_create_ark_context(env, &newEnv2);
    27. if (status != napi_ok) {
    28. return nullptr;
    29. }
    30. // 主动切换到新的上下文环境newEnv2
    31. status = napi_switch_ark_context(newEnv2);
    32. if (status != napi_ok) {
    33. OH_LOG_INFO(LOG_APP, "switch to newEnv2 failed");
    34. }
    35. napi_value plugin2 = nullptr;
    36. // 在新的上下文环境中加载模块plugin2.ets
    37. status = napi_load_module_with_info(newEnv2, "entry/src/main/ets/pages/plugin2", "com.example.myapplication/entry",
    38. &plugin2);
    39. if (status != napi_ok) {
    40. OH_LOG_INFO(LOG_APP, "load plugin2 failed");
    41. }
    42. napi_value getLocation2 = nullptr;
    43. status = napi_get_named_property(newEnv2, plugin2, "GetLocation", &getLocation2);
    44. if (status != napi_ok) {
    45. OH_LOG_INFO(LOG_APP, "obtain GetLocation from plugin2 failed");
    46. }
    47. // 在新上下文环境中执行ArkTS侧的方法getLocation, 入参为模块plugin2中的方法GetLocation
    48. napi_value result = nullptr;
    49. napi_value args2[1] = {};
    50. args2[0] = getLocation2;
    51. status = napi_call_function(newEnv2, nullptr, args[0], 1, args2, &result);
    52. if (status != napi_ok) {
    53. OH_LOG_INFO(LOG_APP, "call function of env failed");
    54. }
    55. int32_t ret = 0;
    56. status = napi_get_value_int32(newEnv2, result, &ret);
    57. if (status != napi_ok) {
    58. OH_LOG_INFO(LOG_APP, "napi_get_value_int32 of env failed");
    59. } else {
    60. // plugin2的上下文中globalThis.a为3000
    61. OH_LOG_INFO(LOG_APP, "ret is %{public}d", ret); // 3000
    62. }
    63. // 主动切回原始上下文环境env
    64. status = napi_switch_ark_context(env);
    65. if (status != napi_ok) {
    66. OH_LOG_INFO(LOG_APP, "switch to env failed");
    67. }
    68. args2[0] = getLocation1;
    69. status = napi_call_function(env, nullptr, args[0], 1, args2, &result);
    70. if (status != napi_ok) {
    71. return nullptr;
    72. }
    73. // 获取GetLocation接口调用之后的返回值
    74. ret = 0;
    75. status = napi_get_value_int32(env, result, &ret);
    76. if (status != napi_ok) {
    77. return nullptr;
    78. } else {
    79. // plugin1的上下文中globalThis.a为2000
    80. OH_LOG_INFO(LOG_APP, "ret is %{public}d", ret); // 2000
    81. }
    82. // 销毁创建的上下文环境
    83. status = napi_destroy_ark_context(newEnv2);
    84. if (status != napi_ok) {
    85. return nullptr;
    86. }
    87. return result;
    88. }
    89. // 模块注册
    90. EXTERN_C_START
    91. static napi_value Init(napi_env env, napi_value exports) {
    92. napi_property_descriptor desc[] = {
    93. {"callFunctionInContext", nullptr, NAPI_Global_callFunctionInContext,
    94. nullptr, nullptr, nullptr, napi_default, nullptr}
    95. };
    96. napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    97. return exports;
    98. }
    99. EXTERN_C_END
    100. static napi_module demoModule = {
    101. .nm_version = 1,
    102. .nm_flags = 0,
    103. .nm_filename = nullptr,
    104. .nm_register_func = Init,
    105. .nm_modname = "entry",
    106. .nm_priv = ((void*)0),
    107. .reserved = { 0 },
    108. };
    109. extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
    110. {
    111. napi_module_register(&demoModule);
    112. }
  • 接口声明

    收起
    自动换行
    深色代码主题
    复制
    1. // index.d.ts
    2. export const callFunctionInContext: (func: (func:()=>number)=>{}) => number;
  • 编译配置

  1. CMakeLists.txt文件需要按照如下配置

    收起
    自动换行
    深色代码主题
    复制
    1. // CMakeLists.txt
    2. # the minimum version of CMake.
    3. cmake_minimum_required(VERSION 3.5.0)
    4. project(MyApplication8)
    5. set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
    6. if(DEFINED PACKAGE_FIND_FILE)
    7. include(${PACKAGE_FIND_FILE})
    8. endif()
    9. add_definitions( "-DLOG_DOMAIN=0xd0d0" )
    10. add_definitions( "-DLOG_TAG=\"testTag\"")
    11. include_directories(${NATIVERENDER_ROOT_PATH}
    12. ${NATIVERENDER_ROOT_PATH}/include)
    13. add_library(entry SHARED napi_init.cpp)
    14. target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so)
  2. 需要在工程的build-profile.json5文件中进行以下配置

    收起
    自动换行
    深色代码主题
    复制
    1. {
    2. "buildOption" : {
    3. "arkOptions" : {
    4. "runtimeOnly" : {
    5. "sources": [
    6. "./src/main/ets/pages/plugin1.ets",
    7. "./src/main/ets/pages/plugin2.ets"
    8. ]
    9. }
    10. }
    11. }
    12. }
  • ArkTS代码示例
    收起
    自动换行
    深色代码主题
    复制
    1. // index.ets
    2. import testNapi from "libentry.so"
    3. // 该接口用于执行模块plugin1或plugin2中的GetLocation方法
    4. function getLocation(func: () => number) {
    5. return func();
    6. }
    7. testNapi.callFunctionInContext(getLocation)
    收起
    自动换行
    深色代码主题
    复制
    1. // ets/pages/plugin1.ets
    2. globalThis.a = 2000;
    3. export function GetLocation() : number {
    4. return globalThis.a;
    5. }
    收起
    自动换行
    深色代码主题
    复制
    1. // ets/pages/plugin2.ets
    2. globalThis.a = 3000;
    3. export function GetLocation() : number {
    4. return globalThis.a;
    5. }
在 指南 中进行搜索
请输入您想要搜索的关键词