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