1e41f4b71Sopenharmony_ci# Setting JS Object Properties Using JSVM-API 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Introduction 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThis topic walks you through on how to obtain and set properties of a JavaScript (JS) object using JSVM-API. Properly using these APIs help to implement more complex functionalities and logic. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Basic Concepts 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciBefore working with JS objects using JSVM-API, you need to understand the following concepts: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Object: a composite data type that allows values of different types in an independent entity in JS. An object is a collection of properties and methods. A property is a value associated with the object, and a method is an operation that the object can perform. 12e41f4b71Sopenharmony_ci- Property: a feature, in the key-value format, of an object in JS. Each property has a name (key or identifier) and a value. The property value can be of any data type, including the basic type, object, and function. 13e41f4b71Sopenharmony_ci- Enumerable property: a property in JS with **enumerable** set to **true**. An enumerable property can be traversed by **for...in**. 14e41f4b71Sopenharmony_ci- Own property: a property defined for an object rather than inherited from the prototype chain. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Available APIs 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci| API | Description | 19e41f4b71Sopenharmony_ci|----------------------------|--------------------------------| 20e41f4b71Sopenharmony_ci| OH_JSVM_GetPropertyNames | Obtains the names of all enumerable properties of a JS object as a JS array. | 21e41f4b71Sopenharmony_ci| OH_JSVM_SetProperty | Sets a property for a JS object. | 22e41f4b71Sopenharmony_ci| OH_JSVM_GetProperty | Obtains a property from a JS object. | 23e41f4b71Sopenharmony_ci| OH_JSVM_HasProperty | Checks whether a JS object has the specified property. | 24e41f4b71Sopenharmony_ci| OH_JSVM_DeleteProperty | Deletes a property from a JS object. | 25e41f4b71Sopenharmony_ci| OH_JSVM_HasOwnProperty | Checks whether an object has the own property specified by **key**.| 26e41f4b71Sopenharmony_ci| OH_JSVM_SetNamedProperty | Sets a property with the given property name for a JS object. This API is equivalent to calling **OH_JSVM_SetNamedProperty** with a **JSVM_Value** created from the string passed in as **utf8Name**.| 27e41f4b71Sopenharmony_ci| OH_JSVM_GetNamedProperty | Obtains a property from a JS object. This API is equivalent to calling **OH_JSVM_GetNamedProperty** with a **JSVM_Value** created from the string passed in as **utf8Name**.| 28e41f4b71Sopenharmony_ci| OH_JSVM_HasNamedProperty | Checks whether a JS object has the specified property. This API is equivalent to calling **OH_JSVM_HasProperty** with a **JSVM_Value** created from the string passed in as **utf8Name**.| 29e41f4b71Sopenharmony_ci| OH_JSVM_DefineProperties | Defines multiple properties for a JS object. | 30e41f4b71Sopenharmony_ci| OH_JSVM_GetAllPropertyNames | Obtains the names of all available properties of a JS object as a JS array. | 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## Example 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_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 JS property APIs. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci### OH_JSVM_GetPropertyNames 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciUse **OH_JSVM_GetPropertyNames** to obtain names of all enumerable properties of a JS object in the form of a string array. If the operation is successful, **JSVM_OK** is returned. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciCPP code: 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci```cpp 43e41f4b71Sopenharmony_ci// Register the GetPropertyNames callback. 44e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 45e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetPropertyNames}, 46e41f4b71Sopenharmony_ci}; 47e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 48e41f4b71Sopenharmony_ci// Set a property descriptor named getPropertyNames and associate it with a callback. This allows the CreateStringLatin1 callback to be called from JS. 49e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 50e41f4b71Sopenharmony_ci {"getPropertyNames", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 51e41f4b71Sopenharmony_ci}; 52e41f4b71Sopenharmony_ci// Define OH_JSVM_GetPropertyNames. 53e41f4b71Sopenharmony_cistatic JSVM_Value GetPropertyNames(JSVM_Env env, JSVM_CallbackInfo info) 54e41f4b71Sopenharmony_ci{ 55e41f4b71Sopenharmony_ci // Pass in obj as a parameter. 56e41f4b71Sopenharmony_ci size_t argc = 1; 57e41f4b71Sopenharmony_ci JSVM_Value args[1] = {nullptr}; 58e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 59e41f4b71Sopenharmony_ci // Obtain the names of all the enumerable properties of the object in the form of a string array and output the string array in result. 60e41f4b71Sopenharmony_ci JSVM_Value result = nullptr; 61e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetPropertyNames(env, args[0], &result); 62e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 63e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "Failed to get propertynames"); 64e41f4b71Sopenharmony_ci return nullptr; 65e41f4b71Sopenharmony_ci } else { 66e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetPropertyNames success"); 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci return result; 69e41f4b71Sopenharmony_ci} 70e41f4b71Sopenharmony_ci``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciArkTS code: 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci```ts 75e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 76e41f4b71Sopenharmony_ci// Import the native APIs. 77e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 78e41f4b71Sopenharmony_cilet obj = '{ data: 0, message: "hello world"}'; 79e41f4b71Sopenharmony_cilet script: string = `getPropertyNames(${obj})`; 80e41f4b71Sopenharmony_citry { 81e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 82e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getPropertyNames: %{public}s', result); 83e41f4b71Sopenharmony_ci} catch (error) { 84e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getPropertyNames error: %{public}s', error.message); 85e41f4b71Sopenharmony_ci} 86e41f4b71Sopenharmony_ci``` 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci### OH_JSVM_SetProperty 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciUse **OH_JSVM_SetProperty** to set a property for an object. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciCPP code: 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci```cpp 95e41f4b71Sopenharmony_ci// Register the SetProperty callback. 96e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 97e41f4b71Sopenharmony_ci {.data = nullptr, .callback = SetProperty}, 98e41f4b71Sopenharmony_ci}; 99e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 100e41f4b71Sopenharmony_ci// Set a property descriptor named setProperty and associate it with a callback. This allows the SetProperty callback to be called from JS. 101e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 102e41f4b71Sopenharmony_ci {"setProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 103e41f4b71Sopenharmony_ci}; 104e41f4b71Sopenharmony_ci// Define OH_JSVM_SetProperty. 105e41f4b71Sopenharmony_cistatic JSVM_Value SetProperty(JSVM_Env env, JSVM_CallbackInfo info) 106e41f4b71Sopenharmony_ci{ 107e41f4b71Sopenharmony_ci // Obtain the parameters passed from JS. The first parameter specifies the object, the second parameter specifies the property name, and the third parameter specifies the property value to set. 108e41f4b71Sopenharmony_ci size_t argc = 3; 109e41f4b71Sopenharmony_ci JSVM_Value args[3] = {nullptr}; 110e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 111e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 112e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetCbInfo fail"); 113e41f4b71Sopenharmony_ci return nullptr; 114e41f4b71Sopenharmony_ci } 115e41f4b71Sopenharmony_ci // Call OH_JSVM_SetProperty to set the property name and value to the object. If the operation fails, an error is thrown. 116e41f4b71Sopenharmony_ci status = OH_JSVM_SetProperty(env, args[0], args[1], args[2]); 117e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 118e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetProperty fail"); 119e41f4b71Sopenharmony_ci return nullptr; 120e41f4b71Sopenharmony_ci } else { 121e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_SetProperty success"); 122e41f4b71Sopenharmony_ci } 123e41f4b71Sopenharmony_ci // Return the object that is successfully set. 124e41f4b71Sopenharmony_ci return args[0]; 125e41f4b71Sopenharmony_ci} 126e41f4b71Sopenharmony_ci``` 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ciArkTS code: 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci```ts 131e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 132e41f4b71Sopenharmony_ci// Import the native APIs. 133e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 134e41f4b71Sopenharmony_cilet script: string = ` 135e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 136e41f4b71Sopenharmony_ci setProperty(obj, "code", "hi") 137e41f4b71Sopenharmony_ci` 138e41f4b71Sopenharmony_citry { 139e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 140e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM setProperty: %{public}s', result); 141e41f4b71Sopenharmony_ci} catch (error) { 142e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM setProperty error: %{public}s', error.message); 143e41f4b71Sopenharmony_ci} 144e41f4b71Sopenharmony_ci``` 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci### OH_JSVM_GetProperty 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ciUse **OH_JSVM_GetProperty** to obtain a property value of a JS object based on the property name. 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ciCPP code: 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci```cpp 153e41f4b71Sopenharmony_ci// Register the GetProperty callback. 154e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 155e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetProperty}, 156e41f4b71Sopenharmony_ci}; 157e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 158e41f4b71Sopenharmony_ci// Set a property descriptor named getProperty and associate it with a callback. This allows the GetProperty callback to be called from JS. 159e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 160e41f4b71Sopenharmony_ci {"getProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 161e41f4b71Sopenharmony_ci}; 162e41f4b71Sopenharmony_ci// Define OH_JSVM_GetProperty. 163e41f4b71Sopenharmony_cistatic JSVM_Value GetProperty(JSVM_Env env, JSVM_CallbackInfo info) 164e41f4b71Sopenharmony_ci{ 165e41f4b71Sopenharmony_ci // Obtain the two parameters passed from JS. 166e41f4b71Sopenharmony_ci size_t argc = 2; 167e41f4b71Sopenharmony_ci JSVM_Value args[2] = {nullptr}; 168e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 169e41f4b71Sopenharmony_ci // The first parameter specifies the target object, and the second specifies the property name. Call OH_JSVM_GetProperty to obtain the value of the property. 170e41f4b71Sopenharmony_ci JSVM_Value result = nullptr; 171e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetProperty(env, args[0], args[1], &result); 172e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 173e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetProperty fail"); 174e41f4b71Sopenharmony_ci return nullptr; 175e41f4b71Sopenharmony_ci } else { 176e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetProperty success"); 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci return result; 179e41f4b71Sopenharmony_ci} 180e41f4b71Sopenharmony_ci``` 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ciArkTS code: 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci```ts 185e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 186e41f4b71Sopenharmony_ci// Import the native APIs. 187e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 188e41f4b71Sopenharmony_cilet script: string = ` 189e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 190e41f4b71Sopenharmony_ci getProperty(obj, "message") 191e41f4b71Sopenharmony_ci` 192e41f4b71Sopenharmony_citry { 193e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 194e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getProperty: %{public}s', result); 195e41f4b71Sopenharmony_ci} catch (error) { 196e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getProperty error: %{public}s', error.message); 197e41f4b71Sopenharmony_ci} 198e41f4b71Sopenharmony_ci``` 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci### OH_JSVM_HasProperty 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ciUse **OH_JSVM_HasProperty** to check whether an object has the specified property. This can prevent the exception or error caused by access to a property that does not exist. 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ciCPP code: 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci```cpp 207e41f4b71Sopenharmony_ci// Register the HasProperty callback. 208e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 209e41f4b71Sopenharmony_ci {.data = nullptr, .callback = HasProperty}, 210e41f4b71Sopenharmony_ci}; 211e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 212e41f4b71Sopenharmony_ci// Set a property descriptor named hasProperty and associate it with a callback. This allows the HasProperty callback to be called from JS. 213e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 214e41f4b71Sopenharmony_ci {"hasProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 215e41f4b71Sopenharmony_ci}; 216e41f4b71Sopenharmony_ci// Define OH_JSVM_HasProperty. 217e41f4b71Sopenharmony_cistatic JSVM_Value HasProperty(JSVM_Env env, JSVM_CallbackInfo info) 218e41f4b71Sopenharmony_ci{ 219e41f4b71Sopenharmony_ci // Pass in two parameters from JS. The first parameter specifies the target object, and the second parameter specifies the property to check. 220e41f4b71Sopenharmony_ci size_t argc = 2; 221e41f4b71Sopenharmony_ci JSVM_Value args[2] = {nullptr}; 222e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 223e41f4b71Sopenharmony_ci // Pass the parameters to OH_JSVM_HasProperty. If the API is successfully called, convert the result to JSVM_Value and return JSVM_Value. Otherwise, throw an error. 224e41f4b71Sopenharmony_ci bool result; 225e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_HasProperty(env, args[0], args[1], &result); 226e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 227e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasProperty fail"); 228e41f4b71Sopenharmony_ci return nullptr; 229e41f4b71Sopenharmony_ci } else { 230e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_HasProperty success:%{public}d", result); 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci // If the property exists in the object, output true, convert the result to JSVM_Value, and return JSVM_Value. 233e41f4b71Sopenharmony_ci JSVM_Value returnReslut = nullptr; 234e41f4b71Sopenharmony_ci OH_JSVM_GetBoolean(env, result, &returnReslut); 235e41f4b71Sopenharmony_ci return returnReslut; 236e41f4b71Sopenharmony_ci} 237e41f4b71Sopenharmony_ci``` 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ciArkTS code: 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci```ts 242e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 243e41f4b71Sopenharmony_ci// Import the native APIs. 244e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 245e41f4b71Sopenharmony_cilet script: string = ` 246e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 247e41f4b71Sopenharmony_ci` 248e41f4b71Sopenharmony_cilet scriptTrue: string = script + `\n` + ` 249e41f4b71Sopenharmony_ci hasProperty(obj, "data") 250e41f4b71Sopenharmony_ci` 251e41f4b71Sopenharmony_cilet scriptFalse: string = script + `\n` + ` 252e41f4b71Sopenharmony_ci hasProperty(obj, 0) 253e41f4b71Sopenharmony_ci` 254e41f4b71Sopenharmony_citry { 255e41f4b71Sopenharmony_ci let resultTrue = napitest.runJsVm(scriptTrue); 256e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM HasProperty: %{public}s', resultTrue); 257e41f4b71Sopenharmony_ci let resultFalse = napitest.runJsVm(scriptFalse); 258e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM HasProperty: %{public}s', resultFalse); 259e41f4b71Sopenharmony_ci} catch (error) { 260e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM hasProperty error: %{public}s', error.message); 261e41f4b71Sopenharmony_ci} 262e41f4b71Sopenharmony_ci``` 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci### OH_JSVM_DeleteProperty 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ciUse **OH_JSVM_DeleteProperty** to delete the property specified by **key** from an object. 267e41f4b71Sopenharmony_ciIf the object is a non-extensible object or the property is not configurable, the property cannot be deleted. 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ciCPP code: 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci```cpp 272e41f4b71Sopenharmony_ci// Register the DeleteProperty callback. 273e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 274e41f4b71Sopenharmony_ci {.data = nullptr, .callback = DeleteProperty}, 275e41f4b71Sopenharmony_ci}; 276e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 277e41f4b71Sopenharmony_ci// Set a property descriptor named deleteProperty and associate it with a callback. This allows the DeleteProperty callback to be called from JS. 278e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 279e41f4b71Sopenharmony_ci {"deleteProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 280e41f4b71Sopenharmony_ci}; 281e41f4b71Sopenharmony_ci// Define OH_JSVM_DeleteProperty. 282e41f4b71Sopenharmony_cistatic JSVM_Value DeleteProperty(JSVM_Env env, JSVM_CallbackInfo info) 283e41f4b71Sopenharmony_ci{ 284e41f4b71Sopenharmony_ci // Obtain the two parameters passed from JS. 285e41f4b71Sopenharmony_ci size_t argc = 2; 286e41f4b71Sopenharmony_ci JSVM_Value args[2] = {nullptr}; 287e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 288e41f4b71Sopenharmony_ci JSVM_ValueType valueType; 289e41f4b71Sopenharmony_ci OH_JSVM_Typeof(env, args[0], &valueType); 290e41f4b71Sopenharmony_ci if (valueType != JSVM_OBJECT) { 291e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "Expects an object as argument."); 292e41f4b71Sopenharmony_ci return nullptr; 293e41f4b71Sopenharmony_ci } 294e41f4b71Sopenharmony_ci // Delete the specified property from the object and return a bool value indicating whether the deletion is successful. 295e41f4b71Sopenharmony_ci bool result = false; 296e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_DeleteProperty(env, args[0], args[1], &result); 297e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 298e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_DeleteProperty failed"); 299e41f4b71Sopenharmony_ci return nullptr; 300e41f4b71Sopenharmony_ci } else { 301e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_DeleteProperty success:%{public}d", result); 302e41f4b71Sopenharmony_ci } 303e41f4b71Sopenharmony_ci // Convert the bool value to JSVM_value and return JSVM_value. 304e41f4b71Sopenharmony_ci JSVM_Value ret; 305e41f4b71Sopenharmony_ci OH_JSVM_GetBoolean(env, result, &ret); 306e41f4b71Sopenharmony_ci return ret; 307e41f4b71Sopenharmony_ci} 308e41f4b71Sopenharmony_ci``` 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ciArkTS code: 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci```ts 313e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 314e41f4b71Sopenharmony_ci// Import the native APIs. 315e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 316e41f4b71Sopenharmony_cilet script: string = ` 317e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 318e41f4b71Sopenharmony_ci deleteProperty(obj, "message") 319e41f4b71Sopenharmony_ci` 320e41f4b71Sopenharmony_citry { 321e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 322e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM deleteProperty: %{public}s', result); 323e41f4b71Sopenharmony_ci} catch (error) { 324e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM deleteProperty error: %{public}s', error.message); 325e41f4b71Sopenharmony_ci} 326e41f4b71Sopenharmony_ci``` 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci### OH_JSVM_HasOwnProperty 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ciUse **OH_JSVM_HasOwnProperty** to check whether a JS object has its own property. 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ciCPP code: 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci```cpp 335e41f4b71Sopenharmony_ci// Register the HasOwnProperty callback. 336e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 337e41f4b71Sopenharmony_ci {.data = nullptr, .callback = HasOwnProperty}, 338e41f4b71Sopenharmony_ci}; 339e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 340e41f4b71Sopenharmony_ci// Set a property descriptor named hasOwnProperty and associate it with a callback. This allows the HasOwnProperty callback to be called from JS. 341e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 342e41f4b71Sopenharmony_ci {"hasOwnProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 343e41f4b71Sopenharmony_ci}; 344e41f4b71Sopenharmony_ci// Define OH_JSVM_HasOwnProperty. 345e41f4b71Sopenharmony_cistatic JSVM_Value HasOwnProperty(JSVM_Env env, JSVM_CallbackInfo info) 346e41f4b71Sopenharmony_ci{ 347e41f4b71Sopenharmony_ci // Obtain the two parameters passed from JS. 348e41f4b71Sopenharmony_ci size_t argc = 2; 349e41f4b71Sopenharmony_ci JSVM_Value args[2] = {nullptr}; 350e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 351e41f4b71Sopenharmony_ci // Check whether the first parameter is an object. 352e41f4b71Sopenharmony_ci JSVM_ValueType valueType1; 353e41f4b71Sopenharmony_ci OH_JSVM_Typeof(env, args[0], &valueType1); 354e41f4b71Sopenharmony_ci if (valueType1 != JSVM_OBJECT) { 355e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "First argument must be an object."); 356e41f4b71Sopenharmony_ci return nullptr; 357e41f4b71Sopenharmony_ci } 358e41f4b71Sopenharmony_ci // Check whether the second parameter is a string. 359e41f4b71Sopenharmony_ci JSVM_ValueType valuetype2; 360e41f4b71Sopenharmony_ci OH_JSVM_Typeof(env, args[1], &valuetype2); 361e41f4b71Sopenharmony_ci if (valuetype2 != JSVM_STRING ) { 362e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "Second argument must be a string."); 363e41f4b71Sopenharmony_ci return nullptr; 364e41f4b71Sopenharmony_ci } 365e41f4b71Sopenharmony_ci // Check whether the object has the specified property and return the result in hasProperty. 366e41f4b71Sopenharmony_ci bool hasProperty; 367e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_HasOwnProperty(env, args[0], args[1], &hasProperty); 368e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 369e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasOwnProperty failed"); 370e41f4b71Sopenharmony_ci return nullptr; 371e41f4b71Sopenharmony_ci } else { 372e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_HasOwnProperty success:%{public}d", hasProperty); 373e41f4b71Sopenharmony_ci } 374e41f4b71Sopenharmony_ci // Convert the bool value to JSVM_Value and return JSVM_value. 375e41f4b71Sopenharmony_ci JSVM_Value result; 376e41f4b71Sopenharmony_ci OH_JSVM_GetBoolean(env, hasProperty, &result); 377e41f4b71Sopenharmony_ci return result; 378e41f4b71Sopenharmony_ci} 379e41f4b71Sopenharmony_ci``` 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ciArkTS code: 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci```ts 384e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 385e41f4b71Sopenharmony_ci// Import the native APIs. 386e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 387e41f4b71Sopenharmony_cilet script: string = ` 388e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 389e41f4b71Sopenharmony_ci` 390e41f4b71Sopenharmony_cilet scriptTrue: string = script + `\n` + ` 391e41f4b71Sopenharmony_ci hasOwnProperty(obj, "message") 392e41f4b71Sopenharmony_ci` 393e41f4b71Sopenharmony_cilet scriptFalse: string = script + `\n` + ` 394e41f4b71Sopenharmony_ci hasOwnProperty(obj, "__defineGetter__") 395e41f4b71Sopenharmony_ci` 396e41f4b71Sopenharmony_citry { 397e41f4b71Sopenharmony_ci let resultTrue = napitest.runJsVm(scriptTrue); 398e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM hasOwnProperty: %{public}s', resultTrue); 399e41f4b71Sopenharmony_ci let resultFalse = napitest.runJsVm(scriptFalse); 400e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM hasOwnProperty: %{public}s', resultFalse); 401e41f4b71Sopenharmony_ci} catch (error) { 402e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM hasOwnProperty error: %{public}s', error.message); 403e41f4b71Sopenharmony_ci} 404e41f4b71Sopenharmony_ci``` 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_ci### OH_JSVM_SetNamedProperty 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ciUse **OH_JSVM_SetNamedProperty** to set a property for a JS object. 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ciCPP code: 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci```cpp 413e41f4b71Sopenharmony_ci// Register the SetNamedProperty callback. 414e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 415e41f4b71Sopenharmony_ci {.data = nullptr, .callback = SetNamedProperty}, 416e41f4b71Sopenharmony_ci}; 417e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 418e41f4b71Sopenharmony_ci// Set a property descriptor named SetNamedProperty and associate it with a callback. This allows the SetNamedProperty callback to be called from JS. 419e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 420e41f4b71Sopenharmony_ci {"setNamedProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 421e41f4b71Sopenharmony_ci}; 422e41f4b71Sopenharmony_ci// Define OH_JSVM_SetNamedProperty. 423e41f4b71Sopenharmony_cistatic JSVM_Value SetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info) 424e41f4b71Sopenharmony_ci{ 425e41f4b71Sopenharmony_ci // Obtain the parameter passed from JS. 426e41f4b71Sopenharmony_ci size_t argc = 1; 427e41f4b71Sopenharmony_ci JSVM_Value str; 428e41f4b71Sopenharmony_ci char strKey[32] = ""; 429e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, &str, nullptr, nullptr); 430e41f4b71Sopenharmony_ci // Obtain the string passed in and store it in strKey. 431e41f4b71Sopenharmony_ci size_t keyLength; 432e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, str, strKey, 32, &keyLength); 433e41f4b71Sopenharmony_ci // Create an object. 434e41f4b71Sopenharmony_ci JSVM_Value newObj; 435e41f4b71Sopenharmony_ci OH_JSVM_CreateObject(env, &newObj); 436e41f4b71Sopenharmony_ci // Set the property value to 1234. 437e41f4b71Sopenharmony_ci int32_t value = 1234; 438e41f4b71Sopenharmony_ci JSVM_Value numValue; 439e41f4b71Sopenharmony_ci OH_JSVM_CreateInt32(env, value, &numValue); 440e41f4b71Sopenharmony_ci // Associate the integer value with the property name. 441e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_SetNamedProperty(env, newObj, strKey, numValue); 442e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 443e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_SetNamedProperty failed"); 444e41f4b71Sopenharmony_ci return nullptr; 445e41f4b71Sopenharmony_ci } else { 446e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_SetNamedProperty success"); 447e41f4b71Sopenharmony_ci } 448e41f4b71Sopenharmony_ci // Return the newly created object with the property set. 449e41f4b71Sopenharmony_ci return newObj; 450e41f4b71Sopenharmony_ci} 451e41f4b71Sopenharmony_ci``` 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ciArkTS code: 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci```ts 456e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 457e41f4b71Sopenharmony_ci// Import the native APIs. 458e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 459e41f4b71Sopenharmony_cilet script: string = ` 460e41f4b71Sopenharmony_ci setNamedProperty("message") 461e41f4b71Sopenharmony_ci` 462e41f4b71Sopenharmony_citry { 463e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 464e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM setNamedProperty: %{public}s', result); 465e41f4b71Sopenharmony_ci} catch (error) { 466e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM setNamedProperty error: %{public}s', error.message); 467e41f4b71Sopenharmony_ci} 468e41f4b71Sopenharmony_ci``` 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci### OH_JSVM_GetNamedProperty 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ciUse **OH_JSVM_GetNamedProperty** to obtain the value of the specified property from a JS object. 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ciCPP code: 475e41f4b71Sopenharmony_ci 476e41f4b71Sopenharmony_ci```cpp 477e41f4b71Sopenharmony_ci// Register the GetNamedProperty callback. 478e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 479e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetNamedProperty}, 480e41f4b71Sopenharmony_ci}; 481e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 482e41f4b71Sopenharmony_ci// Set a property descriptor named getNamedProperty and associate it with a callback. This allows the GetNamedProperty callback to be called from JS. 483e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 484e41f4b71Sopenharmony_ci {"getNamedProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 485e41f4b71Sopenharmony_ci}; 486e41f4b71Sopenharmony_ci// Define OH_JSVM_GetNamedProperty. 487e41f4b71Sopenharmony_cistatic JSVM_Value GetNamedProperty(JSVM_Env env, JSVM_CallbackInfo info) 488e41f4b71Sopenharmony_ci{ 489e41f4b71Sopenharmony_ci // Obtain the two parameters passed from JS. 490e41f4b71Sopenharmony_ci size_t argc = 2; 491e41f4b71Sopenharmony_ci JSVM_Value args[2] = {nullptr}; 492e41f4b71Sopenharmony_ci char strKey[32] = ""; 493e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 494e41f4b71Sopenharmony_ci // Obtain the name of the property to obtain. 495e41f4b71Sopenharmony_ci size_t keyLength; 496e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, args[1], strKey, 32, &keyLength); 497e41f4b71Sopenharmony_ci // Obtain the value of the property and store it in result. 498e41f4b71Sopenharmony_ci JSVM_Value result; 499e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetNamedProperty(env, args[0], strKey, &result); 500e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 501e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_GetNamedProperty failed"); 502e41f4b71Sopenharmony_ci return nullptr; 503e41f4b71Sopenharmony_ci } else { 504e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetNamedProperty success"); 505e41f4b71Sopenharmony_ci } 506e41f4b71Sopenharmony_ci return result; 507e41f4b71Sopenharmony_ci} 508e41f4b71Sopenharmony_ci``` 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ciArkTS code: 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci```ts 513e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 514e41f4b71Sopenharmony_ci// Import the native APIs. 515e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 516e41f4b71Sopenharmony_cilet script: string = ` 517e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 518e41f4b71Sopenharmony_ci getNamedProperty(obj, "message") 519e41f4b71Sopenharmony_ci` 520e41f4b71Sopenharmony_citry { 521e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 522e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM getNamedProperty: %{public}s', result); 523e41f4b71Sopenharmony_ci} catch (error) { 524e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM getNamedProperty error: %{public}s', error.message); 525e41f4b71Sopenharmony_ci} 526e41f4b71Sopenharmony_ci``` 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ci### OH_JSVM_HasNamedProperty 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ciUse **OH_JSVM_HasNamedProperty** to check whether a JS object contains the specified property. 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ciCPP code: 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci```cpp 535e41f4b71Sopenharmony_ci// Register the HasNamedProperty callback. 536e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 537e41f4b71Sopenharmony_ci {.data = nullptr, .callback = HasNamedProperty}, 538e41f4b71Sopenharmony_ci}; 539e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 540e41f4b71Sopenharmony_ci// Set a property descriptor named hasNamedProperty and associate it with a callback. This allows the HasNamedProperty callback to be called from JS. 541e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 542e41f4b71Sopenharmony_ci {"hasNamedProperty", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 543e41f4b71Sopenharmony_ci}; 544e41f4b71Sopenharmony_ci// Define OH_JSVM_HasNamedProperty. 545e41f4b71Sopenharmony_cistatic JSVM_Value HasNamedProperty(JSVM_Env env, JSVM_CallbackInfo info) 546e41f4b71Sopenharmony_ci{ 547e41f4b71Sopenharmony_ci // Obtain the two parameters passed from JS. 548e41f4b71Sopenharmony_ci size_t argc = 2; 549e41f4b71Sopenharmony_ci JSVM_Value args[2] = {nullptr}; 550e41f4b71Sopenharmony_ci char strKey[32] = ""; 551e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 552e41f4b71Sopenharmony_ci // Obtain the property name. 553e41f4b71Sopenharmony_ci size_t keyLength; 554e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, args[1], strKey, 32, &keyLength); 555e41f4b71Sopenharmony_ci // Check whether the object has the specified property and store the result in hasProperty. 556e41f4b71Sopenharmony_ci bool hasProperty = false; 557e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_HasNamedProperty(env, args[0], strKey, &hasProperty); 558e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 559e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM OH_JSVM_HasNamedProperty failed"); 560e41f4b71Sopenharmony_ci return nullptr; 561e41f4b71Sopenharmony_ci } else { 562e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_HasNamedProperty success:%{public}d", hasProperty); 563e41f4b71Sopenharmony_ci } 564e41f4b71Sopenharmony_ci // Convert the bool value to JSVM_Value and return JSVM_Value. 565e41f4b71Sopenharmony_ci JSVM_Value result; 566e41f4b71Sopenharmony_ci OH_JSVM_GetBoolean(env, hasProperty, &result); 567e41f4b71Sopenharmony_ci return result; 568e41f4b71Sopenharmony_ci} 569e41f4b71Sopenharmony_ci``` 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciArkTS code: 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci```ts 574e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 575e41f4b71Sopenharmony_ci// Import the native APIs. 576e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 577e41f4b71Sopenharmony_cilet script: string = ` 578e41f4b71Sopenharmony_ci let obj = { data: 0, message: "hello world", 50: 1}; 579e41f4b71Sopenharmony_ci hasNamedProperty(obj, "message") 580e41f4b71Sopenharmony_ci` 581e41f4b71Sopenharmony_citry { 582e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 583e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM hasNamedProperty: %{public}s', result); 584e41f4b71Sopenharmony_ci} catch (error) { 585e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM hasNamedProperty error: %{public}s', error.message); 586e41f4b71Sopenharmony_ci} 587e41f4b71Sopenharmony_ci``` 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci### OH_JSVM_DefineProperties 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ciUse **OH_JSVM_DefineProperties** to define properties for an object. 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ciCPP code: 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci```cpp 596e41f4b71Sopenharmony_ci// Register the DefineProperties callback. 597e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 598e41f4b71Sopenharmony_ci {.data = nullptr, .callback = DefineProperties}, 599e41f4b71Sopenharmony_ci}; 600e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 601e41f4b71Sopenharmony_ci// Set a property descriptor named defineProperties and associate it with a callback. This allows the DefineProperties callback to be called from JS. 602e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 603e41f4b71Sopenharmony_ci {"defineProperties", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 604e41f4b71Sopenharmony_ci}; 605e41f4b71Sopenharmony_ci// Define OH_JSVM_DefineProperties. 606e41f4b71Sopenharmony_cistatic JSVM_Value DefineMethodPropertiesExample(JSVM_Env env, JSVM_CallbackInfo info) 607e41f4b71Sopenharmony_ci{ 608e41f4b71Sopenharmony_ci int32_t propValue = 26; 609e41f4b71Sopenharmony_ci JSVM_Value returnValue; 610e41f4b71Sopenharmony_ci OH_JSVM_CreateInt32(env, propValue, &returnValue); 611e41f4b71Sopenharmony_ci return returnValue; 612e41f4b71Sopenharmony_ci} 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ci// Define a callback for Getter. 615e41f4b71Sopenharmony_cistatic JSVM_Value GetterCallback(JSVM_Env env, JSVM_CallbackInfo info) 616e41f4b71Sopenharmony_ci{ 617e41f4b71Sopenharmony_ci JSVM_Value result; 618e41f4b71Sopenharmony_ci const char *str = "Hello world!"; 619e41f4b71Sopenharmony_ci size_t length = strlen(str); 620e41f4b71Sopenharmony_ci // Create property values. 621e41f4b71Sopenharmony_ci OH_JSVM_CreateStringUtf8(env, str, length, &result); 622e41f4b71Sopenharmony_ci return result; 623e41f4b71Sopenharmony_ci} 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_cistatic JSVM_Value RunScriptAndLogResult(JSVM_Env env, const std::string &srcCode) { 626e41f4b71Sopenharmony_ci JSVM_Value sourceCodeValue; 627e41f4b71Sopenharmony_ci OH_JSVM_CreateStringUtf8(env, srcCode.c_str(), srcCode.size(), &sourceCodeValue); 628e41f4b71Sopenharmony_ci JSVM_Script script; 629e41f4b71Sopenharmony_ci // Compile the JS code string and return the compiled script. 630e41f4b71Sopenharmony_ci OH_JSVM_CompileScript(env, sourceCodeValue, nullptr, 0, true, nullptr, &script); 631e41f4b71Sopenharmony_ci JSVM_Value jsVmResult; 632e41f4b71Sopenharmony_ci // Execute the JS script. 633e41f4b71Sopenharmony_ci OH_JSVM_RunScript(env, script, &jsVmResult); 634e41f4b71Sopenharmony_ci return jsVmResult; 635e41f4b71Sopenharmony_ci} 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_cistatic JSVM_Value DefineProperties(JSVM_Env env, JSVM_CallbackInfo info) { 638e41f4b71Sopenharmony_ci // Obtain an empty object passed from JS. 639e41f4b71Sopenharmony_ci size_t argc = 2; 640e41f4b71Sopenharmony_ci JSVM_Value argv[2] = {nullptr}; 641e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr); 642e41f4b71Sopenharmony_ci // Create a property value of the string type. 643e41f4b71Sopenharmony_ci size_t length = 0; 644e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, argv[1], nullptr, 0, &length); 645e41f4b71Sopenharmony_ci char *buf = (char *)malloc(length + 1); 646e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, argv[1], buf, length + 1, &length); 647e41f4b71Sopenharmony_ci JSVM_Value stringValue; 648e41f4b71Sopenharmony_ci OH_JSVM_CreateStringUtf8(env, "Hello!", JSVM_AUTO_LENGTH, &stringValue); 649e41f4b71Sopenharmony_ci JSVM_CallbackStruct param[] = { 650e41f4b71Sopenharmony_ci {.data = nullptr, .callback = DefineMethodPropertiesExample}, 651e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetterCallback}, 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci }; 654e41f4b71Sopenharmony_ci JSVM_PropertyDescriptor descriptor[] = { 655e41f4b71Sopenharmony_ci // Define a property value of the method type. 656e41f4b71Sopenharmony_ci {"defineMethodPropertiesExample", nullptr, ¶m[0], nullptr, nullptr, nullptr, JSVM_DEFAULT}, 657e41f4b71Sopenharmony_ci // Define the property value of the string type. 658e41f4b71Sopenharmony_ci {"defineStringPropertiesExample", nullptr, nullptr, nullptr, nullptr, stringValue, JSVM_DEFAULT}, 659e41f4b71Sopenharmony_ci // Define the property value of the getter type. 660e41f4b71Sopenharmony_ci {"getterCallback", nullptr, nullptr, ¶m[1], nullptr, nullptr,JSVM_DEFAULT}}; 661e41f4b71Sopenharmony_ci // Create a property value of the method type. 662e41f4b71Sopenharmony_ci // Define the defineMethodPropertiesExample function for the obj object and a variable in this function, and return it. 663e41f4b71Sopenharmony_ci // When the obj object is called, this function can be invoked. 664e41f4b71Sopenharmony_ci static std::string srcMethod; 665e41f4b71Sopenharmony_ci JSVM_Status statusMethod; 666e41f4b71Sopenharmony_ci statusMethod = OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[0]), descriptor); 667e41f4b71Sopenharmony_ci // Run obj.defineMethodPropertiesExample() and return the result to JS. 668e41f4b71Sopenharmony_ci srcMethod = R"JS(obj.defineMethodPropertiesExample();)JS"; 669e41f4b71Sopenharmony_ci JSVM_Value jsVmResult = RunScriptAndLogResult(env, srcMethod); 670e41f4b71Sopenharmony_ci // Create a property value of the string type. 671e41f4b71Sopenharmony_ci static std::string srcString; 672e41f4b71Sopenharmony_ci JSVM_Status statusString; 673e41f4b71Sopenharmony_ci statusString = OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[1]), descriptor); 674e41f4b71Sopenharmony_ci // Run obj.defineStringPropertiesExample() and return the result to JS. 675e41f4b71Sopenharmony_ci srcString = R"JS(obj.defineStringPropertiesExample;)JS"; 676e41f4b71Sopenharmony_ci JSVM_Value jsVmResult1 = RunScriptAndLogResult(env, srcString); 677e41f4b71Sopenharmony_ci // Define a property with getter. 678e41f4b71Sopenharmony_ci static std::string srcGetter; 679e41f4b71Sopenharmony_ci JSVM_Status statusGetter; 680e41f4b71Sopenharmony_ci statusGetter = OH_JSVM_DefineProperties(env, *argv, sizeof(descriptor) / sizeof(descriptor[2]), descriptor); 681e41f4b71Sopenharmony_ci // Call getterCallback() of obj and return the result string to JS. 682e41f4b71Sopenharmony_ci srcGetter = R"JS(obj.getterCallback;)JS"; 683e41f4b71Sopenharmony_ci JSVM_Value jsVmResult2 = RunScriptAndLogResult(env, srcGetter); 684e41f4b71Sopenharmony_ci if (statusMethod != JSVM_OK || statusString != JSVM_OK || statusGetter != JSVM_OK) { 685e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "JSVM DefineProperties fail"); 686e41f4b71Sopenharmony_ci return nullptr; 687e41f4b71Sopenharmony_ci } else if (statusMethod == JSVM_OK) { 688e41f4b71Sopenharmony_ci int32_t number; 689e41f4b71Sopenharmony_ci OH_JSVM_GetValueInt32(env, jsVmResult, &number); 690e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM DefineMethodPropertiesExample success:%{public}d", number); 691e41f4b71Sopenharmony_ci } else if (statusString == JSVM_OK) { 692e41f4b71Sopenharmony_ci size_t length = 0; 693e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, jsVmResult1, nullptr, 0, &length); 694e41f4b71Sopenharmony_ci char *buf = (char *)malloc(length + 1); 695e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, jsVmResult1, buf, length + 1, &length); 696e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM defineStringPropertiesExample success:%{public}s", buf); 697e41f4b71Sopenharmony_ci } else if (statusGetter == JSVM_OK) { 698e41f4b71Sopenharmony_ci size_t length = 0; 699e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, jsVmResult2, nullptr, 0, &length); 700e41f4b71Sopenharmony_ci char *buf = (char *)malloc(length + 1); 701e41f4b71Sopenharmony_ci OH_JSVM_GetValueStringUtf8(env, jsVmResult2, buf, length + 1, &length); 702e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM getterCallback success:%{public}s", buf); 703e41f4b71Sopenharmony_ci } 704e41f4b71Sopenharmony_ci return jsVmResult; 705e41f4b71Sopenharmony_ci} 706e41f4b71Sopenharmony_ci``` 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ciArkTS code: 709e41f4b71Sopenharmony_ci 710e41f4b71Sopenharmony_ci```ts 711e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 712e41f4b71Sopenharmony_ci// Import the native APIs. 713e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 714e41f4b71Sopenharmony_ci// Define a property of the method type. 715e41f4b71Sopenharmony_cilet script: string = ` 716e41f4b71Sopenharmony_ci let obj = {}; 717e41f4b71Sopenharmony_ci defineProperties(obj) 718e41f4b71Sopenharmony_ci` 719e41f4b71Sopenharmony_cilet result = napitest.runJsVm(script); 720e41f4b71Sopenharmony_cihilog.info(0x0000, 'testJSVM', 'Test JSVM defineGetterProperties: %{public}s', result); 721e41f4b71Sopenharmony_ci``` 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_ci### OH_JSVM_GetAllPropertyNames 724e41f4b71Sopenharmony_ci 725e41f4b71Sopenharmony_ciUse **OH_JSVM_GetAllPropertyNames** to obtain the names of all available properties of a JS object as a JS array. 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ciCPP code: 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci```cpp 730e41f4b71Sopenharmony_ci// Register the GetAllPropertyNames callback. 731e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct param[] = { 732e41f4b71Sopenharmony_ci {.data = nullptr, .callback = GetAllPropertyNames}, 733e41f4b71Sopenharmony_ci}; 734e41f4b71Sopenharmony_cistatic JSVM_CallbackStruct *method = param; 735e41f4b71Sopenharmony_ci// Set a property descriptor named getAllPropertyNames and associate it with a callback. This allows the GetAllPropertyNames callback to be called from JS. 736e41f4b71Sopenharmony_cistatic JSVM_PropertyDescriptor descriptor[] = { 737e41f4b71Sopenharmony_ci {"getAllPropertyNames", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 738e41f4b71Sopenharmony_ci}; 739e41f4b71Sopenharmony_ci// Define OH_JSVM_GetAllPropertyNames. 740e41f4b71Sopenharmony_cistatic JSVM_Value GetAllPropertyNames(JSVM_Env env, JSVM_CallbackInfo info) 741e41f4b71Sopenharmony_ci{ 742e41f4b71Sopenharmony_ci // Obtain the parameter passed from JS. 743e41f4b71Sopenharmony_ci size_t argc = 1; 744e41f4b71Sopenharmony_ci JSVM_Value args[1]; 745e41f4b71Sopenharmony_ci OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 746e41f4b71Sopenharmony_ci // Obtain names of all properties (own properties) of the specified object. 747e41f4b71Sopenharmony_ci JSVM_Value result; 748e41f4b71Sopenharmony_ci JSVM_Status status = OH_JSVM_GetAllPropertyNames(env, args[0], 749e41f4b71Sopenharmony_ci JSVM_KeyCollectionMode::JSVM_KEY_OWN_ONLY, 750e41f4b71Sopenharmony_ci JSVM_KeyFilter::JSVM_KEY_WRITABLE, 751e41f4b71Sopenharmony_ci JSVM_KeyConversion::JSVM_KEY_NUMBERS_TO_STRINGS, &result); 752e41f4b71Sopenharmony_ci if (status != JSVM_OK) { 753e41f4b71Sopenharmony_ci OH_JSVM_ThrowError(env, nullptr, "Failed to get allpropertynames"); 754e41f4b71Sopenharmony_ci return nullptr; 755e41f4b71Sopenharmony_ci } else { 756e41f4b71Sopenharmony_ci OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_GetAllPropertyNames success"); 757e41f4b71Sopenharmony_ci } 758e41f4b71Sopenharmony_ci return result; 759e41f4b71Sopenharmony_ci} 760e41f4b71Sopenharmony_ci``` 761e41f4b71Sopenharmony_ci 762e41f4b71Sopenharmony_ciArkTS code: 763e41f4b71Sopenharmony_ci 764e41f4b71Sopenharmony_ci```ts 765e41f4b71Sopenharmony_ciimport hilog from "@ohos.hilog" 766e41f4b71Sopenharmony_ci// Import the native APIs. 767e41f4b71Sopenharmony_ciimport napitest from "libentry.so" 768e41f4b71Sopenharmony_cilet obj = '{ data: 0, message: "hello world", 50: 1}'; 769e41f4b71Sopenharmony_cilet script: string = ` 770e41f4b71Sopenharmony_ci getAllPropertyNames(${obj}) 771e41f4b71Sopenharmony_ci ` 772e41f4b71Sopenharmony_citry { 773e41f4b71Sopenharmony_ci let result = napitest.runJsVm(script); 774e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testJSVM', 'Test JSVM GetAllPropertyNames: %{public}s', result); 775e41f4b71Sopenharmony_ci} catch (error) { 776e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testJSVM', 'Test JSVM GetAllPropertyNames error: %{public}s', error.message); 777e41f4b71Sopenharmony_ci} 778e41f4b71Sopenharmony_ci``` 779