• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 64 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   Configure the **build-profile.json5** file of the current module as follows:
39   ```json
40   {
41       "buildOption" : {
42           "arkOptions" : {
43               "runtimeOnly" : {
44                   "sources": [
45                       "./src/main/ets/pages/ObjectUtils.ets"
46                   ]
47               }
48           }
49       }
50   }
51   ```
52
53   **Register modules.**
54
55   ```cpp
56   // create_ark_runtime.cpp
57   EXTERN_C_START
58   static napi_value Init(napi_env env, napi_value exports)
59   {
60       napi_property_descriptor desc[] = {
61           { "createArkRuntime", nullptr, CreateArkRuntime, nullptr, nullptr, nullptr, napi_default, nullptr }
62       };
63       napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
64       return exports;
65   }
66   EXTERN_C_END
67
68   static napi_module nativeModule = {
69       .nm_version = 1,
70       .nm_flags = 0,
71       .nm_filename = nullptr,
72       .nm_register_func = Init,
73       .nm_modname = "entry",
74       .nm_priv = nullptr,
75       .reserved = { 0 },
76   };
77
78   extern "C" __attribute__((constructor)) void RegisterQueueWorkModule()
79   {
80       napi_module_register(&nativeModule);
81   }
82   ```
83
842. 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).
85
86   ```cpp
87   // create_ark_runtime.cpp
88   #include <pthread.h>
89   #include "napi/native_api.h"
90
91   static void *CreateArkRuntimeFunc(void *arg)
92   {
93       // 1. Create the ArkTS runtime environment.
94       napi_env env;
95       napi_status ret = napi_create_ark_runtime(&env);
96       if (ret != napi_ok) {
97           return nullptr;
98       }
99
100       // 2. Load custom modules.
101       napi_value objUtils;
102       ret = napi_load_module_with_info(env, "entry/src/main/ets/pages/ObjectUtils", "com.example.myapplication/entry", &objUtils);
103       if (ret != napi_ok) {
104           return nullptr;
105       }
106
107       // 3. Use the logger in ArkTS.
108       napi_value logger;
109       ret = napi_get_named_property(env, objUtils, "Logger", &logger);
110       if (ret != napi_ok) {
111           return nullptr;
112       }
113       ret = napi_call_function(env, objUtils, logger, 0, nullptr, nullptr);
114
115       // 4. Destroy the ArkTS runtime environment.
116       ret = napi_destroy_ark_runtime(&env);
117       return nullptr;
118   }
119
120   static napi_value CreateArkRuntime(napi_env env, napi_callback_info info)
121   {
122       pthread_t tid;
123       pthread_create(&tid, nullptr, CreateArkRuntimeFunc, nullptr);
124       pthread_join(tid, nullptr);
125       return nullptr;
126   }
127   ```
128
1293. Write the ArkTS code.
130
131   ```ts
132   // ObjectUtils.ets
133   export function Logger() {
134       console.log("print log");
135   }
136
137   // Call ArkTS APIs.
138   import testNapi from 'libentry.so';
139
140   testNapi.createArkRuntime();
141   ```
142