1# 使用Node-API接口创建ArkTS运行时环境 2<!--Kit: NDK--> 3<!--Subsystem: arkcompiler--> 4<!--Owner: @xliu-huanwei; @shilei123; @huanghello--> 5<!--Designer: @shilei123--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @fang-jinxu--> 8 9## 场景介绍 10 11开发者通过pthread_create创建新线程后,可以通过`napi_create_ark_runtime`来创建一个新的ArkTS基础运行时环境,并通过该运行时环境加载ArkTS模块。当使用结束后,开发者需要通过`napi_destroy_ark_runtime`来销毁所创建的ArkTS基础运行时环境。 12 13## 约束限制 14 15一个进程最多只能创建64个运行时环境。 16 17## 使用示例 18 191. 接口声明、编译配置以及模块注册。 20 21 **接口声明** 22 23 ```ts 24 // index.d.ts 25 export const createArkRuntime: () => object; 26 ``` 27 <!-- @[napi_ark_runtime_dts](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/NodeAPI/NodeAPIClassicUseCases/NodeAPIApplicationScenario/entry/src/main/cpp/types/libentry/Index.d.ts) --> 28 29 **编译配置** 30 31 ``` 32 // CMakeLists.txt 33 # the minimum version of CMake. 34 cmake_minimum_required(VERSION 3.4.1) 35 project(MyApplication) 36 37 set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 38 39 include_directories(${NATIVERENDER_ROOT_PATH} 40 ${NATIVERENDER_ROOT_PATH}/include) 41 add_library(entry SHARED create_ark_runtime.cpp) 42 target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so) 43 ``` 44 45 在当前模块的build-profile.json5文件中进行以下配置: 46 ```json 47 { 48 "buildOption" : { 49 "arkOptions" : { 50 "runtimeOnly" : { 51 "sources": [ 52 "./src/main/ets/pages/ObjectUtils.ets" 53 ] 54 } 55 } 56 } 57 } 58 ``` 59 <!-- @[napi_ark_runtime_build](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/NodeAPI/NodeAPIClassicUseCases/NodeAPIApplicationScenario/entry/build-profile.json5) --> 60 61 **模块注册** 62 63 ```cpp 64 // create_ark_runtime.cpp 65 EXTERN_C_START 66 static napi_value Init(napi_env env, napi_value exports) 67 { 68 napi_property_descriptor desc[] = { 69 { "createArkRuntime", nullptr, CreateArkRuntime, nullptr, nullptr, nullptr, napi_default, nullptr } 70 }; 71 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 72 return exports; 73 } 74 EXTERN_C_END 75 76 static napi_module nativeModule = { 77 .nm_version = 1, 78 .nm_flags = 0, 79 .nm_filename = nullptr, 80 .nm_register_func = Init, 81 .nm_modname = "entry", 82 .nm_priv = nullptr, 83 .reserved = { 0 }, 84 }; 85 86 extern "C" __attribute__((constructor)) void RegisterQueueWorkModule() 87 { 88 napi_module_register(&nativeModule); 89 } 90 ``` 91 <!-- @[napi_ark_runtime_cpp](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/NodeAPI/NodeAPIClassicUseCases/NodeAPIApplicationScenario/entry/src/main/cpp/napi_init.cpp) --> 92 932. 新建线程并创建ArkTS基础运行时环境,加载自定义模块请参考[napi_load_module_with_info](./use-napi-load-module-with-info.md)。 94 95 ```cpp 96 // create_ark_runtime.cpp 97 #include <pthread.h> 98 #include "napi/native_api.h" 99 100 static void *CreateArkRuntimeFunc(void *arg) 101 { 102 // 1. 创建基础运行环境 103 napi_env env; 104 napi_status ret = napi_create_ark_runtime(&env); 105 if (ret != napi_ok) { 106 return nullptr; 107 } 108 109 // 2. 加载自定义模块 110 napi_value objUtils; 111 ret = napi_load_module_with_info(env, "entry/src/main/ets/pages/ObjectUtils", "com.example.myapplication/entry", &objUtils); 112 if (ret != napi_ok) { 113 return nullptr; 114 } 115 116 // 3. 使用ArkTS中的logger 117 napi_value logger; 118 ret = napi_get_named_property(env, objUtils, "Logger", &logger); 119 if (ret != napi_ok) { 120 return nullptr; 121 } 122 ret = napi_call_function(env, objUtils, logger, 0, nullptr, nullptr); 123 124 // 4. 销毁ArkTS环境 125 ret = napi_destroy_ark_runtime(&env); 126 return nullptr; 127 } 128 129 static napi_value CreateArkRuntime(napi_env env, napi_callback_info info) 130 { 131 pthread_t tid; 132 pthread_create(&tid, nullptr, CreateArkRuntimeFunc, nullptr); 133 pthread_join(tid, nullptr); 134 return nullptr; 135 } 136 ``` 137 <!-- @[napi_ark_runtime_cpp](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/NodeAPI/NodeAPIClassicUseCases/NodeAPIApplicationScenario/entry/src/main/cpp/napi_init.cpp) --> 138 1393. 编写ArkTS侧示例代码。 140 141 ```ts 142 // ObjectUtils.ets 143 export function Logger() { 144 console.info("print log"); 145 } 146 ``` 147 <!-- @[napi_ark_runtime_utils](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/NodeAPI/NodeAPIClassicUseCases/NodeAPIApplicationScenario/entry/src/main/ets/pages/ObjectUtils.ets) --> 148 ```ts 149 // ArkTS侧调用接口 150 import testNapi from 'libentry.so'; 151 152 testNapi.createArkRuntime(); 153 ``` 154 <!-- @[napi_ark_runtime_ets](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/ArkTS/NodeAPI/NodeAPIClassicUseCases/NodeAPIApplicationScenario/entry/src/main/ets/pages/Index.ets) --> 155