1e41f4b71Sopenharmony_ci# Working with Primitives Using JSVM-API 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciJSVM-API provides APIs for converting data between C/C++ and JavaScript (JS) data types and obtaining the JS object of the specified type. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Basic Concepts 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciBefore using JSVM-API to operate JS objects, you need to understand the following basic concepts: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Conversion between JS and C/C primitives: You can use JSVM-API to convert JS values to C/C++ data types, for example, convert a JS value into a C/C++ integer and convert a JS string into a C/C++ string array. You can also convert C/C++ data into a JS value and return the JS value to JS. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Available APIs 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci| API | Description | 16e41f4b71Sopenharmony_ci| ---------------------- | ------------------------------------------------------- | 17e41f4b71Sopenharmony_ci| OH_JSVM_CoerceToBool | Converts a JS value to an object of the Boolean type. | 18e41f4b71Sopenharmony_ci| OH_JSVM_CoerceToNumber | Converts a JS value to an object of the number type. | 19e41f4b71Sopenharmony_ci| OH_JSVM_CoerceToObject | Converts a JS value to an object of the object type. | 20e41f4b71Sopenharmony_ci| OH_JSVM_CoerceToString | Converts a JS value to an object of the string type. | 21e41f4b71Sopenharmony_ci| OH_JSVM_GetBoolean | Obtains a JS singleton object that is used to represent the given Boolean value. | 22e41f4b71Sopenharmony_ci| OH_JSVM_GetValueBool | Obtains the C Boolean primitive equivalent of the given JS Boolean. | 23e41f4b71Sopenharmony_ci| OH_JSVM_GetGlobal | Obtains the **global** object of the current environment. | 24e41f4b71Sopenharmony_ci| OH_JSVM_GetNull | Obtains the JS **null** object. | 25e41f4b71Sopenharmony_ci| OH_JSVM_GetUndefined | Obtains the JS **undefined** object. | 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci## Example 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_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 involved in primitive-related APIs. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci### OH_JSVM_CoerceToBool 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciUse **OH_JSVM_CoerceToBool** to forcibly convert a JS value to a JS Boolean value. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciCPP code: 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci```cpp 38e41f4b71Sopenharmony_ci// hello.cpp 39e41f4b71Sopenharmony_ci#include "napi/native_api.h" 40e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 41e41f4b71Sopenharmony_ci#include <hilog/log.h> 42e41f4b71Sopenharmony_ci// Register the CoerceToBool callback. 43e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 44e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CoerceToBool}, 45e41f4b71Sopenharmony_ci}; 46e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 47e41f4b71Sopenharmony_ci// Set a property descriptor named coerceToBool and associate it with a callback. This allows the CoerceToBool callback to be called from JS. 48e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 49e41f4b71Sopenharmony_ci {"coerceToBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 50e41f4b71Sopenharmony_ci}; 51e41f4b71Sopenharmony_ci// Define OH_JSVM_CoerceToBool. 52e41f4b71Sopenharmony_cistatic JSVM_Value CoerceToBool(JSVM_Env env, JSVM_CallbackInfo info) 53e41f4b71Sopenharmony_ci{ 54e41f4b71Sopenharmony_ci size_t argc = 1; 55e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 56e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 57e41f4b71Sopenharmony_ci JSVM_Value boolean = nullptr; 58e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CoerceToBool(env, args[0], &boolean); 59e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 60e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToBool failed"); 61e41f4b71Sopenharmony_ci } else { 62e41f4b71Sopenharmony_ci bool result = false; 63e41f4b71Sopenharmony_ci OH_JSVM_GetValueBool(env, boolean, &result); 64e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToBool success:%{public}d", result); 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci return boolean; 67e41f4b71Sopenharmony_ci} 68e41f4b71Sopenharmony_ci``` 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciArkTS code: 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci```ts 73e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 74e41f4b71Sopenharmony_ci// Import the native APIs. 75e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 76e41f4b71Sopenharmony_cilet script: string = `coerceToBool("123")`; 77e41f4b71Sopenharmony_citry { 78e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 79e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'CoerceToBool: %{public}s', result); 80e41f4b71Sopenharmony_ci} catch (error) { 81e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'CoerceToBool: %{public}s', error.message); 82e41f4b71Sopenharmony_ci} 83e41f4b71Sopenharmony_ci``` 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci### OH_JSVM_CoerceToNumber 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ciUse **OH_JSVM_CoerceToNumber** to forcibly convert a JS value to a JS number. 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ciCPP code: 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci```cpp 92e41f4b71Sopenharmony_ci// hello.cpp 93e41f4b71Sopenharmony_ci#include "napi/native_api.h" 94e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 95e41f4b71Sopenharmony_ci#include <hilog/log.h> 96e41f4b71Sopenharmony_ci// Register the CoerceToNumber callback. 97e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 98e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CoerceToNumber}, 99e41f4b71Sopenharmony_ci}; 100e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 101e41f4b71Sopenharmony_ci// Set a property descriptor named coerceToNumber and associate it with a callback. This allows the CoerceToNumber callback to be called from JS. 102e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 103e41f4b71Sopenharmony_ci {"coerceToNumber", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 104e41f4b71Sopenharmony_ci}; 105e41f4b71Sopenharmony_ci// Define OH_JSVM_CoerceToNumber. 106e41f4b71Sopenharmony_cistatic JSVM_Value CoerceToNumber(JSVM_Env env, JSVM_CallbackInfo info) 107e41f4b71Sopenharmony_ci{ 108e41f4b71Sopenharmony_ci size_t argc = 1; 109e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 110e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 111e41f4b71Sopenharmony_ci JSVM_Value number = nullptr; 112e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CoerceToNumber(env, args[0], &number); 113e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 114e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToNumber failed"); 115e41f4b71Sopenharmony_ci } else { 116e41f4b71Sopenharmony_ci int32_t result = 0; 117e41f4b71Sopenharmony_ci OH_JSVM_GetValueInt32(env, number, &result); 118e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToNumber success:%{public}d", result); 119e41f4b71Sopenharmony_ci } 120e41f4b71Sopenharmony_ci return number; 121e41f4b71Sopenharmony_ci} 122e41f4b71Sopenharmony_ci``` 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciArkTS code: 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci```ts 127e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 128e41f4b71Sopenharmony_ci// Import the native APIs. 129e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 130e41f4b71Sopenharmony_cilet script: string = `coerceToNumber(true)`; 131e41f4b71Sopenharmony_citry { 132e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 133e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'CoerceToNumber: %{public}s', result); 134e41f4b71Sopenharmony_ci} catch (error) { 135e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'CoerceToNumber: %{public}s', error.message); 136e41f4b71Sopenharmony_ci} 137e41f4b71Sopenharmony_ci``` 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci### OH_JSVM_CoerceToObject 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ciUse **OH_JSVM_CoerceToObject** to forcibly convert a JS value to a JS object. 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ciCPP code: 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci```cpp 146e41f4b71Sopenharmony_ci// hello.cpp 147e41f4b71Sopenharmony_ci#include "napi/native_api.h" 148e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 149e41f4b71Sopenharmony_ci#include <hilog/log.h> 150e41f4b71Sopenharmony_ci// Register the CoerceToObjec callback. 151e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 152e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CoerceToObject}, 153e41f4b71Sopenharmony_ci}; 154e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 155e41f4b71Sopenharmony_ci// Set a property descriptor named coerceToObject and associate it with a callback. This allows the CoerceToObject callback to be called from JS. 156e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 157e41f4b71Sopenharmony_ci {"coerceToObject", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 158e41f4b71Sopenharmony_ci}; 159e41f4b71Sopenharmony_ci// Define OH_JSVM_CoerceToObject. 160e41f4b71Sopenharmony_cistatic JSVM_Value CoerceToObject(JSVM_Env env, JSVM_CallbackInfo info) 161e41f4b71Sopenharmony_ci{ 162e41f4b71Sopenharmony_ci size_t argc = 1; 163e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 164e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 165e41f4b71Sopenharmony_ci JSVM_Value obj; 166e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CoerceToObject(env, args[0], &obj); 167e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 168e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToObject failed"); 169e41f4b71Sopenharmony_ci return nullptr; 170e41f4b71Sopenharmony_ci } else { 171e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToObject success"); 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci return obj; 174e41f4b71Sopenharmony_ci} 175e41f4b71Sopenharmony_ci``` 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciArkTS code: 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci```ts 180e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 181e41f4b71Sopenharmony_ci// Import the native APIs. 182e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 183e41f4b71Sopenharmony_cilet script: string = `coerceToObject(123)`; 184e41f4b71Sopenharmony_citry { 185e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 186e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'CoerceToObject001: %{public}s', result); 187e41f4b71Sopenharmony_ci} catch (error) { 188e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'CoerceToObject001: %{public}s', error.message); 189e41f4b71Sopenharmony_ci} 190e41f4b71Sopenharmony_ci``` 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci### OH_JSVM_CoerceToString 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciUse **OH_JSVM_CoerceToString** to forcibly convert a JS value to a JS string. 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciCPP code: 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci```cpp 199e41f4b71Sopenharmony_ci// hello.cpp 200e41f4b71Sopenharmony_ci#include "napi/native_api.h" 201e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 202e41f4b71Sopenharmony_ci#include <hilog/log.h> 203e41f4b71Sopenharmony_ci// Register the CoerceToString callback. 204e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 205e41f4b71Sopenharmony_ci {.data = nullptr, .callback = CoerceToString}, 206e41f4b71Sopenharmony_ci}; 207e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 208e41f4b71Sopenharmony_ci// Set a property descriptor named coerceToString and associate it with a callback. This allows the CoerceToString callback to be called from JS. 209e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 210e41f4b71Sopenharmony_ci {"coerceToString", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 211e41f4b71Sopenharmony_ci}; 212e41f4b71Sopenharmony_ci// Define OH_JSVM_CoerceToString. 213e41f4b71Sopenharmony_cistatic JSVM_Value CoerceToString(JSVM_Env env, JSVM_CallbackInfo info) 214e41f4b71Sopenharmony_ci{ 215e41f4b71Sopenharmony_ci size_t argc = 1; 216e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 217e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 218e41f4b71Sopenharmony_ci JSVM_Value str; 219e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_CoerceToString(env, args[0], &str); 220e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 221e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_CoerceToString fail"); 222e41f4b71Sopenharmony_ci return nullptr; 223e41f4b71Sopenharmony_ci } else { 224e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToString success"); 225e41f4b71Sopenharmony_ci } 226e41f4b71Sopenharmony_ci return str; 227e41f4b71Sopenharmony_ci} 228e41f4b71Sopenharmony_ci``` 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ciArkTS code: 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci```ts 233e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 234e41f4b71Sopenharmony_ci// Import the native APIs. 235e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 236e41f4b71Sopenharmony_cilet script: string = `coerceToString(22222)`; 237e41f4b71Sopenharmony_citry { 238e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 239e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'CoerceToString: %{public}s', result); 240e41f4b71Sopenharmony_ci} catch (error) { 241e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'CoerceToString: %{public}s', error.message); 242e41f4b71Sopenharmony_ci} 243e41f4b71Sopenharmony_ci``` 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci### OH_JSVM_GetBoolean 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ciUse **OH_JSVM_GetBoolean** to obtain a JS singleton object that is used to represent the given Boolean value. 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciCPP code: 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci```cpp 252e41f4b71Sopenharmony_ci// hello.cpp 253e41f4b71Sopenharmony_ci#include "napi/native_api.h" 254e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 255e41f4b71Sopenharmony_ci#include <hilog/log.h> 256e41f4b71Sopenharmony_ci// Register the GetBoolean callback. 257e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 258e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetBoolean}, 259e41f4b71Sopenharmony_ci}; 260e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 261e41f4b71Sopenharmony_ci// Set a property descriptor named getBoolean and associate it with a callback. This allows the GetBoolean callback to be called from JS. 262e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 263e41f4b71Sopenharmony_ci {"getBoolean", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 264e41f4b71Sopenharmony_ci}; 265e41f4b71Sopenharmony_ci// Define OH_JSVM_GetBoolean. 266e41f4b71Sopenharmony_cistatic JSVM_Value GetBoolean(JSVM_Env env, JSVM_CallbackInfo info) 267e41f4b71Sopenharmony_ci{ 268e41f4b71Sopenharmony_ci // Pass in two parameters and parse them. 269e41f4b71Sopenharmony_ci size_t argc = 2; 270e41f4b71Sopenharmony_ci JSVM_Value argv[2] = {nullptr}; 271e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); 272e41f4b71Sopenharmony_ci int32_t paramData; 273e41f4b71Sopenharmony_ci OH_JSVM_GetValueInt32(env, argv[0], ¶mData); 274e41f4b71Sopenharmony_ci int32_t paramValue; 275e41f4b71Sopenharmony_ci OH_JSVM_GetValueInt32(env, argv[1], ¶mValue); 276e41f4b71Sopenharmony_ci JSVM_Value returnValue = nullptr; 277e41f4b71Sopenharmony_ci bool type = false; 278e41f4b71Sopenharmony_ci if (paramData == paramValue) 279e41f4b71Sopenharmony_ci { 280e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM resultType equal"); 281e41f4b71Sopenharmony_ci type = true; 282e41f4b71Sopenharmony_ci } 283e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetBoolean(env, type, &returnValue); 284e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 285e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CoerceToNumber fail"); 286e41f4b71Sopenharmony_ci } else { 287e41f4b71Sopenharmony_ci bool result = false; 288e41f4b71Sopenharmony_ci OH_JSVM_GetValueBool(env, returnValue, &result); 289e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CoerceToNumber success:%{public}d", result); 290e41f4b71Sopenharmony_ci } 291e41f4b71Sopenharmony_ci // Return the result. 292e41f4b71Sopenharmony_ci return returnValue; 293e41f4b71Sopenharmony_ci} 294e41f4b71Sopenharmony_ci``` 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ciArkTS code: 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci```ts 299e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 300e41f4b71Sopenharmony_ci// Import the native APIs. 301e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 302e41f4b71Sopenharmony_citry { 303e41f4b71Sopenharmony_ci let data = 1; 304e41f4b71Sopenharmony_ci let compareData = 2; 305e41f4b71Sopenharmony_ci let script: string = `getBoolean(${data}, ${compareData})`; 306e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 307e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetBoolean: %{public}s', result); 308e41f4b71Sopenharmony_ci} catch (error) { 309e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetBoolean: %{public}s', error.message); 310e41f4b71Sopenharmony_ci} 311e41f4b71Sopenharmony_citry { 312e41f4b71Sopenharmony_ci let data = 1; 313e41f4b71Sopenharmony_ci let compareData = 1; 314e41f4b71Sopenharmony_ci let script: string = `getBoolean(${data}, ${compareData})`; 315e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 316e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetBoolean: %{public}s', result); 317e41f4b71Sopenharmony_ci} catch (error) { 318e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetBoolean: %{public}s', error.message); 319e41f4b71Sopenharmony_ci} 320e41f4b71Sopenharmony_ci``` 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci### OH_JSVM_GetValueBool 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ciUse **OH_JSVM_GetValueBool** to obtain the C Boolean equivalent of the given JS Boolean. 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ciCPP code: 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci```cpp 329e41f4b71Sopenharmony_ci// hello.cpp 330e41f4b71Sopenharmony_ci#include "napi/native_api.h" 331e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 332e41f4b71Sopenharmony_ci#include <hilog/log.h> 333e41f4b71Sopenharmony_ci// Register the GetValueBool callback. 334e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 335e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetValueBool}, 336e41f4b71Sopenharmony_ci}; 337e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 338e41f4b71Sopenharmony_ci// Set a property descriptor named getValueBool and associate it with a callback. This allows the GetValueBool callback to be called from JS. 339e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 340e41f4b71Sopenharmony_ci {"getValueBool", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 341e41f4b71Sopenharmony_ci}; 342e41f4b71Sopenharmony_ci// Define OH_JSVM_GetValueBool. 343e41f4b71Sopenharmony_cistatic JSVM_Value GetValueBool(JSVM_Env env, JSVM_CallbackInfo info) 344e41f4b71Sopenharmony_ci{ 345e41f4b71Sopenharmony_ci size_t argc = 1; 346e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 347e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 348e41f4b71Sopenharmony_ci bool result; 349e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetValueBool(env, args[0], &result); 350e41f4b71Sopenharmony_ci if (status == JSVM_BOOLEAN_EXPECTED || status != JSVM_OK) { 351e41f4b71Sopenharmony_ci // If OH_JSVM_GetValueBool is successful, JSVM_OK is returned. If a non-Boolean value is passed in, JSVM_BOOLEAN_EXPECTED is returned. 352e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetValueBool fail:%{public}d", status); 353e41f4b71Sopenharmony_ci return nullptr; 354e41f4b71Sopenharmony_ci } else { 355e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetValueBool success:%{public}d", result); 356e41f4b71Sopenharmony_ci } 357e41f4b71Sopenharmony_ci JSVM_Value boolJv; 358e41f4b71Sopenharmony_ci OH_JSVM_GetBoolean(env, result, &boolJv); 359e41f4b71Sopenharmony_ci return boolJv; 360e41f4b71Sopenharmony_ci} 361e41f4b71Sopenharmony_ci``` 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ciArkTS code: 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci```ts 366e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 367e41f4b71Sopenharmony_ci// Import the native APIs. 368e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 369e41f4b71Sopenharmony_ci// Pass in a Boolean value and a non-Boolean value. After the Boolean value is passed in, the Boolean value is returned. After the non-Boolean value is passed in, undefined is returned. 370e41f4b71Sopenharmony_citry { 371e41f4b71Sopenharmony_ci let data = `"abc"`; 372e41f4b71Sopenharmony_ci let script: string = `getValueBool(${data})`; 373e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 374e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetValueBool: %{public}s', result); 375e41f4b71Sopenharmony_ci} catch (error) { 376e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetValueBool: %{public}s', error.message); 377e41f4b71Sopenharmony_ci} 378e41f4b71Sopenharmony_citry { 379e41f4b71Sopenharmony_ci let data = true; 380e41f4b71Sopenharmony_ci let script: string = `getValueBool(${data})`; 381e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 382e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetValueBool: %{public}s', result); 383e41f4b71Sopenharmony_ci} catch (error) { 384e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetValueBool: %{public}s', error.message); 385e41f4b71Sopenharmony_ci} 386e41f4b71Sopenharmony_citry { 387e41f4b71Sopenharmony_ci let data = false; 388e41f4b71Sopenharmony_ci let script: string = `getValueBool(${data})`; 389e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 390e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetValueBool: %{public}s', result); 391e41f4b71Sopenharmony_ci} catch (error) { 392e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetValueBool: %{public}s', error.message); 393e41f4b71Sopenharmony_ci} 394e41f4b71Sopenharmony_ci``` 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci### OH_JSVM_GetGlobal 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ciUse **OH_JSVM_GetGlobal** to obtain a JS global object. You can use this API to obtain the **JSVM_Value** that represents a JS global object, so that the JSVM module can interact with the global variables and functions defined in the JS context. 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ciCPP code: 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci```cpp 403e41f4b71Sopenharmony_ci// hello.cpp 404e41f4b71Sopenharmony_ci#include "napi/native_api.h" 405e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 406e41f4b71Sopenharmony_ci#include <hilog/log.h> 407e41f4b71Sopenharmony_ci// Register the GetGlobal callback. 408e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 409e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetGlobal}, 410e41f4b71Sopenharmony_ci}; 411e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 412e41f4b71Sopenharmony_ci// Set a property descriptor named getGlobal and associate it with a callback. This allows the GetGlobal callback to be called from JS. 413e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 414e41f4b71Sopenharmony_ci {"getGlobal", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 415e41f4b71Sopenharmony_ci}; 416e41f4b71Sopenharmony_ci// Define OH_JSVM_GetGlobal. 417e41f4b71Sopenharmony_cistatic JSVM_Value GetGlobal(JSVM_Env env, JSVM_CallbackInfo info) 418e41f4b71Sopenharmony_ci{ 419e41f4b71Sopenharmony_ci // Obtain the global object. 420e41f4b71Sopenharmony_ci JSVM_Value value = nullptr; 421e41f4b71Sopenharmony_ci JSVM_Value global; 422e41f4b71Sopenharmony_ci OH_JSVM_CreateInt32(env, 1, &value); 423e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetGlobal(env, &global); 424e41f4b71Sopenharmony_ci OH_JSVM_SetNamedProperty(env, global, "Row", value); 425e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 426e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetGlobal fail"); 427e41f4b71Sopenharmony_ci } else { 428e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetGlobal success"); 429e41f4b71Sopenharmony_ci } 430e41f4b71Sopenharmony_ci return global; 431e41f4b71Sopenharmony_ci} 432e41f4b71Sopenharmony_ci``` 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ciArkTS code: 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci```ts 437e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 438e41f4b71Sopenharmony_ci// Import the native APIs. 439e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 440e41f4b71Sopenharmony_cilet script: string = `getGlobal()` 441e41f4b71Sopenharmony_citry { 442e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 443e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetGlobal: %{public}s', result); 444e41f4b71Sopenharmony_ci} catch (error) { 445e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetGlobal: %{public}s', error.message); 446e41f4b71Sopenharmony_ci} 447e41f4b71Sopenharmony_ci``` 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci### OH_JSVM_GetNull 450e41f4b71Sopenharmony_ci 451e41f4b71Sopenharmony_ciUse **OH_JSVM_GetNull** to obtain a JS **null** object. 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ciCPP code: 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci```cpp 456e41f4b71Sopenharmony_ci// hello.cpp 457e41f4b71Sopenharmony_ci#include "napi/native_api.h" 458e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 459e41f4b71Sopenharmony_ci#include <hilog/log.h> 460e41f4b71Sopenharmony_ci// Register the GetNull callback. 461e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 462e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetNull}, 463e41f4b71Sopenharmony_ci}; 464e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 465e41f4b71Sopenharmony_ci// Set a property descriptor named getNull and associate it with a callback. This allows the GetNull callback to be called from JS. 466e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 467e41f4b71Sopenharmony_ci {"getNull", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 468e41f4b71Sopenharmony_ci}; 469e41f4b71Sopenharmony_ci// Define OH_JSVM_GetNull. 470e41f4b71Sopenharmony_cistatic JSVM_Value GetNull(JSVM_Env env, JSVM_CallbackInfo info) { 471e41f4b71Sopenharmony_ci JSVM_Value nullValue; 472e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetNull(env, &nullValue); 473e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 474e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetNull fail"); 475e41f4b71Sopenharmony_ci } else { 476e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetNull success"); 477e41f4b71Sopenharmony_ci } 478e41f4b71Sopenharmony_ci return nullValue; 479e41f4b71Sopenharmony_ci} 480e41f4b71Sopenharmony_ci``` 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ciArkTS code: 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci```ts 485e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 486e41f4b71Sopenharmony_ci// Import the native APIs. 487e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 488e41f4b71Sopenharmony_citry { 489e41f4b71Sopenharmony_ci let script: string = `getNull()`; 490e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 491e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetNull: %{public}s', result); 492e41f4b71Sopenharmony_ci} catch (error) { 493e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetNull: %{public}s', error.message); 494e41f4b71Sopenharmony_ci} 495e41f4b71Sopenharmony_ci``` 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci### OH_JSVM_GetUndefined 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ciUse **OH_JSVM_GetUndefined** to obtain a JS **undefined** object. 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ciCPP code: 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci```cpp 504e41f4b71Sopenharmony_ci// hello.cpp 505e41f4b71Sopenharmony_ci#include "napi/native_api.h" 506e41f4b71Sopenharmony_ci#include "ark_runtime/jsvm.h" 507e41f4b71Sopenharmony_ci#include <hilog/log.h> 508e41f4b71Sopenharmony_ci// Register the GetUndefined callback. 509e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 510e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetUndefined}, 511e41f4b71Sopenharmony_ci}; 512e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 513e41f4b71Sopenharmony_ci// Set a property descriptor named getUndefined and associate it with a callback. This allows the GetUndefined callback to be called from JS. 514e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 515e41f4b71Sopenharmony_ci {"getUndefined", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 516e41f4b71Sopenharmony_ci}; 517e41f4b71Sopenharmony_ci// Define OH_JSVM_GetUndefined. 518e41f4b71Sopenharmony_cistatic JSVM_Value GetUndefined(JSVM_Env env, JSVM_CallbackInfo info) 519e41f4b71Sopenharmony_ci{ 520e41f4b71Sopenharmony_ci // Obtain and parse the parameters passed in. 521e41f4b71Sopenharmony_ci size_t argc = 1; 522e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 523e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 524e41f4b71Sopenharmony_ci // Create the value 'undefined'. 525e41f4b71Sopenharmony_ci JSVM_Value value; 526e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetUndefined(env, &value); 527e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 528e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_GetUndefined failed"); 529e41f4b71Sopenharmony_ci } else { 530e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetUndefined success"); 531e41f4b71Sopenharmony_ci } 532e41f4b71Sopenharmony_ci return value; 533e41f4b71Sopenharmony_ci} 534e41f4b71Sopenharmony_ci``` 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ciAPI declaration: 537e41f4b71Sopenharmony_ci 538e41f4b71Sopenharmony_ciArkTS code: 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci```ts 541e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 542e41f4b71Sopenharmony_ci// Import the native APIs. 543e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 544e41f4b71Sopenharmony_citry { 545e41f4b71Sopenharmony_ci let script: string = `getUndefined()`; 546e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 547e41f4b71Sopenharmony_ci hilog.info(0x0000, 'JSVM', 'GetUndefined: %{public}s', result); 548e41f4b71Sopenharmony_ci} catch (error) { 549e41f4b71Sopenharmony_ci hilog.error(0x0000, 'JSVM', 'GetUndefined: %{public}s', error.message); 550e41f4b71Sopenharmony_ci} 551e41f4b71Sopenharmony_ci``` 552