1e41f4b71Sopenharmony_ci# Creating an ArkTs Runtime Environment Using Node-API 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciAfter creating a thread using **pthread_create**, you can use **napi_create_ark_runtime** to create an ArkTS runtime environment and load the ArkTS module in the runtime environment. To destroy an ArkTS runtime environment that is not required, use **napi_destroy_ark_runtime**. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Constraints 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciA maximum of 16 runtime environments can be created for a process. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Example 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci1. Declare the APIs, configure compile settings, and register the module. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci **Declare the APIs.** 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci ```ts 18e41f4b71Sopenharmony_ci // index.d.ts 19e41f4b71Sopenharmony_ci export const createArkRuntime: () => object; 20e41f4b71Sopenharmony_ci ``` 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci **Configure compile settings.** 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci ``` 25e41f4b71Sopenharmony_ci // CMakeLists.txt 26e41f4b71Sopenharmony_ci # the minimum version of CMake. 27e41f4b71Sopenharmony_ci cmake_minimum_required(VERSION 3.4.1) 28e41f4b71Sopenharmony_ci project(MyApplication) 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci include_directories(${NATIVERENDER_ROOT_PATH} 33e41f4b71Sopenharmony_ci ${NATIVERENDER_ROOT_PATH}/include) 34e41f4b71Sopenharmony_ci add_library(entry SHARED create_ark_runtime.cpp) 35e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so) 36e41f4b71Sopenharmony_ci ``` 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci **Register modules.** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci ```cpp 41e41f4b71Sopenharmony_ci // create_ark_runtime.cpp 42e41f4b71Sopenharmony_ci EXTERN_C_START 43e41f4b71Sopenharmony_ci static napi_value Init(napi_env env, napi_value exports) 44e41f4b71Sopenharmony_ci { 45e41f4b71Sopenharmony_ci napi_property_descriptor desc[] = { 46e41f4b71Sopenharmony_ci { "createArkRuntime", nullptr, CreateArkRuntime, nullptr, nullptr, nullptr, napi_default, nullptr } 47e41f4b71Sopenharmony_ci }; 48e41f4b71Sopenharmony_ci napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 49e41f4b71Sopenharmony_ci return exports; 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci EXTERN_C_END 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci static napi_module nativeModule = { 54e41f4b71Sopenharmony_ci .nm_version = 1, 55e41f4b71Sopenharmony_ci .nm_flags = 0, 56e41f4b71Sopenharmony_ci .nm_filename = nullptr, 57e41f4b71Sopenharmony_ci .nm_register_func = Init, 58e41f4b71Sopenharmony_ci .nm_modname = "entry", 59e41f4b71Sopenharmony_ci .nm_priv = nullptr, 60e41f4b71Sopenharmony_ci .reserved = { 0 }, 61e41f4b71Sopenharmony_ci }; 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci extern "C" __attribute__((constructor)) void RegisterQueueWorkModule() 64e41f4b71Sopenharmony_ci { 65e41f4b71Sopenharmony_ci napi_module_register(&nativeModule); 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci ``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci2. Create a thread and an ArkTS runtime environment, and load the module. For details about how to load a custom module, see [Loading a Module Using Node-API](use-napi-load-module-with-info.md). 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci ```cpp 72e41f4b71Sopenharmony_ci // create_ark_runtime.cpp 73e41f4b71Sopenharmony_ci #include <pthread.h> 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci #include "napi/native_api.h" 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci static void *CreateArkRuntimeFunc(void *arg) 78e41f4b71Sopenharmony_ci { 79e41f4b71Sopenharmony_ci // 1. Create the ArkTS runtime environment. 80e41f4b71Sopenharmony_ci napi_env env; 81e41f4b71Sopenharmony_ci napi_status ret = napi_create_ark_runtime(&env); 82e41f4b71Sopenharmony_ci if (ret != napi_ok) { 83e41f4b71Sopenharmony_ci return nullptr; 84e41f4b71Sopenharmony_ci } 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci // 2. Load custom modules. 87e41f4b71Sopenharmony_ci napi_value objUtils; 88e41f4b71Sopenharmony_ci ret = napi_load_module_with_info(env, "entry/src/main/ets/pages/ObjectUtils", "com.example.myapplication/entry", &objUtils); 89e41f4b71Sopenharmony_ci if (ret != napi_ok) { 90e41f4b71Sopenharmony_ci return nullptr; 91e41f4b71Sopenharmony_ci } 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci // 3. Use the logger in ArkTS. 94e41f4b71Sopenharmony_ci napi_value logger; 95e41f4b71Sopenharmony_ci ret = napi_get_named_property(env, objUtils, "Logger", &logger); 96e41f4b71Sopenharmony_ci if (ret != napi_ok) { 97e41f4b71Sopenharmony_ci return nullptr; 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci ret = napi_call_function(env, objUtils, logger, 0, nullptr, nullptr); 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci // 4. Destroy the ArkTS runtime environment. 102e41f4b71Sopenharmony_ci ret = napi_destroy_ark_runtime(&env); 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci return nullptr; 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci static napi_value CreateArkRuntime(napi_env env, napi_callback_info info) 109e41f4b71Sopenharmony_ci { 110e41f4b71Sopenharmony_ci pthread_t tid; 111e41f4b71Sopenharmony_ci pthread_create(&tid, nullptr, CreateArkRuntimeFunc, nullptr); 112e41f4b71Sopenharmony_ci pthread_join(tid, nullptr); 113e41f4b71Sopenharmony_ci return nullptr; 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci ``` 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci3. Write the ArkTS code. 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci ```ts 120e41f4b71Sopenharmony_ci // ObjectUtils.ets 121e41f4b71Sopenharmony_ci export function Logger() { 122e41f4b71Sopenharmony_ci console.log("print log"); 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci ``` 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci 127