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