• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Working with Arrays Using Node-API
2
3## Introduction
4
5Node-API provides APIs for directly managing ArkTS arrays.
6
7## Basic Concepts
8
9Node-API can be used to create, access, modify, and traverse arrays. Before using Node-API, it's helpful if you understand the following concepts:
10
11- Array creation: You can use **napi_create_array** to create an array and pass it to the ArkTS layer.
12- Array-related operations: You can use the APIs provides by the Node-API module to obtain the length of an ArkTS array, retrieve the element at the specified index, and set the element value at the specified index.
13- **TypedArray**: A **TypedArray** object in ArkTS is an array-like view of an underlying binary data buffer. It can be simply understood as an array of elements of the specified type. There is no constructor for **TypedArray** objects, but its child class constructor can be used to construct **TypedArray** data. The child classes of **TypedArray** include **Int8Array**, **Uint8Array**, **Uint8ClampedArray**, **Int16Array**, and **Int32Array**.
14- **DataView**: **DataView** is an ArkTS view that allows a variety of number types to be read and written in an **ArrayBuffer** object.
15- **ArrayBuffer**: **ArrayBuffer** is a data struct used to represent a binary data buffer of fixed length.
16
17## Available APIs
18
19The following table describes the APIs for managing ArkTS arrays.
20| API| Description|
21| -------- | -------- |
22| napi_create_array | Creates an ArkTS array.|
23| napi_create_array_with_length | Creates an ArkTS array of the specified length.|
24| napi_get_array_length | Obtains the length of an ArkTS array.|
25| napi_is_array | Checks whether the given **napi_value** is an array.|
26| napi_set_element | Sets an element at the specified index in the given ArkTS array.|
27| napi_get_element | Obtains the element at the specified index in the given ArkTS array.|
28| napi_has_element | Checks whether the element at the specified index exists.|
29| napi_delete_element | Deletes the element at the specified index of the given ArkTS array.|
30| napi_create_typedarray | Creates an ArkTS **TypedArray** object of the specified type, such as **Uint8Array** or **Int32Array**. You can use this API to convert a native value into a **TypedArray** object for performant data processing.  |
31| napi_is_typedarray | Checks whether the given **napi_value** is a **TypedArray** object.|
32| napi_get_typedarray_info | Obtains the properties of a **TypedArray** object.|
33| napi_create_dataview |  Creates a **DataView** object, which allows access to binary data.|
34| napi_is_dataview | Checks whether the given **napi_value** is a **DataView** object in ArkTS.|
35| napi_get_dataview_info | Obtains the properties of a **DataView** object.|
36
37## Example
38
39If you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code for array management APIs.
40
41### napi_create_array
42
43Call **napi_create_array** to create an ArkTS array.
44
45CPP code:
46
47```cpp
48#include "napi/native_api.h"
49
50static constexpr int INT_NUM_5 = 5; // Array length.
51
52static napi_value CreateArray(napi_env env, napi_callback_info info)
53{
54    // Create an empty array.
55    napi_value jsArray = nullptr;
56    napi_create_array(env, &jsArray);
57    // Assign a value to the created array.
58    for (int i = 0; i < INT_NUM_5; i++) {
59        napi_value element;
60        napi_create_int32(env, i, &element);
61        napi_set_element(env, jsArray, i, element);
62    }
63    // Return the created array.
64    return jsArray;
65}
66```
67
68API declaration:
69
70```ts
71// index.d.ts
72export const createArray: () => number[];
73```
74
75ArkTS code:
76
77```ts
78import hilog from '@ohos.hilog';
79import testNapi from 'libentry.so';
80
81hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_array:%{public}s', JSON.stringify(testNapi.createArray()));
82```
83
84### napi_create_array_with_length
85
86Call **napi_create_array_with_length** to create an ArkTS array of the specified length.
87
88CPP code:
89
90```cpp
91#include "napi/native_api.h"
92
93static napi_value CreateArrayWithLength(napi_env env, napi_callback_info info)
94{
95    // Obtain the parameters passed from ArkTS.
96    size_t argc = 1;
97    napi_value argv[1] = {nullptr};
98    napi_value jsArray = nullptr;
99    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
100    // Obtain the array length passed.
101    int32_t length = 0;
102    napi_get_value_int32(env, argv[0], &length);
103    // Use napi_create_array_with_length to create an array of the specified length.
104    napi_create_array_with_length(env, length, &jsArray);
105    // Return the array.
106    return jsArray;
107}
108```
109
110API declaration:
111
112```ts
113// index.d.ts
114export const createArrayWithLength: (length: number) => void[];
115```
116
117ArkTS code:
118
119```ts
120import hilog from '@ohos.hilog';
121import testNapi from 'libentry.so';
122
123let array = testNapi.createArrayWithLength(6);
124hilog.info(0x0000, 'testTag', 'Test Node-API napi_create_array_with_length:%{public}d', array.length);
125```
126
127### napi_get_array_length
128
129Call **napi_get_array_length** to obtain the length of an array.
130
131CPP code:
132
133```cpp
134#include "napi/native_api.h"
135
136static napi_value GetArrayLength(napi_env env, napi_callback_info info)
137{
138    // Obtain the parameters passed from ArkTS.
139    size_t argc = 1;
140    napi_value args[1] = {nullptr};
141    napi_value result;
142    uint32_t length;
143    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
144    // Check whether the parameter is an array.
145    bool is_array;
146    napi_is_array(env, args[0], &is_array);
147    if (!is_array) {
148        napi_throw_type_error(env, nullptr, "Argument must be an array");
149        return nullptr;
150    }
151    napi_get_array_length(env, args[0], &length);
152    // Create a return value.
153    napi_create_uint32(env, length, &result);
154    return result;
155}
156```
157
158API declaration:
159
160```ts
161// index.d.ts
162export const getArrayLength: (arr: Array<any>) => number | void;
163```
164
165ArkTS code:
166
167```ts
168import hilog from '@ohos.hilog';
169import testNapi from 'libentry.so';
170
171const arr = [0, 1, 2, 3, 4, 5];
172hilog.info(0x0000, 'testTag', 'Test Node-API get_array_length:%{public}d', testNapi.getArrayLength(arr));
173```
174
175### napi_is_array
176
177Call **napi_is_array** to check whether the given ArkTS value is an array.
178
179CPP code:
180
181```cpp
182#include "napi/native_api.h"
183
184static napi_value IsArray(napi_env env, napi_callback_info info)
185{
186    // Obtain the parameters passed from ArkTS.
187    size_t argc = 1;
188    napi_value args[1] = {nullptr};
189    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
190    // Call napi_is_array to check whether the input parameter is an array.
191    bool result;
192    napi_status status;
193    status = napi_is_array(env, args[0], &result);
194    if (status != napi_ok) {
195        napi_throw_error(env, nullptr, "Node-API napi_is_array fail");
196        return nullptr;
197    }
198    // Convert the result to napi_value and return it.
199    napi_value returnValue;
200    napi_get_boolean(env, result, &returnValue);
201
202    return returnValue;
203}
204```
205
206API declaration:
207
208```ts
209// index.d.ts
210export const isArray: <T>(data: Array<T> | T) => boolean | void;
211```
212
213ArkTS code:
214
215```ts
216import hilog from '@ohos.hilog';
217import testNapi from 'libentry.so';
218try {
219  let value = new Array<number>(1);
220  let data = "123";
221  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_array: %{public}s', testNapi.isArray<number>(value));
222  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_array: %{public}s', testNapi.isArray<string>(data));
223} catch (error) {
224  hilog.error(0x0000, 'testTag', 'Test Node-API napi_is_array error: %{public}s', error.message);
225}
226```
227
228### napi_set_element
229
230Call **napi_set_element** to set an element at the specified index in an ArkTS array.
231For the object that uses the index value as the key, you can use **napi_set_element** to set the property value.
232
233CPP code:
234
235```cpp
236#include "napi/native_api.h"
237
238static constexpr int INT_ARG_2 = 2; // Input parameter index.
239
240static napi_value NapiSetElement(napi_env env, napi_callback_info info)
241{
242    // Obtain the parameters passed from ArkTS.
243    size_t argc = 3;
244    napi_value args[3] = {nullptr};
245    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
246    // Check whether the first parameter is an array.
247    bool isArr = false;
248    napi_is_array(env, args[0], &isArr);
249    if (!isArr) {
250        napi_throw_type_error(env, nullptr, "Argument should be an object of type array");
251        return nullptr;
252    }
253    // Obtain the index of the element to be set.
254    double index = 0;
255    napi_get_value_double(env, args[1], &index);
256    // Set the input value at the specified index of the array.
257    napi_set_element(env, args[0], static_cast<uint32_t>(index), args[INT_ARG_2]);
258
259    return nullptr;
260}
261```
262
263API declaration:
264
265```ts
266export const napiSetElement: <T>(arr: Array<T>, index: number, value: T) => void;
267```
268
269ArkTS code:
270
271```ts
272import hilog from '@ohos.hilog';
273import testNapi from 'libentry.so';
274let arr = [10, 20, 30];
275testNapi.napiSetElement<number | string>(arr, 1, 'newElement');
276testNapi.napiSetElement<number | string>(arr, 2, 50);
277hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_element arr: %{public}s', arr.toString());
278hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_element arr[3]: %{public}s', arr[3]);
279interface MyObject {
280  first: number;
281  second: number;
282}
283let obj: MyObject = {
284  first: 1,
285  second: 2
286};
287testNapi.napiSetElement<number | string | Object>(arr, 4, obj);
288let objAsString = JSON.stringify(arr[4]);
289hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_element arr[4]: %{public}s', objAsString);
290```
291
292### napi_get_element
293
294Call **napi_get_element** to obtain the element at the specified index in an ArkTS array. The index must be within the valid range of the array. Otherwise, **undefined** will be returned.
295
296CPP code:
297
298```cpp
299#include "napi/native_api.h"
300
301static napi_value NapiGetElement(napi_env env, napi_callback_info info)
302{
303    // Obtain the parameters passed from ArkTS.
304    size_t argc = 2;
305    napi_value args[2] = {nullptr};
306    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
307    // Obtain the index value of the element.
308    uint32_t index;
309    napi_get_value_uint32(env, args[1], &index);
310    // Obtain the element value at the index and store it in result.
311    napi_value result;
312    napi_get_element(env, args[0], index, &result);
313
314    return result;
315}
316```
317
318API declaration:
319
320```ts
321// index.d.ts
322export const napiGetElement: <T>(arr: Array<T>, index: number) => number | string | Object | boolean | undefined;
323```
324
325ArkTS code:
326
327```ts
328import hilog from '@ohos.hilog';
329import testNapi from 'libentry.so';
330
331interface MyObject {
332  first: number;
333  second: number;
334}
335let obj: MyObject = {
336  first: 1,
337  second: 2
338};
339let arr = [10, 'hello', null, obj];
340hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[0]: %{public}d', testNapi.napiGetElement<number | string | null | Object>(arr, 0));
341hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[1]: %{public}s', testNapi.napiGetElement<number | string | null | Object>(arr, 1));
342hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[2]: %{public}s', testNapi.napiGetElement<number | string | null | Object>(arr, 2));
343hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[3]: %{public}s', testNapi.napiGetElement<number | string | null | Object>(arr, 3));
344hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[4]: %{public}s', JSON.stringify(testNapi.napiGetElement(arr, 4)));
345hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[null]: %{public}s', testNapi.napiGetElement<number | string | null | Object>(arr, 5));
346```
347
348### napi_has_element
349
350Call **napi_has_element** to check whether an ArkTS array has the element at the specified index. If the specified index exceeds the valid range of the array, **false** will be returned, which indicates that the element does not exist.
351
352CPP code:
353
354```cpp
355#include "napi/native_api.h"
356
357static napi_value NapiHasElement(napi_env env, napi_callback_info info)
358{
359    // Obtain the parameters passed from ArkTS.
360    size_t argc = 2;
361    napi_value args[2] = {nullptr};
362    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
363    // Obtain the index of the element to be checked.
364    uint32_t index;
365    napi_get_value_uint32(env, args[1], &index);
366    // Check whether the element exists based on the given index.
367    bool hasElement = true;
368    napi_has_element(env, args[0], index, &hasElement);
369    // Convert the bool value to napi_value and return it.
370    napi_value result;
371    napi_get_boolean(env, hasElement, &result);
372    return result;
373}
374```
375
376API declaration:
377
378```ts
379// index.d.ts
380export const napiHasElement: <T>(arr: Array<T>, index: number) => boolean;
381```
382
383ArkTS code:
384
385```ts
386import hilog from '@ohos.hilog';
387import testNapi from 'libentry.so';
388
389let arr = [10, 'hello', null, 'world'];
390hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_element arr[0]: %{public}s', testNapi.napiHasElement<number | string | null>(arr, 0));
391hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_element arr[7]: %{public}s', testNapi.napiHasElement<number | string | null>(arr, 7));
392```
393
394### napi_delete_element
395
396Call **napi_delete_element** to delete the element at the specified index from an ArkTS array.
397
398CPP code:
399
400```cpp
401#include "napi/native_api.h"
402
403static napi_value NapiDeleteElement(napi_env env, napi_callback_info info)
404{
405    // Obtain the parameters passed from ArkTS.
406    size_t argc = 2;
407    napi_value args[2] = {nullptr};
408    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
409    // Obtain the index of the element to delete.
410    uint32_t index;
411    napi_get_value_uint32(env, args[1], &index);
412    // Delete the element at the specified index.
413    bool deleted = true;
414    napi_delete_element(env, args[0], index, &deleted);
415    // Convert the bool value to napi_value and return it.
416    napi_value result;
417    napi_get_boolean(env, deleted, &result);
418    return result;
419}
420```
421
422API declaration:
423
424```ts
425// index.d.ts
426export const napiDeleteElement: <T>(arr: Array<T>, index: number) => boolean;
427```
428
429ArkTS code:
430
431```ts
432// Import napiHasElement and napiGetElement.
433import hilog from '@ohos.hilog';
434import testNapi from 'libentry.so';
435
436let arr = [10, 'hello', null, 'world'];
437hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_element arr[0]: %{public}s', testNapi.napiHasElement<number | string | null>(arr, 0));
438hilog.info(0x0000, 'testTag', 'Test Node-API napi_delete_element arr[0]: %{public}s', testNapi.napiDeleteElement<number | string | null>(arr, 0));
439hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_element deleted arr[0]: %{public}s', testNapi.napiHasElement<number | string | null>(arr, 0));
440hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_element arr[0]: %{public}d', testNapi.napiGetElement<number | string | null>(arr, 0));
441```
442
443### napi_create_typedarray
444
445Call **napi_create_typedarray** to create an ArkTS **TypedArray** object of the specified type from an existing **ArrayBuffer** object.
446
447CPP code:
448
449```cpp
450#include "napi/native_api.h"
451
452static napi_value CreateTypedArray(napi_env env, napi_callback_info info)
453{
454    // Obtain the parameters passed from ArkTS.
455    size_t argc = 1;
456    napi_value args[1] = {nullptr};
457    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
458    int32_t typeNum = 0;
459    napi_get_value_int32(env, args[0], &typeNum);
460    napi_typedarray_type arrayType;
461    // Set the element size.
462    size_t elementSize = 0;
463    // Determine the type of the array to create based on the type value passed.
464    arrayType = static_cast<napi_typedarray_type>(typeNum);
465        switch (typeNum) {
466    case napi_int8_array:
467    case napi_uint8_array:
468    case napi_uint8_clamped_array:
469        elementSize = sizeof(int8_t);
470        break;
471    case napi_int16_array:
472    case napi_uint16_array:
473        elementSize = sizeof(int16_t);
474        break;
475    case napi_int32_array:
476    case napi_uint32_array:
477        elementSize = sizeof(int32_t);
478        break;
479    case napi_float32_array:
480        elementSize = sizeof(float);
481        break;
482    case napi_float64_array:
483        elementSize = sizeof(double);
484        break;
485    case napi_bigint64_array:
486    case napi_biguint64_array:
487        elementSize = sizeof(int64_t);
488        break;
489    default:
490    // By default, an array of the napi_int8_array type is created.
491        arrayType = napi_int8_array;
492        elementSize = sizeof(int8_t);
493        break;
494    }
495    size_t length = 3;
496    napi_value arrayBuffer = nullptr;
497    napi_value typedArray = nullptr;
498    void *data;
499    // Create an ArrayBuffer object.
500    napi_create_arraybuffer(env, length * elementSize, (void **)&data, &arrayBuffer);
501    // Create a TypedArray object of the specified type.
502    napi_create_typedarray(env, arrayType, length, arrayBuffer, 0, &typedArray);
503    return typedArray;
504}
505```
506
507API declaration:
508
509```ts
510// index.d.ts
511export const enum TypedArrayTypes {
512  INT8_ARRAY = 0,
513  UINT8_ARRAY,
514  UINT8_CLAMPED_ARRAY,
515  INT16_ARRAY,
516  UINT16_ARRAY,
517  INT32_ARRAY,
518  UINT32_ARRAY,
519  FLOAT32_ARRAY,
520  FLOAT64_ARRAY,
521  BIGINT64_ARRAY,
522  BIGuINT64_ARRAY,
523}
524export const createTypedArray: <T>(type: TypedArrayTypes) => T;
525```
526
527ArkTS code:
528
529```ts
530import hilog from '@ohos.hilog';
531import testNapi from 'libentry.so';
532
533// Pass the type of the array to create.
534let typedArray = testNapi.createTypedArray<Int8Array>(testNapi.TypedArrayTypes["INT8_ARRAY"]);
535if (typedArray instanceof Int8Array) {
536    hilog.info(0x0000, 'testTag', ' Node-API napi_create_typedarray: Int8Array');
537}
538let uint8Array = testNapi.createTypedArray<Uint8Array>(testNapi.TypedArrayTypes["UINT8_ARRAY"]);
539if (uint8Array instanceof Uint8Array) {
540    hilog.info(0x0000, 'testTag', ' Node-API napi_create_typedarray: Uint8Array');
541}
542```
543
544Modify the module initialization in **use-napi-process.md** as follows:
545
546```cpp
547#include <string>
548
549EXTERN_C_START
550static napi_value Init(napi_env env, napi_value exports)
551{
552    // The defined TypedArray type is used by ArkTS to store typedArrayTypes. For details about the example, see createTypedArray() on ArkTS.
553    napi_value typedArrayTypes;
554    napi_create_object(env, &typedArrayTypes);
555    napi_value typeIndex;
556    std::string typeKeys[] = {
557        "INT8_ARRAY",   "UINT8_ARRAY",   "UINT8_CLAMPED_ARRAY", "INT16_ARRAY",      "UINT16_ARRAY",    "INT32_ARRAY",
558        "UINT32_ARRAY", "FLOAT32_ARRAY", "FLOAT64_ARRAY",       "BIGINT64_ARRAY", "BIGUINT64_ARRAY",
559    };
560    for (int32_t i = 0; i < sizeof(typeKeys) / sizeof(typeKeys[0]); i++) {
561        napi_create_int32(env, i, &typeIndex);
562        napi_set_named_property(env, typedArrayTypes, typeKeys[i].c_str(), typeIndex);
563    }
564    napi_property_descriptor desc[] = {
565        {"createTypedArray", nullptr, CreateTypedArray, nullptr, nullptr, nullptr, napi_default, nullptr},
566        {"TypedArrayTypes", nullptr, nullptr, nullptr, nullptr, typedArrayTypes, napi_default, nullptr}
567    };
568    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
569    return exports;
570}
571EXTERN_C_END
572
573```
574
575### napi_is_typedarray
576
577Call **napi_is_typedarray** to check whether the **napi_value** given from ArkTS is a **TypedArray** object.
578
579CPP code:
580
581```cpp
582#include "napi/native_api.h"
583
584static napi_value IsTypedarray(napi_env env, napi_callback_info info)
585{
586    // Obtain the parameters passed from ArkTS.
587    size_t argc = 1;
588    napi_value args[1] = {nullptr};
589    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
590    // Call napi_is_typedarray to check whether the input parameter type is TypedArray.
591    bool result = false;
592        napi_status status;
593    status = napi_is_typedarray(env, args[0], &result);
594    if (status != napi_ok) {
595        napi_throw_error(env, nullptr, "Node-API napi_is_typedarray fail");
596        return nullptr;
597    }
598    // Convert the result to napi_value and return it.
599    napi_value returnValue = nullptr;
600    napi_get_boolean(env, result, &returnValue);
601
602    return returnValue;
603}
604```
605
606API declaration:
607
608```ts
609// index.d.ts
610export const isTypedarray: (data: Object) => boolean | void;
611```
612
613ArkTS code:
614
615```ts
616import hilog from '@ohos.hilog';
617import testNapi from 'libentry.so';
618try {
619  let value = new Uint8Array([1, 2, 3, 4]);
620  let data = "123";
621  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_typedarray: %{public}s', testNapi.isTypedarray(value));
622  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_typedarray: %{public}s', testNapi.isTypedarray(data));
623} catch (error) {
624  hilog.error(0x0000, 'testTag', 'Test Node-API napi_is_typedarray error: %{public}s', error.message);
625}
626```
627
628### napi_get_typedarray_info
629
630Call **napi_get_typedarray_info** to obtain properties of a **TypedArray** object.
631
632CPP code:
633
634```cpp
635#include "napi/native_api.h"
636
637static napi_value GetTypedarrayInfo(napi_env env, napi_callback_info info)
638{
639    // Obtain and parse the parameters passed from ArkTS. The first parameter is the TypedArray type of the property to obtain, and the second parameter is the enums of the property type to obtain.
640    size_t argc = 2;
641    napi_value args[2] = {nullptr};
642    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
643    // Convert the second parameter to the int32 type for comparison.
644    int32_t infoTypeParam;
645    napi_get_value_int32(env, args[1], &infoTypeParam);
646    // Define the InfoType enums in the same sequence as those in ArkTS.
647    enum InfoType { INFO_TYPE = 1, INFO_LENGTH, INFO_ARRAY_BUFFER, INFO_BYTE_OFFSET };
648    void *data;
649    napi_typedarray_type type;
650    size_t byteOffset, length;
651    napi_value arraybuffer;
652    // Call napi_get_typedarray_info to obtain TypedArray information.
653    napi_get_typedarray_info(env, args[0], &type, &length, &data, &arraybuffer, &byteOffset);
654    napi_value result;
655    // Return the property value based on the property name.
656    switch (infoTypeParam) {
657    case INFO_TYPE:
658        // If the input parameter is TypedArray data of the int8 type, the value type is napi_int8_array.
659        napi_value int8_type;
660        napi_get_boolean(env, type == napi_int8_array, &int8_type);
661        result = int8_type;
662        break;
663    case INFO_LENGTH:
664        // Byte length of elements in TypedArray.
665        napi_value napiLength;
666        napi_create_int32(env, length, &napiLength);
667        result = napiLength;
668        break;
669    case INFO_BYTE_OFFSET:
670        // Byte offset of the first TypedArray element in the native array.
671        napi_value napiByteOffset;
672        napi_create_int32(env, byteOffset, &napiByteOffset);
673        result = napiByteOffset;
674        break;
675    case INFO_ARRAY_BUFFER:
676        // ArrayBuffer under TypedArray.
677        result = arraybuffer;
678        break;
679    default:
680        break;
681    }
682    return result;
683}
684```
685
686API declaration:
687
688```ts
689// index.d.ts
690export const getTypedarrayInfo: <T>(typeArray: T, infoType: number) => ArrayBuffer | number | boolean;
691```
692
693ArkTS code:
694
695```ts
696import hilog from '@ohos.hilog';
697import testNapi from 'libentry.so';
698
699// Pass in the TypedArray type. TypedArray is a class array data view used to describe binary data. It does not have a constructor and can be constructed from its child class.
700// The child classes of TypedArray include Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, and Int32Array.
701let int8Array = new Int8Array([15, 7]);
702// Define the InfoType enums, which are the properties of TypedArray.
703enum InfoType {
704    TYPE = 1, // Type of the TypedArray.
705    LENGTH = 2, // Length of the input TypedArray.
706    ARRAY_BUFFER = 3, // ArrayBuffer under TypedArray.
707    BYTE_OFFSET = 4 // Offset of the first ArrayBuffer element in the native array.
708};
709let arrbuff = testNapi.getTypedarrayInfo(int8Array, InfoType.ARRAY_BUFFER) as ArrayBuffer;
710// Convert arraybuffer to an array.
711let arr = new Array(new Int8Array(arrbuff));
712hilog.info(0x0000, 'Node-API', 'get_typedarray_info_arraybuffer: %{public}s', arr.toString());
713hilog.info(0x0000, 'Node-API', 'get_typedarray_info_isIn8Array: %{public}s', testNapi.getTypedarrayInfo(int8Array, InfoType.TYPE).toString());
714hilog.info(0x0000, 'Node-API', 'get_typedarray_info_length: %{public}d', testNapi.getTypedarrayInfo(int8Array, InfoType.LENGTH));
715hilog.info(0x0000, 'Node-API', 'get_typedarray_info_byte_offset: %{public}d', testNapi.getTypedarrayInfo(int8Array, InfoType.BYTE_OFFSET));
716```
717
718### napi_create_dataview
719
720Call **napi_create_dataview** to create a **DataView** object to facilitate access and operation of binary data. You need to specify a buffer pointing to the binary data and the number of bytes included.
721
722CPP code:
723
724```cpp
725#include "napi/native_api.h"
726
727static napi_value CreateDataView(napi_env env, napi_callback_info info)
728{
729    // Obtain the parameters passed from ArkTS.
730    size_t argc = 1;
731    napi_value args[1] = {nullptr};
732    napi_value arraybuffer = nullptr;
733    napi_value result = nullptr;
734    // Byte length of DataView.
735    size_t byteLength = 12;
736    // Offset of the byte.
737    size_t byteOffset = 4;
738    // Obtain the parameters of the callback.
739    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
740    // Convert the parameter into the object type.
741    napi_coerce_to_object(env, args[0], &arraybuffer);
742    // Create a DataView object with the specified byte length and offset.
743    napi_status status = napi_create_dataview(env, byteLength, arraybuffer, byteOffset, &result);
744    if (status != napi_ok) {
745        // Throw an error indicating that the DataView fails to be created.
746        napi_throw_error(env, nullptr, "Failed to create DataView");
747        return nullptr;
748    }
749    // Obtain the pointer to the DataView object and the length.
750    uint8_t *data = nullptr;
751    size_t length = 0;
752    napi_get_dataview_info(env, result, &length, (void **)&data, nullptr, nullptr);
753    // Assign values to DataView.
754    for (size_t i = 0; i < length; i++) {
755        data[i] = static_cast<uint8_t>(i + 1);
756    }
757    return result;
758}
759```
760
761API declaration:
762
763```ts
764// index.d.ts
765export const createDataView: (arraybuffer:ArrayBuffer) => DataView | void;
766```
767
768ArkTS code:
769
770```ts
771import hilog from '@ohos.hilog';
772import testNapi from 'libentry.so';
773
774const arrayBuffer = new ArrayBuffer(16);
775const dataView = testNapi.createDataView(arrayBuffer) as DataView;
776hilog.info(0x0000, 'testTag', 'Test Node-API dataView: %{public}d', dataView.byteLength);
777hilog.info(0x0000, 'testTag','Test Node-API dataView first data: %{public}d', dataView.getInt8(0));
778```
779
780### napi_is_dataview
781
782Call **napi_is_dataview** to check whether the **napi_value** given from ArkTS is a **DataView** object.
783
784CPP code:
785
786```cpp
787#include "napi/native_api.h"
788
789static napi_value IsDataView(napi_env env, napi_callback_info info)
790{
791    // Obtain the parameters passed from ArkTS.
792    size_t argc = 1;
793    napi_value args[1] = {nullptr};
794    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
795
796    // Call napi_is_dataview to check whether the input parameter is a DataView object.
797    bool result;
798    napi_status status;
799    status = napi_is_dataview(env, args[0], &result);
800    if (status != napi_ok) {
801        napi_throw_error(env, nullptr, "Node-API napi_is_dataview fail");
802        return nullptr;
803    }
804    // Convert the result to napi_value and return it.
805    napi_value returnValue;
806    napi_get_boolean(env, result, &returnValue);
807
808    return returnValue;
809}
810```
811
812API declaration:
813
814```ts
815// index.d.ts
816export const isDataView: (date: DataView | string) => boolean | void;
817```
818
819ArkTS code:
820
821```ts
822import hilog from '@ohos.hilog';
823import testNapi from 'libentry.so';
824try {
825  let buffer = new ArrayBuffer(16);
826  let dataView = new DataView(buffer);
827  let data = "123";
828  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_dataview: %{public}s', testNapi.isDataView(dataView));
829  hilog.info(0x0000, 'testTag', 'Test Node-API napi_is_dataview: %{public}s', testNapi.isDataView(data));
830} catch (error) {
831  hilog.error(0x0000, 'testTag', 'Test Node-API napi_is_dataview error: %{public}s', error.message);
832}
833```
834
835### napi_get_dataview_info
836
837Call **napi_get_dataview_info** to obtain properties of a **DataView** object.
838
839CPP code:
840
841```cpp
842#include "napi/native_api.h"
843
844static napi_value GetDataViewInfo(napi_env env, napi_callback_info info)
845{
846    // Obtain the parameters passed from ArkTS.
847    size_t argc = 2;
848    napi_value args[2] = {nullptr};
849    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
850    // Convert the second parameter to an int32 number.
851    int32_t infoType;
852    napi_get_value_int32(env, args[1], &infoType);
853    size_t byteLength;
854    void *data;
855    napi_value arrayBuffer;
856    size_t byteOffset;
857    // Define the InfoType enums in the same sequence as those in ArkTS.
858    enum InfoType { BYTE_LENGTH = 0, ARRAY_BUFFER, BYTE_OFFSET };
859    // Obtain DataView information.
860    napi_get_dataview_info(env, args[0], &byteLength, &data, &arrayBuffer, &byteOffset);
861    napi_value result;
862    switch (infoType) {
863        case BYTE_LENGTH:
864            // Return the number of bytes of the DataView object.
865            napi_value napiByteLength;
866            napi_create_int32(env, byteLength, &napiByteLength);
867            result = napiByteLength;
868            break;
869        case ARRAY_BUFFER:
870            // Return the ArrayBuffer of the DataView object.
871            result = arrayBuffer;
872            break;
873        case BYTE_OFFSET:
874            // Return the byte offset of the DataView object.
875            napi_value napiByteOffset;
876            napi_create_int32(env, byteOffset, &napiByteOffset);
877            result = napiByteOffset;
878            break;
879        default:
880            break;
881    }
882    return result;
883}
884```
885
886API declaration:
887
888```ts
889// index.d.ts
890export const getDataViewInfo: (dataView: DataView, infoType: number) => ArrayBuffer | number;
891```
892
893ArkTS code:
894
895```ts
896import hilog from '@ohos.hilog';
897import testNapi from 'libentry.so';
898
899// Create an ArrayBuffer object.
900let arrayBuffer = new Int8Array([2, 5]).buffer;
901// Use arrayBuffer to create a DataView object.
902let dataView = new DataView(arrayBuffer);
903// Define an enum type.
904enum InfoType {
905    BYTE_LENGTH = 0,
906    ARRAY_BUFFER = 1,
907    BYTE_OFFSET = 2,
908};
909// Pass in the parameter of DataView to obtain the number of bytes in DataView.
910hilog.info(0x0000, 'Node-API', 'get_dataview_info_bytelength %{public}d', testNapi.getDataViewInfo(dataView, InfoType.BYTE_LENGTH));
911// Pass in the parameter of DataView to obtain the ArrayBuffer of DataView.
912let arrbuff = testNapi.getDataViewInfo(dataView, InfoType.ARRAY_BUFFER) as ArrayBuffer;
913// Convert arraybuffer to an array.
914let arr = Array.from(new Int8Array(arrbuff));
915hilog.info(0x0000, 'Node-API', 'get_dataview_info_arraybuffer %{public}s', arr.toString());
916// Pass in the parameter of DataView to obtain the byte offset in the data buffer of DataView.
917hilog.info(0x0000, 'Node-API', 'get_dataview_info_byteoffset %{public}d', testNapi.getDataViewInfo(dataView, InfoType.BYTE_OFFSET));
918```
919
920To 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"**.
921
922```text
923// CMakeLists.txt
924add_definitions( "-DLOG_DOMAIN=0xd0d0" )
925add_definitions( "-DLOG_TAG=\"testTag\"" )
926target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
927```
928