• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Associating Data with a Running Environment to Tie Their Lifecycle Using Node-API
2
3## Introduction
4
5Use Node-API to associate specific data with the currently running environment so that the data can be retrieved later when required.
6
7## Basic Concepts
8
9By associating the data with the currently running environment, the lifecycle of the C++ data struct is associated with that of the environment. This means the associated data remains valid as long as the running environment exists.
10
11## Available APIs
12
13The following table lists the APIs.
14| API| Description|
15| -------- | -------- |
16| napi_set_instance_data | Associates data with the currently running environment.|
17| napi_get_instance_data | Retrieves the data that was previously associated with the currently running environment.|
18
19## Example
20
21For details about the Node-API development process, see [Node-API Development Process](use-napi-process.md). This document describes only the related C++ and ArkTS code.
22
23### napi_set_instance_data
24
25Associate data with the currently running environment.
26
27CPP code
28
29```cpp
30#include <cstdlib>
31#include "napi/native_api.h"
32
33// Define a struct to store instance data.
34struct InstanceData {
35    int32_t value;
36};
37
38// Callback to be invoked to clear the instance data when the object is released.
39void FinalizeCallback(napi_env env, void *finalize_data, void *finalize_hint)
40{
41    if (finalize_data) {
42        InstanceData *data = reinterpret_cast<InstanceData *>(finalize_data);
43        // Release the memory and clear the address pointed by the pointer.
44        delete (data);
45        *(InstanceData **)finalize_data = nullptr;
46    }
47}
48
49static napi_value SetInstanceData(napi_env env, napi_callback_info info)
50{
51    size_t argc = 1;
52    napi_value argv[1];
53    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
54    int32_t instanceDataValue;
55    napi_get_value_int32(env, argv[0], &instanceDataValue);
56    InstanceData *instanceData = new InstanceData;
57    instanceData->value = instanceDataValue;
58    // Call napi_set_instance_data to associate the instance data with the Node-API environment and specify the FinalizeCallback function.
59    napi_status status = napi_set_instance_data(env, instanceData, FinalizeCallback, nullptr);
60    bool success = true;
61    napi_value result;
62    if (status == napi_ok) {
63        napi_get_boolean(env, success, &result);
64    }
65    return result;
66}
67```
68
69API declaration
70
71```ts
72// index.d.ts
73export const setInstanceData: (data: number) => boolean;
74```
75
76ArkTS code
77
78```ts
79import hilog from '@ohos.hilog';
80import testNapi from 'libentry.so';
81let data = 5;
82let value = testNapi.setInstanceData(data);
83hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_instance_data:%{public}s', value);
84```
85
86### napi_get_instance_data
87
88Retrieve the data that was previously associated with the currently running environment.
89
90CPP code
91
92```cpp
93#include "napi/native_api.h"
94
95static napi_value GetInstanceData(napi_env env, napi_callback_info info) {
96    InstanceData *resData = nullptr;
97    // Call napi_get_instance_data to obtain the data.
98    napi_get_instance_data(env, (void **)&resData);
99    napi_value result;
100    napi_create_int32(env, resData->value, &result);
101    return result;
102}
103```
104
105API declaration
106
107```ts
108// index.d.ts
109export const getInstanceData: () => number;
110```
111
112ArkTS code
113
114```ts
115import hilog from '@ohos.hilog';
116import testNapi from 'libentry.so';
117let data = 5;
118testNapi.setInstanceData(data);
119let value = testNapi.getInstanceData();
120hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_instance_data:%{public}d', value);
121```
122
123To print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**.
124
125```text
126// CMakeLists.txt
127add_definitions( "-DLOG_DOMAIN=0xd0d0" )
128add_definitions( "-DLOG_TAG=\"testTag\"" )
129target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
130```
131