1# Working with Primitives Using Node-API 2 3## Introduction 4 5Node-API provides APIs for converting data between C/C++ and ArkTS data types and obtaining the ArkTS objects in specified format. 6 7## Basic Concepts 8 9Before using Node-API to operate ArkTS objects, you need to understand the following basic concepts: 10 11- Conversion between ArkTS and C/C primitives: You can use Node-API to convert ArkTS values to C/C++ data types, for example, convert an ArkTS value into a C/C++ integer and convert an ArkTS string into a C/C++ string array. You can also convert C/C++ data into an ArkTS value and return the ArkTS value to ArkTS. 12 13## Available APIs 14 15The following table lists the APIs for converting data between ArkTS and C/C++ types. 16| API| Description| 17| -------- | -------- | 18| napi_coerce_to_bool | Forcibly converts an ArkTS value to an ArkTS Boolean value.| 19| napi_coerce_to_number | Forcibly converts an ArkTS value to an ArkTS number.| 20| napi_coerce_to_object | Forcibly converts an ArkTS value to an ArkTS object.| 21| napi_coerce_to_string | Forcibly converts an ArkTS value to an ArkTS string.| 22| napi_get_boolean | Obtains the ArkTS Boolean value based on the given C Boolean value.| 23| napi_get_value_bool | Obtains the C/C++ equivalent of the given ArkTS Boolean value.| 24| napi_get_global | Obtains an ArkTS global object so that it can be accessed and manipulated in C/C++.| 25| napi_get_null | Obtains the ArkTS **null**.| 26| napi_get_undefined | Obtains the ArkTS **undefined**.| 27 28## Example 29 30If you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code involved in the primitive-related APIs. 31 32### napi_coerce_to_bool 33 34Use **napi_coerce_to_bool** to forcibly convert an ArkTS value to an ArkTS Boolean value. 35 36CPP code: 37 38```cpp 39#include "napi/native_api.h" 40 41static napi_value CoerceToBool(napi_env env, napi_callback_info info) 42{ 43 // Obtain and parse the parameters passed in. 44 size_t argc = 1; 45 napi_value args[1] = {nullptr}; 46 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 47 // Convert the input value to a Boolean value. 48 napi_value result = nullptr; 49 napi_coerce_to_bool(env, args[0], &result); 50 // Return the ArkTS boolean value. 51 return result; 52} 53``` 54 55API declaration: 56 57```ts 58// index.d.ts 59export const coerceToBool: <T>(data: T) => boolean; 60``` 61 62ArkTS code: 63 64```ts 65import hilog from '@ohos.hilog' 66import testNapi from 'libentry.so' 67 68let value = testNapi.coerceToBool<number>(0); 69let str = testNapi.coerceToBool<string>('111111111'); 70let obj = new Object(); 71let res = testNapi.coerceToBool<Object>(obj); 72let result = testNapi.coerceToBool<null>(null); 73// false 74hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_bool:%{public}s', value); 75// true 76hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_bool:%{public}s', str); 77// true 78hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_bool:%{public}s', res); 79// false 80hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_bool:%{public}s', result); 81``` 82 83### napi_coerce_to_number 84 85Use **napi_coerce_to_number** to forcibly convert an ArkTS value to an ArkTS number. 86 87CPP code: 88 89```cpp 90#include "napi/native_api.h" 91 92static napi_value CoerceToNumber(napi_env env, napi_callback_info info) 93{ 94 // Obtain and parse the parameters passed in. 95 size_t argc = 1; 96 napi_value args[1] = {nullptr}; 97 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 98 // Convert the input value to a number. 99 napi_value result = nullptr; 100 napi_coerce_to_number(env, args[0], &result); 101 return result; 102} 103``` 104 105API declaration: 106 107```ts 108// index.d.ts 109export const coerceToNumber: <T>(data: T) => number; 110``` 111 112ArkTS code: 113 114```ts 115import hilog from '@ohos.hilog' 116import testNapi from 'libentry.so' 117 118let value = testNapi.coerceToNumber<string>('2556'); 119let str = testNapi.coerceToNumber<string>('sssss'); 120let bool = testNapi.coerceToNumber<boolean>(true); 121hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_number:%{public}d', value); 122// Not-a-Number (NaN) is returned since 'sssss' is not a valid number. 123hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_number:%{public}d', str); 124// The boolean value true is converted into 1. 125hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_number:%{public}d', bool); 126``` 127 128### napi_coerce_to_object 129 130Use **napi_coerce_to_object** to forcibly convert an ArkTS value to an ArkTS object. 131 132CPP code: 133 134```cpp 135#include "napi/native_api.h" 136 137static napi_value CoerceToObject(napi_env env, napi_callback_info info) 138{ 139 // Obtain and parse the parameters passed in. 140 size_t argc = 1; 141 napi_value args[1] = {nullptr}; 142 napi_value obj = nullptr; 143 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 144 // Convert the input value to an object. 145 napi_coerce_to_object(env, args[0], &obj); 146 return obj; 147} 148``` 149 150API declaration: 151 152```ts 153// index.d.ts 154export const coerceToObject: <T>(data: T) => Object; 155``` 156 157ArkTS code: 158 159```ts 160import hilog from '@ohos.hilog' 161import testNapi from 'libentry.so' 162 163let value = testNapi.coerceToObject<string>('222222'); 164let result = testNapi.coerceToObject<number>(111); 165hilog.info(0x0000, 'testTag', 'Node-API coerceToObject:%{public}s.', typeof result); 166if (typeof value === 'object') { 167 hilog.info(0x0000, 'testTag', 'Node-API The value is an object.'); 168} else { 169 hilog.info(0x0000, 'testTag', 'Node-API The value is not an object.'); 170} 171``` 172 173### napi_coerce_to_string 174 175Use **napi_coerce_to_string** to forcibly convert an ArkTS value to an ArkTS string. 176 177CPP code: 178 179```cpp 180#include "napi/native_api.h" 181 182static napi_value CoerceToString(napi_env env, napi_callback_info info) 183{ 184 // Obtain and parse the parameters passed in. 185 size_t argc = 1; 186 napi_value args[1] = {nullptr}; 187 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 188 // Convert the input value to a string. 189 napi_value str = nullptr; 190 napi_coerce_to_string(env, args[0], &str); 191 return str; 192} 193``` 194 195API declaration: 196 197```ts 198// index.d.ts 199export const coerceToString: <T>(data: T) => string; 200``` 201 202ArkTS code: 203 204```ts 205import hilog from '@ohos.hilog' 206import testNapi from 'libentry.so' 207 208let value = testNapi.coerceToString<number>(212); 209let obj = new Object(); 210let res = testNapi.coerceToString<Object>(obj); 211let bool = testNapi.coerceToString<boolean>(false); 212hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_string:%{public}s', value); 213hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_string:%{public}s', typeof res); 214hilog.info(0x0000, 'testTag', 'Test Node-API napi_coerce_to_string:%{public}s', bool); 215``` 216 217### napi_get_boolean 218 219Use **napi_get_boolean** to obtain the ArkTS Boolean value based on the given C Boolean value. 220 221CPP code: 222 223```cpp 224#include "napi/native_api.h" 225 226static napi_value GetBoolean(napi_env env, napi_callback_info info) 227{ 228 // Pass in two parameters and parse them. 229 size_t argc = 2; 230 napi_value argv[2]; 231 napi_valuetype data, value; 232 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 233 // Check the types of the two parameters. 234 napi_typeof(env, argv[0], &data); 235 napi_typeof(env, argv[1], &value); 236 237 napi_value returnValue = nullptr; 238 // Check whether the types of the two parameters are the same and return the result via a Boolean value. 239 napi_get_boolean(env, data == value, &returnValue); 240 // Return the result. 241 return returnValue; 242} 243``` 244 245API declaration: 246 247```ts 248// index.d.ts 249export const getBoolean: <T>(data: T, value: String) => boolean; 250``` 251 252ArkTS code: 253 254```ts 255import hilog from '@ohos.hilog' 256import testNapi from 'libentry.so' 257 258let value = testNapi.getBoolean<number>(1, '1'); 259let data = testNapi.getBoolean<string>('sss', '1'); 260hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_boolean:%{public}s', value); 261hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_boolean:%{public}s', data); 262``` 263 264### napi_get_value_bool 265 266Use **napi_get_value_bool** to obtain the C Boolean equivalent of an ArkTS Boolean value. 267 268CPP code: 269 270```cpp 271#include "napi/native_api.h" 272 273static napi_value GetValueBool(napi_env env, napi_callback_info info) 274{ 275 size_t argc = 1; 276 napi_value args[1] = {nullptr}; 277 278 napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 279 bool bool_c = false; 280 napi_status status = napi_get_value_bool(env, args[0], &bool_c); 281 if (status == napi_boolean_expected) { 282 // If napi_get_value_bool is successful, napi_ok is returned. If a non-Boolean value is passed in, napi_boolean_expected is returned. 283 return nullptr; 284 } 285 napi_value boolNapi = nullptr; 286 napi_get_boolean(env, bool_c, &boolNapi); 287 return boolNapi; 288} 289``` 290 291API declaration: 292 293```ts 294// index.d.ts 295export const getValueBool: (value: boolean | string) => boolean | void; 296``` 297 298ArkTS code: 299 300```ts 301import hilog from '@ohos.hilog' 302import testNapi from 'libentry.so' 303 304// 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. 305hilog.info(0x0000, 'Node-API', 'get_value_bool_not_bool %{public}s', testNapi.getValueBool ('Hello 123')); 306hilog.info(0x0000, 'Node-API', 'get_value_bool_true %{public}s', testNapi.getValueBool(true)); 307hilog.info(0x0000, 'Node-API', 'get_value_bool_false %{public}s', testNapi.getValueBool(false)); 308``` 309 310### napi_get_global 311 312Use **napi_get_global** to obtain an ArkTS global object. You can use this API to obtain the **napi_value** that represents an ArkTS global object, so that the global object of the ArkTS runtime can be called by C/C++. 313 314CPP code: 315 316```cpp 317#include "napi/native_api.h" 318 319static napi_value GetGlobal(napi_env env, napi_callback_info info) 320{ 321 napi_value global = nullptr; 322 // Obtains a global object. 323 napi_get_global(env, &global); 324 return global; 325} 326``` 327 328API declaration: 329 330```ts 331// index.d.ts 332export const getGlobal: () => Object; 333``` 334 335ArkTS code: 336 337```ts 338import hilog from '@ohos.hilog' 339import testNapi from 'libentry.so' 340 341let globalObj = testNapi.getGlobal(); 342// Check whether the obtained global object has its own properties. 343hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_global:%{public}s', globalObj.hasOwnProperty!("undefined")); 344``` 345 346### napi_get_null 347 348Use **napi_get_null** to obtains **null** in ArkTS. 349 350CPP code: 351 352```cpp 353#include "napi/native_api.h" 354 355static napi_value GetNull(napi_env env, napi_callback_info info) 356{ 357 napi_value nullValue = nullptr; 358 napi_get_null(env, &nullValue); 359 return nullValue; 360} 361``` 362 363API declaration: 364 365```ts 366// index.d.ts 367export const getNull: () => null; 368``` 369 370ArkTS code: 371 372```ts 373import hilog from '@ohos.hilog' 374import testNapi from 'libentry.so' 375 376let value = testNapi.getNull(); 377hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_null:%{public}s', value); 378``` 379 380### napi_get_undefined 381 382Use **napi_get_undefined** to obtain **undefined** in ArkTS. 383 384CPP code: 385 386```cpp 387#include "napi/native_api.h" 388 389static napi_value GetUndefined(napi_env env, napi_callback_info info) 390{ 391 // Obtain and parse the parameters passed in. 392 size_t argc = 1; 393 napi_value args[1] = {nullptr}; 394 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 395 396 napi_value value = nullptr; 397 napi_get_undefined(env, &value); 398 // Check whether the input parameter is equal to the ArkTS 'undefined' in both the type and value. 399 bool isEqual = false; 400 napi_strict_equals(env, args[0], value, &isEqual); 401 // Return a value based on the strict equality check. 402 napi_value result = nullptr; 403 // Return true if the two parameters are equal, return false otherwise. 404 napi_get_boolean(env, isEqual, &result); 405 return result; 406} 407``` 408 409API declaration: 410 411```ts 412// index.d.ts 413export const getUndefined: (value: undefined) => boolean; 414``` 415 416ArkTS code: 417 418```ts 419import hilog from '@ohos.hilog' 420import testNapi from 'libentry.so' 421 422let data: undefined = undefined; 423let value = testNapi.getUndefined(data); 424hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_undefined:%{public}s', value); 425``` 426 427To print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**. 428 429```text 430// CMakeLists.txt 431add_definitions( "-DLOG_DOMAIN=0xd0d0" ) 432add_definitions( "-DLOG_TAG=\"testTag\"" ) 433target_link_libraries(entry PUBLIC libhilog_ndk.z.so) 434``` 435