1# Working with Arrays Using JSVM-API 2 3## Introduction 4 5JSVM-API provides APIs for directly managing JS arrays. 6 7## Basic Concepts 8 9JSVM-API can be used to create, access, modify, and traverse arrays. Before using JSVM-API, it's helpful if you understand the following concepts: 10 11- Array creation: You can use **OH_JSVM_CreateArray** to create an array and pass it to the JS layer. 12- Array-related operations: You can use the APIs provides by the JSVM module to obtain the length of a JS array, retrieve the element at the specified index, and set the element value at the specified index. 13- **TypedArray**: A **TypedArray** object in JS 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 a JS 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 19| API | Description | 20| ---------------------------- | ------------------------------------------ | 21|OH_JSVM_CreateArray | Creates a JS array object.| 22|OH_JSVM_CreateArrayWithLength | Creates a JS array object of the specified length.| 23|OH_JSVM_CreateTypedarray | Creates a JS **TypedArray** object for an **ArrayBuffer**. The **TypedArray** object provides an array-like view over an underlying data buffer, where each element has the same underlying binary scalar data type.| 24|OH_JSVM_CreateDataview | Creates a JS **DataView** object for an **ArrayBuffer**. The **DataView** object provides an array-like view of over an underlying data buffer.| 25|OH_JSVM_GetArrayLength | Obtains the length of an array.| 26|OH_JSVM_GetTypedarrayInfo | Obtains information about a **TypedArray** object.| 27|OH_JSVM_GetDataviewInfo | Obtains information of a **DataView** object.| 28|OH_JSVM_IsArray | Checks whether a JS object is an array.| 29|OH_JSVM_SetElement | Sets an element at the specified index for a JS object.| 30|OH_JSVM_GetElement | Obtains the element at the specified index of a JS object.| 31|OH_JSVM_HasElement | Checks whether a JS object has an element at the specified index.| 32|OH_JSVM_DeleteElement | Deletes the element at the specified index from a JS object.| 33|OH_JSVM_IsDataview | Checks whether a JS object is a **DataView** object.| 34|OH_JSVM_IsTypedarray | Checks whether a JS object is a **TypedArray** object.| 35 36## Example 37 38If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ and ArkTS code for array management. The **OH_JSVM_CreateTypedarray** API is different. For details, see the example. 39 40### OH_JSVM_CreateArray 41 42Creates a JS array object. 43 44CPP code: 45 46```cpp 47// hello.cpp 48#include "napi/native_api.h" 49#include "ark_runtime/jsvm.h" 50#include <hilog/log.h> 51// Register the CreateArray callback. 52static JSVM_CallbackStruct param[] = { 53 {.data = nullptr, .callback = CreateArray}, 54}; 55static JSVM_CallbackStruct *method = param; 56// Set a property descriptor named createArray and associate it with a callback. This allows the CreateArray callback to be called from JS. 57static JSVM_PropertyDescriptor descriptor[] = { 58 {"createArray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 59}; 60static int DIFF_VALUE_FIVE = 5; 61// Define OH_JSVM_CreateArray. 62static JSVM_Value CreateArray(JSVM_Env env, JSVM_CallbackInfo info) 63{ 64 // Create an empty array. 65 JSVM_Value array = nullptr; 66 JSVM_Status status = OH_JSVM_CreateArray(env, &array); 67 // Assign values to the created array. 68 for (int i = 0; i < DIFF_VALUE_FIVE; i++) { 69 JSVM_Value element; 70 OH_JSVM_CreateInt32(env, i, &element); 71 OH_JSVM_SetElement(env, array, i, element); 72 } 73 if (status != JSVM_OK) { 74 OH_LOG_ERROR(LOG_APP, "JSVM CreateArray fail"); 75 } else { 76 OH_LOG_INFO(LOG_APP, "JSVM CreateArray success"); 77 } 78 return array; 79} 80``` 81 82ArkTS code: 83 84```ts 85import hilog from "@ohos.hilog" 86// Import the native APIs. 87import napitest from "libentry.so" 88let script: string = ` 89 function testCreateArray() { 90 return createArray(); 91 } 92 testCreateArray() 93`; 94try { 95 let result = napitest.runJsVm(script); 96 hilog.info(0x0000, 'testJSVM', 'Test JSVM testCreateArray: %{public}s', result); 97} catch (error) { 98 hilog.error(0x0000, 'testJSVM', 'Test JSVM testCreateArray error: %{public}s', error.message); 99} 100``` 101 102### OH_JSVM_CreateArrayWithLength 103 104Creates a JS array object of the specified length. 105 106CPP code: 107 108```cpp 109// hello.cpp 110#include "napi/native_api.h" 111#include "ark_runtime/jsvm.h" 112#include <hilog/log.h> 113// Register the CreateArrayWithLength callback. 114static JSVM_CallbackStruct param[] = { 115 {.data = nullptr, .callback = CreateArrayWithLength}, 116}; 117static JSVM_CallbackStruct *method = param; 118// Set a property descriptor named createArrayWithLength and associate it with a callback. This allows the CreateArrayWithLength callback to be called from JS. 119static JSVM_PropertyDescriptor descriptor[] = { 120 {"createArrayWithLength", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 121}; 122// Define OH_JSVM_CreateArrayWithLength. 123static JSVM_Value CreateArrayWithLength(JSVM_Env env, JSVM_CallbackInfo info) 124{ 125 size_t argc = 1; 126 JSVM_Value argv[1] = {nullptr}; 127 JSVM_Value result = nullptr; 128 // Obtain the callback information. 129 OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); 130 // Obtain the array length passed. 131 int32_t length; 132 OH_JSVM_GetValueInt32(env, argv[0], &length); 133 // Call OH_JSVM_CreateArrayWithLength to create an array with the specified length. 134 JSVM_Status status = OH_JSVM_CreateArrayWithLength(env, length, &result); 135 if (status == JSVM_OK) { 136 // Set an element in the created array. 137 for (int32_t i = 0; i < length; i++) { 138 JSVM_Value value; 139 OH_JSVM_CreateInt32(env, i, &value); 140 OH_JSVM_SetElement(env, result, i, value); 141 } 142 OH_LOG_INFO(LOG_APP, "JSVM CreateArrayWithLength success"); 143 } else { 144 OH_LOG_ERROR(LOG_APP, "JSVM CreateArrayWithLength fail"); 145 } 146 return result; 147} 148``` 149 150ArkTS code: 151 152```ts 153import hilog from "@ohos.hilog" 154// Import the native APIs. 155import napitest from "libentry.so" 156let num = 7; 157let script: string = ` 158 function testCreateArrayWithLength(num) { 159 return createArrayWithLength(num); 160 } 161 testCreateArrayWithLength(${num}) 162 `; 163try { 164 let result = napitest.runJsVm(script); 165 hilog.info(0x0000, 'testJSVM', 'Test JSVM testCreateArrayWithLength: %{public}s', result); 166} catch (error) { 167 hilog.error(0x0000, 'testJSVM', 'Test JSVM testCreateArrayWithLength error: %{public}s', error.message); 168} 169``` 170 171### OH_JSVM_CreateTypedarray 172 173Creates a JS **TypedArray** object for an **ArrayBuffer**. The **TypedArray** object provides an array-like view over an underlying data buffer, where each element has the same underlying binary scalar data type. 174 175CPP code: 176 177```cpp 178// hello.cpp 179#include "napi/native_api.h" 180#include "ark_runtime/jsvm.h" 181#include <hilog/log.h> 182// Register the CreateTypedArray callback. 183static JSVM_CallbackStruct param[] = { 184 {.data = nullptr, .callback = CreateTypedArray}, 185}; 186static JSVM_CallbackStruct *method = param; 187// Set a property descriptor named createTypedArray and associate it with a callback. This allows the CreateTypedArray callback to be called from JS. 188static JSVM_PropertyDescriptor descriptor[] = { 189 {"createTypedArray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 190}; 191// Define OH_JSVM_CreateTypedarray. 192static int DIFF_VALUE_THREE = 3; 193static JSVM_Value CreateTypedArray(JSVM_Env env, JSVM_CallbackInfo info) 194{ 195 size_t argc = 1; 196 JSVM_Value args[1] = {nullptr}; 197 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 198 int32_t typeNum; 199 OH_JSVM_GetValueInt32(env, args[0], &typeNum); 200 JSVM_TypedarrayType arrayType; 201 // Set the element size. 202 size_t elementSize = 0; 203 // Convert the value to the JSVM_TypedarrayType type. 204 arrayType = static_cast<JSVM_TypedarrayType>(typeNum); 205 switch (typeNum) { 206 case JSVM_INT8_ARRAY: 207 case JSVM_UINT8_ARRAY: 208 case JSVM_UINT8_CLAMPED_ARRAY: 209 elementSize = sizeof(int8_t); 210 break; 211 case JSVM_INT16_ARRAY: 212 case JSVM_UINT16_ARRAY: 213 elementSize = sizeof(int16_t); 214 break; 215 case JSVM_INT32_ARRAY: 216 case JSVM_UINT32_ARRAY: 217 elementSize = sizeof(int32_t); 218 break; 219 case JSVM_FLOAT32_ARRAY: 220 elementSize = sizeof(float); 221 break; 222 case JSVM_FLOAT64_ARRAY: 223 elementSize = sizeof(double); 224 break; 225 case JSVM_BIGINT64_ARRAY: 226 case JSVM_BIGUINT64_ARRAY: 227 elementSize = sizeof(int64_t); 228 break; 229 default: 230 // By default, an array of the JSVM_INT8_ARRAY type is created. 231 arrayType = JSVM_INT8_ARRAY; 232 elementSize = sizeof(int8_t); 233 break; 234 } 235 size_t length = DIFF_VALUE_THREE; 236 JSVM_Value arrayBuffer = nullptr; 237 JSVM_Value typedArray = nullptr; 238 void *data; 239 // Create an ArrayBuffer object. 240 OH_JSVM_CreateArraybuffer(env, length * elementSize, (void **)&data, &arrayBuffer); 241 // Create a TypedArray object of the specified type. 242 JSVM_Status status = OH_JSVM_CreateTypedarray(env, arrayType, length, arrayBuffer, 0, &typedArray); 243 if (status != JSVM_OK) { 244 OH_LOG_ERROR(LOG_APP, "JSVM CreateTypedArray fail"); 245 } else { 246 OH_LOG_INFO(LOG_APP, "JSVM CreateTypedArray success"); 247 } 248 return typedArray; 249} 250``` 251 252Modify the module initialization in **use-jsvm-process.md** as follows: 253 254```cpp 255// hello.cpp 256static napi_value Init(napi_env env, napi_value exports) { 257 // The defined TypedArray type is used by JS to store JSVM_TypedArrayType. For details, see createTypedArray sample code in ArkTS code. You can determine whether to use it based on service requirements. 258 napi_value typedArrayTypes; 259 napi_create_object(env, &typedArrayTypes); 260 napi_value typeIndex; 261 std::string typeKeys[] = { 262 "INT8_ARRAY", "UINT8_ARRAY", "UINT8_CLAMPED_ARRAY", "INT_ARRAY", "UINT16_ARRAY", "INT32_ARRAY", 263 "UINT32_ARRAY", "FLOAT32_ARRAY", "FLOAT64_ARRAY", "BIGINT64_ARRAY", "BIGUINT64_ARRAY", 264 }; 265 for (int32_t i = 0; i < sizeof(typeKeys) / sizeof(typeKeys[0]); i++) { 266 napi_create_int32(env, i, &typeIndex); 267 napi_set_named_property(env, typedArrayTypes, typeKeys[i].c_str(), typeIndex); 268 } 269 // Create the mappings between the ArkTS and C++ APIs. 270 napi_property_descriptor desc[] = { 271 {"runJsVm", nullptr, RunJsVm, nullptr, nullptr, nullptr, napi_default, nullptr}, 272 {"TypedArrayTypes", nullptr, nullptr, nullptr, nullptr, typedArrayTypes, napi_default, nullptr}, 273 }; 274 // Register the native RunJsVm function with the JS exports object, making the native API available to JS. 275 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 276 return exports; 277} 278``` 279 280ArkTS code: 281 282```ts 283import hilog from "@ohos.hilog" 284// Import the native APIs. 285import napitest from "libentry.so" 286try { 287 // Pass in the TypedArray type to set, for example, INT8_ARRAY. 288 let type: number = napitest.TypedArrayTypes["INT8_ARRAY"]; 289 let script: string = ` 290 createTypedArray(${type}) 291 `; 292 let result = napitest.runJsVm(script); 293 hilog.info(0x0000, 'testJSVM', 'Test JSVM CreateTypedArray: %{public}s', result); 294} catch (error) { 295 hilog.error(0x0000, 'testJSVM', 'Test JSVM CreateTypedArray error: %{public}s', error.message); 296} 297try { 298 // Pass in the TypedArray type to set, for example, INT32_ARRAY. 299 let type: number = napitest.TypedArrayTypes["INT32_ARRAY"]; 300 let str: string = `createTypedArray(${type})`; 301 let result = napitest.runJsVm(str); 302 hilog.info(0x0000, 'testJSVM', 'Test JSVM CreateTypedArray: %{public}s', result); 303} catch (error) { 304 hilog.error(0x0000, 'testJSVM', 'Test JSVM CreateTypedArray error: %{public}s', error.message); 305} 306``` 307 308### OH_JSVM_CreateDataview 309 310Creates a JS **DataView** object for an **ArrayBuffer**. The **DataView** object provides an array-like view of over an underlying data buffer. 311 312CPP code: 313 314```cpp 315// Register the CreateDataView callback. 316static JSVM_CallbackStruct param[] = { 317 {.data = nullptr, .callback = CreateDataView}, 318}; 319static JSVM_CallbackStruct *method = param; 320// Set a property descriptor named createDataView and associate it with a callback. This allows the CreateDataView callback to be called from JS. 321static JSVM_PropertyDescriptor descriptor[] = { 322 {"createDataView", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 323}; 324static int DIFF_VALUE_FOUR = 4; 325static int DIFF_VALUE_TWELVE = 12; 326// Define OH_JSVM_CreateDataview. 327static JSVM_Value CreateDataView(JSVM_Env env, JSVM_CallbackInfo info) 328{ 329 // Obtain the two parameters passed from JS. 330 size_t argc = 2; 331 JSVM_Value args[2] = {nullptr}; 332 JSVM_Value arrayBuffer = nullptr; 333 JSVM_Value result = nullptr; 334 // Byte length of DataView. 335 size_t byteLength = DIFF_VALUE_TWELVE; 336 // Offset of the byte. 337 size_t byteOffset = DIFF_VALUE_FOUR; 338 // Obtain the parameters of the callback. 339 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 340 // Convert the parameter into the object type. 341 OH_JSVM_CoerceToObject(env, args[0], &arrayBuffer); 342 // Create a DataView object with the specified byte length and offset. 343 JSVM_Status status = OH_JSVM_CreateDataview(env, byteLength, arrayBuffer, byteOffset, &result); 344 // Obtain the pointer to the DataView object and the length. 345 uint8_t *data = nullptr; 346 size_t length = 0; 347 // Assign values to DataView. 348 for (size_t i = 0; i < length; i++) { 349 data[i] = static_cast<uint8_t>(i + 1); 350 } 351 int32_t infoType; 352 OH_JSVM_GetValueInt32(env, args[1], &infoType); 353 size_t returnLength; 354 JSVM_Value returnArrayBuffer = nullptr; 355 size_t returnOffset; 356 enum InfoType { BYTE_LENGTHE, ARRAY_BUFFERE, BYTE_OFFSET }; 357 // Obtain DataView information. 358 OH_JSVM_GetDataviewInfo(env, result, &returnLength, (void **)&data, &returnArrayBuffer, &returnOffset); 359 JSVM_Value returnResult = nullptr; 360 switch (infoType) { 361 case BYTE_LENGTHE: 362 JSVM_Value len; 363 OH_JSVM_CreateInt32(env, returnLength, &len); 364 returnResult = len; 365 if (status != JSVM_OK) { 366 OH_LOG_ERROR(LOG_APP, "JSVM CreateDataView fail"); 367 } else { 368 OH_LOG_INFO(LOG_APP, "JSVM CreateDataView success returnLength:%{public}d", returnLength); 369 } 370 break; 371 case ARRAY_BUFFERE: 372 bool isArraybuffer; 373 OH_JSVM_IsArraybuffer(env, returnArrayBuffer, &isArraybuffer); 374 JSVM_Value isArray; 375 OH_JSVM_GetBoolean(env, isArraybuffer, &isArray); 376 returnResult = isArray; 377 if (status != JSVM_OK) { 378 OH_LOG_ERROR(LOG_APP, "JSVM CreateDataView fail"); 379 } else { 380 OH_LOG_INFO(LOG_APP, "JSVM CreateDataView success isArraybuffer:%{public}d", isArraybuffer); 381 } 382 break; 383 case BYTE_OFFSET: 384 JSVM_Value offset; 385 OH_JSVM_CreateInt32(env, returnOffset, &offset); 386 returnResult = offset; 387 if (status != JSVM_OK) { 388 OH_LOG_ERROR(LOG_APP, "JSVM CreateDataView fail"); 389 } else { 390 OH_LOG_INFO(LOG_APP, "JSVM CreateDataView success returnOffset:%{public}d", returnOffset); 391 } 392 break; 393 default: 394 break; 395 } 396 return returnResult; 397} 398``` 399 400ArkTS code: 401 402```ts 403import hilog from "@ohos.hilog" 404// Import the native APIs. 405import napitest from "libentry.so" 406try { 407 let BYTE_LENGTGH = 0; 408 let str: string = ` 409 createDataView(new ArrayBuffer(16), ${BYTE_LENGTGH}) 410 `; 411 let result = napitest.runJsVm(str); 412 hilog.info(0x0000, 'testJSVM', 'Test JSVM createDataView len: %{public}s', result); 413} catch (error) { 414 hilog.error(0x0000, 'testJSVM', 'Test JSVM createDataView error: %{public}s', error.message); 415} 416try { 417 let IS_ARRAYBUFFER = 1; 418 let str: string = ` 419 createDataView(new ArrayBuffer(16), ${IS_ARRAYBUFFER}) 420 `; 421 let result = napitest.runJsVm(str); 422 hilog.info(0x0000, 'testJSVM', 'Test JSVM createDataView is buffer: %{public}s', result); 423} catch (error) { 424 hilog.error(0x0000, 'testJSVM', 'Test JSVM createDataView error: %{public}s', error.message); 425} 426try { 427 let BYTE_OFFSET = 2; 428 let str: string = ` 429 createDataView(new ArrayBuffer(16), ${BYTE_OFFSET}) 430 `; 431 let result = napitest.runJsVm(str); 432 hilog.info(0x0000, 'testJSVM', 'Test JSVM createDataView offset: %{public}s', result); 433} catch (error) { 434 hilog.error(0x0000, 'testJSVM', 'Test JSVM createDataView error: %{public}s', error.message); 435} 436``` 437 438### OH_JSVM_GetArrayLength 439 440Obtains the length of an array. 441 442CPP code: 443 444```cpp 445// hello.cpp 446#include "napi/native_api.h" 447#include "ark_runtime/jsvm.h" 448#include <hilog/log.h> 449// Register the GetArrayLength callback. 450static JSVM_CallbackStruct param[] = { 451 {.data = nullptr, .callback = GetArrayLength}, 452}; 453static JSVM_CallbackStruct *method = param; 454// Set a property descriptor named getArrayLength and associate it with a callback. This allows the GetArrayLength callback to be called from JS. 455static JSVM_PropertyDescriptor descriptor[] = { 456 {"getArrayLength", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 457}; 458// Define OH_JSVM_GetArrayLength. 459static JSVM_Value GetArrayLength(JSVM_Env env, JSVM_CallbackInfo info) 460{ 461 size_t argc = 1; 462 JSVM_Value args[1] = {nullptr}; 463 JSVM_Value result = nullptr; 464 uint32_t length; 465 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 466 // Check whether the parameter is an array. 467 bool isArray = false; 468 OH_JSVM_IsArray(env, args[0], &isArray); 469 if (!isArray) { 470 OH_LOG_INFO(LOG_APP, "JSVM Argument must be an array"); 471 return nullptr; 472 } 473 OH_JSVM_GetArrayLength(env, args[0], &length); 474 // Create a return value. 475 OH_JSVM_CreateInt32(env, length, &result); 476 OH_LOG_INFO(LOG_APP, "JSVM length: %{public}d", length); 477 return result; 478} 479``` 480 481ArkTS code: 482 483```ts 484import hilog from "@ohos.hilog" 485// Import the native APIs. 486import napitest from "libentry.so" 487let data = '[0, 1, 2, 3, 4, 5]'; 488let str: string = ` 489 getArrayLength(${data}) 490 `; 491try { 492 let result = napitest.runJsVm(str); 493 hilog.info(0x0000, 'testJSVM', 'Test JSVM GetArrayLength: %{public}s', result); 494} catch (error) { 495 hilog.error(0x0000, 'testJSVM', 'Test JSVM GetArrayLength error: %{public}s', error.message); 496} 497``` 498 499### OH_JSVM_GetTypedarrayInfo 500 501Obtains information about a **TypedArray** object. 502 503CPP code: 504 505```cpp 506// hello.cpp 507#include "napi/native_api.h" 508#include "ark_runtime/jsvm.h" 509#include <hilog/log.h> 510// Register the GetTypedArrayInfo callback. 511static JSVM_CallbackStruct param[] = { 512 {.data = nullptr, .callback = GetTypedArrayInfo}, 513}; 514static JSVM_CallbackStruct *method = param; 515// Set a property descriptor named getTypedArrayInfo and associate it with a callback. This allows the GetTypedArrayInfo callback to be called from JS. 516static JSVM_PropertyDescriptor descriptor[] = { 517 {"getTypedArrayInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 518}; 519// Define OH_JSVM_GetTypedarrayInfo. 520static JSVM_Value GetTypedArrayInfo(JSVM_Env env, JSVM_CallbackInfo info) 521{ 522 // Obtain and parse the parameters passed to a JS callback within a JSVM. The first parameter is the TypedArray type of the information to obtain, and the second parameter is the enums of the information type to obtain. 523 size_t argc = 2; 524 JSVM_Value args[2] = {nullptr}; 525 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 526 527 // Convert the second parameter to the int32 type for comparison. 528 int32_t infoTypeParam; 529 OH_JSVM_GetValueInt32(env, args[1], &infoTypeParam); 530 // Define the infoType enums in the same sequence as those in ArkTS. 531 enum InfoType { INFO_TYPE, INFO_LENGTH, INFO_ARRAY_BUFFER, INFO_BYTE_OFFSET }; 532 void *data; 533 JSVM_TypedarrayType type; 534 size_t byteOffset, length; 535 JSVM_Value arrayBuffer = nullptr; 536 // Call OH_JSVM_GetTypedarrayInfo to obtain TypedArray information. 537 JSVM_Status status = OH_JSVM_GetTypedarrayInfo(env, args[0], &type, &length, &data, &arrayBuffer, &byteOffset); 538 JSVM_Value result = nullptr; 539 // Return the property value based on the property name. 540 switch (infoTypeParam) { 541 case INFO_TYPE: 542 // If the input parameter is TypedArray data of the int8 type, the value type is JSVM_INT8_ARRAY. 543 JSVM_Value int8_type; 544 OH_JSVM_GetBoolean(env, type == JSVM_INT8_ARRAY, &int8_type); 545 result = int8_type; 546 if (status != JSVM_OK) { 547 OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); 548 } else { 549 OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success is JSVM_INT8_ARRAY:%{public}d", type == JSVM_INT8_ARRAY); 550 } 551 break; 552 case INFO_LENGTH: 553 // Number of elements in the TypedArray object. 554 JSVM_Value jsvmLength; 555 OH_JSVM_CreateInt32(env, length, &jsvmLength); 556 result = jsvmLength; 557 if (status != JSVM_OK) { 558 OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); 559 } else { 560 OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success length:%{public}d", length); 561 } 562 break; 563 case INFO_BYTE_OFFSET: 564 // Byte offset of the first TypedArray element in the native array. 565 JSVM_Value jsvmOffset; 566 OH_JSVM_CreateInt32(env, byteOffset, &jsvmOffset); 567 result = jsvmOffset; 568 if (status != JSVM_OK) { 569 OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); 570 } else { 571 OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success byteOffset:%{public}d", byteOffset); 572 } 573 break; 574 case INFO_ARRAY_BUFFER: 575 // ArrayBuffer under TypedArray. 576 bool isArrayBuffer; 577 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer); 578 JSVM_Value isArray; 579 OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray); 580 result = isArray; 581 if (status != JSVM_OK) { 582 OH_LOG_ERROR(LOG_APP, "JSVM GetTypedArrayInfo fail"); 583 } else { 584 OH_LOG_INFO(LOG_APP, "JSVM GetTypedArrayInfo success isArrayBuffer:%{public}d", isArrayBuffer); 585 } 586 break; 587 default: 588 break; 589 } 590 return result; 591} 592``` 593 594ArkTS code: 595 596```ts 597import hilog from "@ohos.hilog" 598// Import the native APIs. 599import napitest from "libentry.so" 600try { 601 // is JSVM_INT8_ARRAY 602 let str: string = ` 603 getTypedArrayInfo(new Int8Array(3), 0) 604 `; 605 let result = napitest.runJsVm(str); 606 hilog.info(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo: %{public}s', result); 607} catch (error) { 608 hilog.error(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo error: %{public}s', error.message); 609} 610try { 611 // Length 612 let str: string = ` 613 getTypedArrayInfo(new Int8Array(5), 1) 614 `; 615 let result = napitest.runJsVm(str); 616 hilog.info(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo: %{public}s', result); 617} catch (error) { 618 hilog.error(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo error: %{public}s', error.message); 619} 620try { 621 // is_arraybuffer 622 let str: string = ` 623 getTypedArrayInfo(new Int8Array(5), 2) 624 `; 625 let result = napitest.runJsVm(str); 626 hilog.info(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo: %{public}s', result); 627} catch (error) { 628 hilog.error(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo error: %{public}s', error.message); 629} 630try { 631 // Byte offset. 632 let str: string = ` 633 getTypedArrayInfo(new Int8Array(1), 3) 634 `; 635 let result = napitest.runJsVm(str); 636 hilog.info(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo: %{public}s', result); 637} catch (error) { 638 hilog.error(0x0000, 'testJSVM', 'Test JSVM GetTypedArrayInfo error: %{public}s', error.message); 639} 640``` 641 642### OH_JSVM_GetDataviewInfo 643 644Obtains information of a **DataView** object. 645 646CPP code: 647 648```cpp 649// hello.cpp 650#include "napi/native_api.h" 651#include "ark_runtime/jsvm.h" 652#include <hilog/log.h> 653// Register the GetDataViewInfo callback. 654static JSVM_CallbackStruct param[] = { 655 {.data = nullptr, .callback = GetDataViewInfo}, 656}; 657static JSVM_CallbackStruct *method = param; 658// Set a property descriptor named getDataViewInfo and associate it with a callback. This allows the GetDataViewInfo callback to be called from JS. 659static JSVM_PropertyDescriptor descriptor[] = { 660 {"getDataViewInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 661}; 662// Define OH_JSVM_GetDataviewInfo. 663static JSVM_Value GetDataViewInfo(JSVM_Env env, JSVM_CallbackInfo info) 664{ 665 // Obtain and parse the parameters passed to a JS callback within a JSVM. The first parameter is the DataView type of the information to obtain, and the second parameter is the enums of the information type to obtain. 666 size_t argc = 2; 667 JSVM_Value args[2] = {nullptr}; 668 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 669 // Convert the second parameter to an int32 number. 670 int32_t infoType; 671 OH_JSVM_GetValueInt32(env, args[1], &infoType); 672 size_t byteLength; 673 void *data; 674 JSVM_Value arrayBuffer = nullptr; 675 size_t byteOffset; 676 // Define the infoType enums in the same sequence as those in ArkTS. 677 enum infoTypeEnum { BYTE_LENGTHE, ARRAY_BUFFERE, BYTE_OFFSET }; 678 // Obtain DataView information. 679 JSVM_Status status = OH_JSVM_GetDataviewInfo(env, args[0], &byteLength, &data, &arrayBuffer, &byteOffset); 680 JSVM_Value result = nullptr; 681 switch (infoType) { 682 case BYTE_LENGTHE: 683 // Return the length of DataView. 684 JSVM_Value len; 685 OH_JSVM_CreateInt32(env, byteLength, &len); 686 result = len; 687 if (status != JSVM_OK) { 688 OH_LOG_ERROR(LOG_APP, "JSVM GetDataViewInfo fail"); 689 } else { 690 OH_LOG_INFO(LOG_APP, "JSVM GetDataViewInfo success byteLength:%{public}d", byteLength); 691 } 692 break; 693 case ARRAY_BUFFERE: 694 // Check whether data in Info of DataView is an ArrayBuffer object. 695 bool isArrayBuffer; 696 OH_JSVM_IsArraybuffer(env, arrayBuffer, &isArrayBuffer); 697 JSVM_Value isArray; 698 OH_JSVM_GetBoolean(env, isArrayBuffer, &isArray); 699 result = isArray; 700 if (status != JSVM_OK) { 701 OH_LOG_ERROR(LOG_APP, "JSVM GetDataViewInfo fail"); 702 } else { 703 OH_LOG_INFO(LOG_APP, "JSVM GetDataViewInfo success isArrayBuffer:%{public}d", isArrayBuffer); 704 } 705 break; 706 case BYTE_OFFSET: 707 // Return the offset of DataView. 708 JSVM_Value offset; 709 OH_JSVM_CreateInt32(env, byteOffset, &offset); 710 result = offset; 711 if (status != JSVM_OK) { 712 OH_LOG_ERROR(LOG_APP, "JSVM GetDataViewInfo fail"); 713 } else { 714 OH_LOG_INFO(LOG_APP, "JSVM GetDataViewInfo success byteOffset:%{public}d", byteOffset); 715 } 716 break; 717 default: 718 break; 719 } 720 return result; 721} 722``` 723 724ArkTS code: 725 726```ts 727import hilog from "@ohos.hilog" 728// Import the native APIs. 729import napitest from "libentry.so" 730try { 731 // Byte length. 732 let script: string = `getDataViewInfo(new DataView(new Int8Array([2,5]).buffer), 0)`; 733 let result = napitest.runJsVm(script); 734 hilog.info(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo: %{public}s', result); 735} catch (error) { 736 hilog.error(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo error: %{public}s', error.message); 737} 738try { 739 // Check whether the data is an ArrayBuffer object. 740 let data = `'a'`; 741 let isarraybuffer = '1'; 742 let script: string = `getDataViewInfo(${data}, ${isarraybuffer})`; 743 let result = napitest.runJsVm(script); 744 hilog.info(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo: %{public}s', result); 745} catch (error) { 746 hilog.error(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo error: %{public}s', error.message); 747} 748try { 749 // Check whether the data is an ArrayBuffer object. 750 let data = 'new DataView(new Int8Array([2,5,3]).buffer)'; 751 let isarraybuffer = '1'; 752 let script: string = `getDataViewInfo(${data}, ${isarraybuffer})`; 753 let result = napitest.runJsVm(script); 754 hilog.info(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo: %{public}s', result); 755} catch (error) { 756 hilog.error(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo error: %{public}s', error.message); 757} 758try { 759 // byte_offset 760 let data = 'new DataView(new Int8Array([2,5,3]).buffer)'; 761 let isarraybuffer = '2'; 762 let script: string = `getDataViewInfo(${data}, ${isarraybuffer})`; 763 let result = napitest.runJsVm(script); 764 hilog.info(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo: %{public}s', result); 765} catch (error) { 766 hilog.error(0x0000, 'testJSVM', 'Test JSVM getDataViewInfo error: %{public}s', error.message); 767} 768``` 769 770### OH_JSVM_IsArray 771 772Checks whether a JS object is an array. 773 774CPP code: 775 776```cpp 777// hello.cpp 778#include "napi/native_api.h" 779#include "ark_runtime/jsvm.h" 780#include <hilog/log.h> 781// Register the IsArray callback. 782static JSVM_CallbackStruct param[] = { 783 {.data = nullptr, .callback = IsArray}, 784}; 785static JSVM_CallbackStruct *method = param; 786// Set a property descriptor named isArray and associate it with a callback. This allows the IsArray callback to be called from JS. 787static JSVM_PropertyDescriptor descriptor[] = { 788 {"isArray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 789}; 790// Define OH_JSVM_IsArray. 791static JSVM_Value IsArray(JSVM_Env env, JSVM_CallbackInfo info) 792{ 793 size_t argc = 1; 794 JSVM_Value args[1] = {nullptr}; 795 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 796 bool result = false; 797 JSVM_Status status = OH_JSVM_IsArray(env, args[0], &result); 798 JSVM_Value returnValue = nullptr; 799 OH_JSVM_GetBoolean(env, result, &returnValue); 800 if (status != JSVM_OK) { 801 OH_LOG_ERROR(LOG_APP, "JSVM IsArray fail"); 802 } else { 803 OH_LOG_INFO(LOG_APP, "JSVM IsArray success:%{public}d", result); 804 } 805 return returnValue; 806} 807``` 808 809ArkTS code: 810 811```ts 812import hilog from "@ohos.hilog" 813// Import the native APIs. 814import napitest from "libentry.so" 815let data = '[1, 2, 3, 4, 5]'; 816let script: string = ` 817 isArray(${data}) 818 ` 819try { 820 let result = napitest.runJsVm(script); 821 hilog.info(0x0000, 'JSVM', 'IsArray: %{public}s', result); 822} catch (error) { 823 hilog.error(0x0000, 'JSVM', 'IsArray: %{public}s', error.message); 824} 825``` 826 827### OH_JSVM_SetElement 828 829Sets an element at the specified index for a JS object. 830 831CPP code: 832 833```cpp 834// hello.cpp 835#include "napi/native_api.h" 836#include "ark_runtime/jsvm.h" 837#include <hilog/log.h> 838// Register the SetElement callback. 839static JSVM_CallbackStruct param[] = { 840 {.data = nullptr, .callback = SetElement}, 841}; 842static JSVM_CallbackStruct *method = param; 843// Set a property descriptor named setElement and associate it with a callback. This allows the SetElement callback to be called from JS. 844static JSVM_PropertyDescriptor descriptor[] = { 845 {"setElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 846}; 847// Define OH_JSVM_SetElement. 848static int DIFF_VALUE_THREE = 3; 849static JSVM_Value SetElement(JSVM_Env env, JSVM_CallbackInfo info) { 850 size_t argc = DIFF_VALUE_THREE; 851 JSVM_Value args[3] = {nullptr}; 852 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 853 int32_t index = 0; 854 OH_JSVM_GetValueInt32(env, args[1], &index); 855 JSVM_Status status = OH_JSVM_SetElement(env, args[0], index, args[2]); 856 if (status != JSVM_OK) { 857 OH_LOG_ERROR(LOG_APP, "JSVM SetElement fail"); 858 } else { 859 OH_LOG_INFO(LOG_APP, "JSVM SetElement success"); 860 } 861 return args[0]; 862} 863``` 864 865ArkTS code: 866 867```ts 868import hilog from "@ohos.hilog" 869// Import the native APIs. 870import napitest from "libentry.so" 871let script: string = ` 872 setElement(3) 873` 874try { 875 let result = napitest.runJsVm(script); 876 hilog.info(0x0000, 'testJSVM', 'Test JSVM setElement: %{public}s', result); 877} catch (error) { 878 hilog.error(0x0000, 'testJSVM', 'Test JSVM setElement error: %{public}s', error.message); 879} 880``` 881 882### OH_JSVM_GetElement 883 884Obtains the element at the specified index of a JS object. 885 886CPP code: 887 888```cpp 889// hello.cpp 890#include "napi/native_api.h" 891#include "ark_runtime/jsvm.h" 892#include <hilog/log.h> 893// Register the GetElement callback. 894static JSVM_CallbackStruct param[] = { 895 {.data = nullptr, .callback = GetElement}, 896}; 897static JSVM_CallbackStruct *method = param; 898// Set a property descriptor named getElement and associate it with a callback. This allows the GetElement callback to be called from JS. 899static JSVM_PropertyDescriptor descriptor[] = { 900 {"getElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 901}; 902// Define OH_JSVM_GetElement. 903static JSVM_Value GetElement(JSVM_Env env, JSVM_CallbackInfo info) { 904 // Obtain the two parameters passed from JS. 905 size_t argc = 2; 906 JSVM_Value args[2] = {nullptr}; 907 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 908 // Obtain the index value of the element. 909 uint32_t index; 910 OH_JSVM_GetValueUint32(env, args[1], &index); 911 // Obtain the element value at the index and store it in result. 912 JSVM_Value result = nullptr; 913 JSVM_Status status = OH_JSVM_GetElement(env, args[0], index, &result); 914 if (status != JSVM_OK) { 915 OH_LOG_ERROR(LOG_APP, "JSVM GetElement fail"); 916 } else { 917 OH_LOG_INFO(LOG_APP, "JSVM GetElement success"); 918 } 919 return result; 920} 921``` 922 923ArkTS code: 924 925```ts 926import hilog from "@ohos.hilog" 927// Import the native APIs. 928import napitest from "libentry.so" 929let script: string = ` 930 let arr = [10, 'hello', null, true]; 931 getElement(arr, 3) 932` 933try { 934 let result = napitest.runJsVm(script); 935 hilog.info(0x0000, 'testJSVM', 'Test JSVM getElement: %{public}s', result); 936} catch (error) { 937 hilog.error(0x0000, 'testJSVM', 'Test JSVM getElement error: %{public}s', error.message); 938} 939``` 940 941### OH_JSVM_HasElement 942 943Checks whether a JS object has an element at the specified index. 944 945CPP code: 946 947```cpp 948// hello.cpp 949#include "napi/native_api.h" 950#include "ark_runtime/jsvm.h" 951#include <hilog/log.h> 952// Register the HasElement callback. 953static JSVM_CallbackStruct param[] = { 954 {.data = nullptr, .callback = HasElement}, 955}; 956static JSVM_CallbackStruct *method = param; 957// Set a property descriptor named hasElement and associate it with a callback. This allows the HasElement callback to be called from JS. 958static JSVM_PropertyDescriptor descriptor[] = { 959 {"hasElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 960}; 961// Define OH_JSVM_HasElement. 962static JSVM_Value HasElement(JSVM_Env env, JSVM_CallbackInfo info) 963{ 964 // Obtain the two parameters passed from JS. 965 size_t argc = 2; 966 JSVM_Value args[2] = {nullptr}; 967 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 968 // Obtain the index of the element to be checked. 969 uint32_t index; 970 OH_JSVM_GetValueUint32(env, args[1], &index); 971 // Check whether the element exists based on the given index. 972 bool hasElement = true; 973 JSVM_Status status = OH_JSVM_HasElement(env, args[0], index, &hasElement); 974 // Convert the boolean value to JSVM_Value and return it. 975 JSVM_Value result = nullptr; 976 OH_JSVM_GetBoolean(env, hasElement, &result); 977 if (status != JSVM_OK) { 978 OH_LOG_ERROR(LOG_APP, "JSVM hasElement fail"); 979 } else { 980 OH_LOG_INFO(LOG_APP, "JSVM JSVM hasElement: %{public}d", hasElement); 981 } 982 return result; 983} 984``` 985 986ArkTS code: 987 988```ts 989import hilog from "@ohos.hilog" 990// Import the native APIs. 991import napitest from "libentry.so" 992let script: string = ` 993 let arr = [10, 'hello', null, true]; 994` 995let scriptTrue: string = script + `\n` + ` 996 hasElement(arr, 0) 997` 998let scriptFalse: string = script + `\n` + ` 999 hasElement(arr, 4) 1000` 1001try { 1002 let resultTrue = napitest.runJsVm(scriptTrue); 1003 hilog.info(0x0000, 'testJSVM', 'Test JSVM resultTrue: %{public}s', resultTrue); 1004 let resultFalse = napitest.runJsVm(scriptFalse); 1005 hilog.info(0x0000, 'testJSVM', 'Test JSVM resultFalse: %{public}s', resultFalse); 1006} catch (error) { 1007 hilog.error(0x0000, 'testJSVM', 'Test JSVM hasElement error: %{public}s', error.message); 1008} 1009``` 1010 1011### OH_JSVM_DeleteElement 1012 1013Deletes the element at the specified index from a JS object. 1014 1015CPP code: 1016 1017```cpp 1018// hello.cpp 1019#include "napi/native_api.h" 1020#include "ark_runtime/jsvm.h" 1021#include <hilog/log.h> 1022// Register the DeleteElement callback. 1023static JSVM_CallbackStruct param[] = { 1024 {.data = nullptr, .callback = DeleteElement}, 1025}; 1026static JSVM_CallbackStruct *method = param; 1027// Set a property descriptor named deleteElement and associate it with a callback. This allows the DeleteElement callback to be called from JS. 1028static JSVM_PropertyDescriptor descriptor[] = { 1029 {"deleteElement", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 1030}; 1031// Define OH_JSVM_DeleteElement. 1032static JSVM_Value DeleteElement(JSVM_Env env, JSVM_CallbackInfo info) { 1033 // Obtain the two parameters passed from JS. 1034 size_t argc = 2; 1035 JSVM_Value args[2] = {nullptr}; 1036 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 1037 // Obtain the index of the element to delete. 1038 uint32_t index; 1039 OH_JSVM_GetValueUint32(env, args[1], &index); 1040 // Delete the element at the specified index. 1041 bool deleted = true; 1042 JSVM_Status status = OH_JSVM_DeleteElement(env, args[0], index, &deleted); 1043 // Convert the boolean value to JSVM_Value and return it. 1044 JSVM_Value result = nullptr; 1045 OH_JSVM_GetBoolean(env, deleted, &result); 1046 if (status != JSVM_OK) { 1047 OH_LOG_ERROR(LOG_APP, "JSVM DeleteElement fail"); 1048 } else { 1049 OH_LOG_INFO(LOG_APP, "JSVM JSVM DeleteElement: %{public}d", deleted); 1050 } 1051 return result; 1052} 1053``` 1054 1055ArkTS code: 1056 1057```ts 1058import hilog from "@ohos.hilog" 1059// Import the native APIs. 1060import napitest from "libentry.so" 1061let script: string = ` 1062 let arr = [10, 'hello', null, true]; 1063 deleteElement(arr, 0) 1064` 1065try { 1066 let result = napitest.runJsVm(script); 1067 hilog.info(0x0000, 'testJSVM', 'Test JSVM deleteElement: %{public}s', result); 1068} catch (error) { 1069 hilog.error(0x0000, 'testJSVM', 'Test JSVM deleteElement error: %{public}s', error.message); 1070} 1071``` 1072 1073### OH_JSVM_IsDataview 1074 1075Checks whether a JS object is a **DataView** object. 1076 1077CPP code: 1078 1079```cpp 1080// hello.cpp 1081#include "napi/native_api.h" 1082#include "ark_runtime/jsvm.h" 1083#include <hilog/log.h> 1084// Register the IsDataView callback. 1085static JSVM_CallbackStruct param[] = { 1086 {.data = nullptr, .callback = IsDataView}, 1087}; 1088static JSVM_CallbackStruct *method = param; 1089// Set a property descriptor named isDataView and associate it with a callback. This allows the IsDataView callback to be called from JS. 1090static JSVM_PropertyDescriptor descriptor[] = { 1091 {"isDataView", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 1092}; 1093// Define OH_JSVM_IsDataview. 1094static JSVM_Value IsDataView(JSVM_Env env, JSVM_CallbackInfo info) { 1095 size_t argc = 1; 1096 JSVM_Value args[1] = {nullptr}; 1097 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 1098 // Call OH_JSVM_IsDataview to check whether the input parameter is a DataView object. 1099 bool result = false; 1100 JSVM_Status status = OH_JSVM_IsDataview(env, args[0], &result); 1101 JSVM_Value isDateView = nullptr; 1102 OH_JSVM_GetBoolean(env, result, &isDateView); 1103 if (status != JSVM_OK) { 1104 OH_LOG_ERROR(LOG_APP, "JSVM IsDataView fail"); 1105 } else { 1106 OH_LOG_INFO(LOG_APP, "JSVM JSVM IsDataView: %{public}d", result); 1107 } 1108 return isDateView; 1109} 1110``` 1111 1112ArkTS code: 1113 1114```ts 1115import hilog from "@ohos.hilog" 1116// Import the native APIs. 1117import napitest from "libentry.so" 1118let script: string = ` 1119let buffer = new ArrayBuffer(16); 1120let dataView = new DataView(buffer); 1121isDataView(dataView); 1122 `; 1123try { 1124 let result = napitest.runJsVm(script); 1125 hilog.info(0x0000, 'JSVM', 'IsDataView: %{public}s', result); 1126} catch (error) { 1127 hilog.error(0x0000, 'JSVM', 'IsDataView: %{public}s', error.message); 1128} 1129``` 1130 1131### OH_JSVM_IsTypedarray 1132 1133Checks whether a JS object is a **TypedArray** object. 1134 1135CPP code: 1136 1137```cpp 1138// hello.cpp 1139#include "napi/native_api.h" 1140#include "ark_runtime/jsvm.h" 1141#include <hilog/log.h> 1142// Register the IsTypedarray callback. 1143static JSVM_CallbackStruct param[] = { 1144 {.data = nullptr, .callback = IsTypedarray}, 1145}; 1146static JSVM_CallbackStruct *method = param; 1147// Set a property descriptor named isTypedarray and associate it with a callback. This allows the IsTypedarray callback to be called from JS. 1148static JSVM_PropertyDescriptor descriptor[] = { 1149 {"isTypedarray", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 1150}; 1151// Define OH_JSVM_IsTypedarray. 1152static JSVM_Value IsTypedarray(JSVM_Env env, JSVM_CallbackInfo info) { 1153 size_t argc = 1; 1154 JSVM_Value args[1] = {nullptr}; 1155 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 1156 bool result = false; 1157 JSVM_Status status = OH_JSVM_IsTypedarray(env, args[0], &result); 1158 JSVM_Value isTypedArray = nullptr; 1159 OH_JSVM_GetBoolean(env, result, &isTypedArray); 1160 if (status != JSVM_OK) { 1161 OH_LOG_ERROR(LOG_APP, "JSVM IsTypedarray fail"); 1162 } else { 1163 OH_LOG_INFO(LOG_APP, "JSVM JSVM IsTypedarray: %{public}d", result); 1164 } 1165 return isTypedArray; 1166} 1167``` 1168 1169ArkTS code: 1170 1171```ts 1172import hilog from "@ohos.hilog" 1173// Import the native APIs. 1174import napitest from "libentry.so" 1175let script: string = ` 1176 isTypedarray(new Uint16Array([1, 2, 3, 4])) 1177 ` 1178try { 1179 let result = napitest.runJsVm(script); 1180 hilog.info(0x0000, 'JSVM', 'IsTypedArray: %{public}s', result); 1181} catch (error) { 1182 hilog.error(0x0000, 'JSVM', 'IsTypedArray: %{public}s', error.message); 1183} 1184``` 1185