1e41f4b71Sopenharmony_ci# Working with BigInt Using JSVM-API 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciBigInt is a data type used to represent integers of any precision in JavaScript (JS), with values greater than the value range of the Number type. You can use JSVM-API to create, obtain, and operate JS BigInt values. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Basic Concepts 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciBefore using JSVM-API to operate BigInt values, you need to understand the following basic concepts: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- BigInt: a data type used to represent integers of any precision in JS. Different from the Number type, BigInt can accurately represent very large integers without losing precision or causing overflows. 12e41f4b71Sopenharmony_ci- BigInt creation: You can use JSVM-API to create a JS BigInt object from a C **Int64** or **Uint64** value. This makes it easy to create BigInt values using C/C++. 13e41f4b71Sopenharmony_ci- BigInt operation: JSVM-API provides APIs for operating BigInt values. You can use these APIs to obtain and convert BigInt values and perform arithmetic and bitwise operations. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Available APIs 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| API | Description | 18e41f4b71Sopenharmony_ci| ---------------------------- | ---------------------------------------- | 19e41f4b71Sopenharmony_ci| OH_JSVM_CreateBigintInt64 | Creates a JS BigInt object from a C int64_t object.| 20e41f4b71Sopenharmony_ci| OH_JSVM_CreateBigintUint64 | Creates a JS BigInt object from a C uint64_t object.| 21e41f4b71Sopenharmony_ci| OH_JSVM_CreateBigintWords | Creates a JS BigInt object from a C uint64_t array.| 22e41f4b71Sopenharmony_ci| OH_JSVM_GetValueBigintInt64 | Obtains the C int64_t primitive equivalent of the given JS BigInt. If necessary, it truncates the value and sets **lossless** to **false**. | 23e41f4b71Sopenharmony_ci| OH_JSVM_GetValueBigintUint64 | Obtains the C uint64_t primitive equivalent of the given JS BigInt. If necessary, it truncates the value and sets **lossless** to **false**. | 24e41f4b71Sopenharmony_ci| OH_JSVM_GetValueBigintWords | Obtains the underlying data of a given JS BigInt object, that is, the word representation of BigInt data. Both **signBit** and **words** can be set to **NULL**. In this case, only **wordCount** is obtained.| 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci## Example 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciIf 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 related to BigInt operations. 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci### OH_JSVM_GetValueBigintWords 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciUse **OH_JSVM_GetValueBigintWords** to obtain the underlying data of a given JS BigInt object, that is, the word representation of BigInt data. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciCPP code: 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci```cpp 37e41f4b71Sopenharmony_ci// hello.cpp 38e41f4b71Sopenharmony_ci#include "napi/native_api.h" 39e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 40e41f4b71Sopenharmony_ci#include <hilog/log.h> 41e41f4b71Sopenharmony_ci// Register the GetValueBigintWords callback. 42e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 43e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetValueBigintWords}, 44e41f4b71Sopenharmony_ci}; 45e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 46e41f4b71Sopenharmony_ci// Set a property descriptor named getValueBigintWords and associate it with a callback. This allows the GetValueBigintWords callback to be called from JS. 47e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 48e41f4b71Sopenharmony_ci {"getValueBigintWords", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 49e41f4b71Sopenharmony_ci}; 50e41f4b71Sopenharmony_ci// Define OH_JSVM_GetValueBigintWords. 51e41f4b71Sopenharmony_cistatic JSVM_Value GetValueBigintWords(JSVM_Env env, JSVM_CallbackInfo info) 52e41f4b71Sopenharmony_ci{ 53e41f4b71Sopenharmony_ci size_t argc = 1; 54e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 55e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 56e41f4b71Sopenharmony_ci int signBit = 0; 57e41f4b71Sopenharmony_ci size_t wordCount = 0; 58e41f4b71Sopenharmony_ci uint64_t words; 59e41f4b71Sopenharmony_ci // Call OH_JSVM_GetValueBigintWords to obtain wordCount. 60e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetValueBigintWords(env, args[0], nullptr, &wordCount, nullptr); 61e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_JSVM_GetValueBigintWords wordCount:%{public}d.", wordCount); 62e41f4b71Sopenharmony_ci // Call OH_JSVM_GetValueBigintWords to obtain BigInt information, such as whether the value passed by signBit is a positive or negative number. 63e41f4b71Sopenharmony_ci status = OH_JSVM_GetValueBigintWords(env, args[0], &signBit, &wordCount, &words); 64e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 65e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "OH_JSVM_GetValueBigintWords fail, status:%{public}d.", status); 66e41f4b71Sopenharmony_ci } else { 67e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "OH_JSVM_GetValueBigintWords signBit: %{public}d.", signBit); 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci // Convert the sign bit into a value of Int type and pass it. 70e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 71e41f4b71Sopenharmony_ci OH_JSVM_CreateInt32(env, signBit, &returnValue); 72e41f4b71Sopenharmony_ci return returnValue; 73e41f4b71Sopenharmony_ci} 74e41f4b71Sopenharmony_ci``` 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciArkTS code: 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci```ts 79e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 80e41f4b71Sopenharmony_ci// Import the native APIs. 81e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 82e41f4b71Sopenharmony_citry { 83e41f4b71Sopenharmony_ci let script: string = `getValueBigintWords(BigInt(5555555555555555))`; 84e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 85e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueBigintWords: %{public}s', result); 86e41f4b71Sopenharmony_ci} catch (error) { 87e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueBigintWords error: %{public}s', error.message); 88e41f4b71Sopenharmony_ci} 89e41f4b71Sopenharmony_citry { 90e41f4b71Sopenharmony_ci let script: string = `getValueBigintWords(BigInt(-5555555555555555))`; 91e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 92e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueBigintWords: %{public}s', result); 93e41f4b71Sopenharmony_ci} catch (error) { 94e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueBigintWords error: %{public}s', error.message); 95e41f4b71Sopenharmony_ci} 96e41f4b71Sopenharmony_ci``` 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci### OH_JSVM_CreateBigintWords 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciUse **OH_JSVM_GetValueBigintWords** to create a JS BigInt object from a C uint64_t array. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ciCPP code: 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci```cpp 105e41f4b71Sopenharmony_ci// hello.cpp 106e41f4b71Sopenharmony_ci#include "napi/native_api.h" 107e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 108e41f4b71Sopenharmony_ci#include <hilog/log.h> 109e41f4b71Sopenharmony_ci// Register the CreateBigintWords callback. 110e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 111e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CreateBigintWords}, 112e41f4b71Sopenharmony_ci}; 113e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 114e41f4b71Sopenharmony_ci// Set a property descriptor named createBigintWords and associate it with a callback. This allows the CreateBigintWords callback to be called from JS. 115e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 116e41f4b71Sopenharmony_ci {"createBigintWords", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 117e41f4b71Sopenharmony_ci}; 118e41f4b71Sopenharmony_ci// Define OH_JSVM_CreateBigintWords. 119e41f4b71Sopenharmony_cistatic int DIFF_VALUE_THREE = 3; 120e41f4b71Sopenharmony_cistatic JSVM_Value CreateBigintWords(JSVM_Env env, JSVM_CallbackInfo info) 121e41f4b71Sopenharmony_ci{ 122e41f4b71Sopenharmony_ci // Call OH_JSVM_CreateBigintWords to create a BigInt object. 123e41f4b71Sopenharmony_ci int signBit = 0; 124e41f4b71Sopenharmony_ci size_t wordCount = DIFF_VALUE_THREE; 125e41f4b71Sopenharmony_ci uint64_t words[] = {12ULL, 34ULL, 56ULL}; 126e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 127e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CreateBigintWords(env, signBit, wordCount, words, &returnValue); 128e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 129e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateBigintWords fail"); 130e41f4b71Sopenharmony_ci } else { 131e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateBigintWords success"); 132e41f4b71Sopenharmony_ci } 133e41f4b71Sopenharmony_ci return returnValue; 134e41f4b71Sopenharmony_ci} 135e41f4b71Sopenharmony_ci``` 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ciArkTS code: 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci```ts 140e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 141e41f4b71Sopenharmony_ci// Import the native APIs. 142e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 143e41f4b71Sopenharmony_citry { 144e41f4b71Sopenharmony_ci let script: string = `createBigintWords()`; 145e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 146e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM createBigintWords: %{public}s', result); 147e41f4b71Sopenharmony_ci} catch (error) { 148e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM createBigintWords error: %{public}s', error.message); 149e41f4b71Sopenharmony_ci} 150e41f4b71Sopenharmony_ci``` 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci### OH_JSVM_CreateBigintUint64 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ciUse **OH_JSVM_CreateBigintUint64** to create a JS BigInt object from a C Uint64 object. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ciCPP code: 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci```cpp 159e41f4b71Sopenharmony_ci// hello.cpp 160e41f4b71Sopenharmony_ci#include "napi/native_api.h" 161e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 162e41f4b71Sopenharmony_ci#include <hilog/log.h> 163e41f4b71Sopenharmony_ci// Declare the variable value of uint64_t. 164e41f4b71Sopenharmony_cistatic uint64_t TEST_VALUE = 5555555555555555555; 165e41f4b71Sopenharmony_ci// Define CreateBigintUint64. 166e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 167e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CreateBigintUint64}, 168e41f4b71Sopenharmony_ci}; 169e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 170e41f4b71Sopenharmony_ci// Set a property descriptor named createBigintUint64 and associate it with a callback. This allows the CreateBigintUint64 callback to be called from JS. 171e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 172e41f4b71Sopenharmony_ci {"createBigintUint64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 173e41f4b71Sopenharmony_ci}; 174e41f4b71Sopenharmony_ci// Define OH_JSVM_CreateBigintUint64. 175e41f4b71Sopenharmony_cistatic JSVM_Value CreateBigintUint64(JSVM_Env env, JSVM_CallbackInfo info) 176e41f4b71Sopenharmony_ci{ 177e41f4b71Sopenharmony_ci // Convert value to the JSVM_Value type and return the value. 178e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 179e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CreateBigintUint64(env, TEST_VALUE, &returnValue); 180e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 181e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateBigintUint64 fail"); 182e41f4b71Sopenharmony_ci } else { 183e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateBigintUint64 success"); 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci return returnValue; 186e41f4b71Sopenharmony_ci} 187e41f4b71Sopenharmony_ci``` 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciArkTS code: 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci```ts 192e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 193e41f4b71Sopenharmony_ci// Import the native APIs. 194e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 195e41f4b71Sopenharmony_citry { 196e41f4b71Sopenharmony_ci let script: string = `createBigintUint64()`; 197e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 198e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM createBigintUint64: %{public}s', result); 199e41f4b71Sopenharmony_ci} catch (error) { 200e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM createBigintUint64 error: %{public}s', error.message); 201e41f4b71Sopenharmony_ci} 202e41f4b71Sopenharmony_ci``` 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci### OH_JSVM_GetValueBigintUint64 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ciUse **OH_JSVM_GetValueBigintUint64** to obtain the C uint64_t primitive equivalent of the given JS BigInt object. 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ciCPP code: 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci```cpp 211e41f4b71Sopenharmony_ci// hello.cpp 212e41f4b71Sopenharmony_ci#include "napi/native_api.h" 213e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 214e41f4b71Sopenharmony_ci#include <hilog/log.h> 215e41f4b71Sopenharmony_ci// Register the GetValueBigintUint64 callback. 216e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 217e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetValueBigintUint64}, 218e41f4b71Sopenharmony_ci}; 219e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 220e41f4b71Sopenharmony_ci// Set a property descriptor named getValueBigintUint64 and associate it with a callback. This allows the GetValueBigintUint64 callback to be called from JS. 221e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 222e41f4b71Sopenharmony_ci {"getValueBigintUint64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 223e41f4b71Sopenharmony_ci}; 224e41f4b71Sopenharmony_ci// Define OH_JSVM_GetValueBigintUint64. 225e41f4b71Sopenharmony_cistatic JSVM_Value GetValueBigintUint64(JSVM_Env env, JSVM_CallbackInfo info) 226e41f4b71Sopenharmony_ci{ 227e41f4b71Sopenharmony_ci size_t argc = 1; 228e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 229e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 230e41f4b71Sopenharmony_ci // Obtain the BigInt value. 231e41f4b71Sopenharmony_ci uint64_t value = 0; 232e41f4b71Sopenharmony_ci bool lossLess = false; 233e41f4b71Sopenharmony_ci OH_JSVM_GetValueBigintUint64(env, args[0], &value, &lossLess); 234e41f4b71Sopenharmony_ci // Check whether the BigInt value obtained is a product of lossless conversion. If no, an exception is thrown. 235e41f4b71Sopenharmony_ci if (!lossLess) { 236e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "BigInt values have no lossless converted"); 237e41f4b71Sopenharmony_ci return nullptr; 238e41f4b71Sopenharmony_ci } else { 239e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM GetValueBigintUint64 success:%{public}d", lossLess); 240e41f4b71Sopenharmony_ci } 241e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 242e41f4b71Sopenharmony_ci OH_JSVM_CreateBigintUint64(env, value, &returnValue); 243e41f4b71Sopenharmony_ci return returnValue; 244e41f4b71Sopenharmony_ci} 245e41f4b71Sopenharmony_ci``` 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ciAPI declaration: 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciArkTS code: 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci```ts 252e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 253e41f4b71Sopenharmony_ci// Import the native APIs. 254e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 255e41f4b71Sopenharmony_citry { 256e41f4b71Sopenharmony_ci let script: string = `getValueBigintUint64(BigInt(5555555555555555))`; 257e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 258e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getValueBigintUint64: %{public}s', result); 259e41f4b71Sopenharmony_ci} catch (error) { 260e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getValueBigintUint64 error: %{public}s', error.message); 261e41f4b71Sopenharmony_ci} 262e41f4b71Sopenharmony_ci``` 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci### OH_JSVM_CreateBigintInt64 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ciUse **OH_JSVM_CreateBigintInt64** to create a JS BigInt object from a C Int64 object. 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ciCPP code: 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci```cpp 271e41f4b71Sopenharmony_ci// hello.cpp 272e41f4b71Sopenharmony_ci#include "napi/native_api.h" 273e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 274e41f4b71Sopenharmony_ci#include <hilog/log.h> 275e41f4b71Sopenharmony_ci// Declare the variable value of int64_t. 276e41f4b71Sopenharmony_cistatic int64_t TEST_VALUE_DEMO = -5555555555555555555; 277e41f4b71Sopenharmony_ci// Register the CreateBigintInt64 callback. 278e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 279e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CreateBigintInt64}, 280e41f4b71Sopenharmony_ci}; 281e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 282e41f4b71Sopenharmony_ci// Set a property descriptor named createBigintInt64 and associate it with a callback. This allows the CreateBigintInt64 callback to be called from JS. 283e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 284e41f4b71Sopenharmony_ci {"createBigintInt64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 285e41f4b71Sopenharmony_ci}; 286e41f4b71Sopenharmony_ci// Define OH_JSVM_CreateBigintInt64. 287e41f4b71Sopenharmony_cistatic JSVM_Value CreateBigintInt64(JSVM_Env env, JSVM_CallbackInfo info) 288e41f4b71Sopenharmony_ci{ 289e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 290e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CreateBigintInt64(env, TEST_VALUE_DEMO, &returnValue); 291e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 292e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateBigintInt64 fail"); 293e41f4b71Sopenharmony_ci } else { 294e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateBigintInt64 success"); 295e41f4b71Sopenharmony_ci } 296e41f4b71Sopenharmony_ci return returnValue; 297e41f4b71Sopenharmony_ci} 298e41f4b71Sopenharmony_ci``` 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ciArkTS code: 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci```ts 303e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 304e41f4b71Sopenharmony_ci// Import the native APIs. 305e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 306e41f4b71Sopenharmony_citry { 307e41f4b71Sopenharmony_ci let script: string = `createBigintInt64()`; 308e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 309e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM createBigintInt64: %{public}s', result); 310e41f4b71Sopenharmony_ci} catch (error) { 311e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM createBigintInt64 error: %{public}s', error.message); 312e41f4b71Sopenharmony_ci} 313e41f4b71Sopenharmony_ci``` 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci### OH_JSVM_GetValueBigintInt64 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ciUse OH_JSVM_GetValueBigintInt64 to obtain the C int64_t primitive equivalent of the given JS BigInt object. 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciCPP code: 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci```cpp 322e41f4b71Sopenharmony_ci// hello.cpp 323e41f4b71Sopenharmony_ci#include "napi/native_api.h" 324e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 325e41f4b71Sopenharmony_ci#include <hilog/log.h> 326e41f4b71Sopenharmony_ci// Register the GetBigintInt64 callback. 327e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 328e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetBigintInt64}, 329e41f4b71Sopenharmony_ci}; 330e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 331e41f4b71Sopenharmony_ci// Set a property descriptor named getBigintInt64 and associate it with a callback. This allows the GetBigintInt64 callback to be called from JS. 332e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 333e41f4b71Sopenharmony_ci {"getBigintInt64", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 334e41f4b71Sopenharmony_ci}; 335e41f4b71Sopenharmony_ci// Define OH_JSVM_GetValueBigintInt64. 336e41f4b71Sopenharmony_cistatic JSVM_Value GetBigintInt64(JSVM_Env env, JSVM_CallbackInfo info) 337e41f4b71Sopenharmony_ci{ 338e41f4b71Sopenharmony_ci size_t argc = 1; 339e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 340e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 341e41f4b71Sopenharmony_ci // Obtain the 64-bit big integer from the input parameter. 342e41f4b71Sopenharmony_ci int64_t value; 343e41f4b71Sopenharmony_ci bool lossLess; 344e41f4b71Sopenharmony_ci OH_JSVM_GetValueBigintInt64(env, args[0], &value, &lossLess); 345e41f4b71Sopenharmony_ci // Check whether the BigInt value obtained is a product of lossless conversion. If no, an exception is thrown. 346e41f4b71Sopenharmony_ci if (!lossLess) { 347e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "BigInt values have no lossless converted"); 348e41f4b71Sopenharmony_ci return nullptr; 349e41f4b71Sopenharmony_ci } else { 350e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM GetBigintInt64 success:%{public}d", lossLess); 351e41f4b71Sopenharmony_ci } 352e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 353e41f4b71Sopenharmony_ci OH_JSVM_CreateBigintInt64(env, value, &returnValue); 354e41f4b71Sopenharmony_ci return returnValue; 355e41f4b71Sopenharmony_ci} 356e41f4b71Sopenharmony_ci``` 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ciArkTS code: 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci```ts 361e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 362e41f4b71Sopenharmony_ci// Import the native APIs. 363e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 364e41f4b71Sopenharmony_citry { 365e41f4b71Sopenharmony_ci let script: string = `getBigintInt64(BigInt(-5555555555555555))`; 366e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 367e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getBigintInt64: %{public}d', result); 368e41f4b71Sopenharmony_ci} catch (error) { 369e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getBigintInt64 error: %{public}s', error.message); 370e41f4b71Sopenharmony_ci} 371e41f4b71Sopenharmony_ci``` 372