目录

Setting ArkTS Object Properties Using Node-API

Introduction

Node-API provides APIs for obtaining and setting properties of ArkTS objects in C/C++ to implement more complex functionalities and logic.

Basic Concepts

Before working with ArkTS objects using Node-API, you need to understand the following concepts:

Available APIs

The following table lists the APIs for manipulating ArkTS object properties.

API Description
napi_get_property_names Obtains the names of the enumerable properties of an object in an array of strings.
napi_set_property Adds a property to an object or modifies a property value of an object.
napi_get_property Obtains the requested property of an object and passes it to another function for processing.
napi_has_property Checks whether an object has the specified property. This can prevent the exception or error caused by access to a property that does not exist.
napi_delete_property Deletes a property from an ArkTS object.
napi_has_own_property Behaves the same as napi_has_property. It checks whether the specified property exists in the object, avoiding exceptions caused by accessing a property that does not exist.
napi_set_named_property Sets a property with the specified name for an ArkTS object.
napi_get_named_property Obtains the value of a property in an ArkTS object.
napi_has_named_property Checks whether an ArkTS object has the property with the specified name.
napi_define_properties Defines multiple properties for an ArkTS object.
napi_get_all_property_names Obtains the names of all properties of an ArkTS object.

Example

If you are just starting out with Node-API, see Node-API Development Process. The following demonstrates only the C++ and ArkTS code involved in related APIs.

[h2]napi_get_property_names

Call napi_get_property_names to obtain the names of the enumerable properties of an object in an array of strings.

CPP code:

// napi_get_property_names
static napi_value GetPropertyNames(napi_env env, napi_callback_info info)
{
    // Parse the parameters passed from ArkTS.
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    // Obtain the names of the object's enumerable properties as a string array, and pass them out through result.
    napi_value result;
    napi_status status = napi_get_property_names(env, args[0], &result);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_get_property_names fail");
        return nullptr;
    }
    return result;
}

API declaration:

export const getPropertyNames: (obj: Object) => Array<string> | undefined; // napi_get_property_names

ArkTS code:

// napi_get_property_names
try {
  class Obj {
    public data: number = 0
    public message: string = ''
  }

  let obj: Obj = { data: 0, message: 'hello world' };
  let propertyNames = testNapi.getPropertyNames(obj);
  if (Array.isArray(propertyNames) && propertyNames.length > 0) {
    hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property_names: %{public}s',
      propertyNames[0]);
    hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property_names: %{public}s',
      propertyNames[1]);
    // ...
  }
} catch (error) {
  hilog.error(0x0000, 'testTag', 'Test Node-API napi_get_property_names error: %{public}s',
    error.message);
  // ...
}

[h2]napi_set_property

Call napi_set_property to set a property for an object.

CPP code:

// napi_set_property
static napi_value SetProperty(napi_env env, napi_callback_info info)
{
    // Receive three parameters from the ArkTS side: the first parameter is the object to set, the second is the property, and the third is the value corresponding to the property.
    size_t argc = 3;
    napi_value args[3] = {nullptr};
    napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_get_cb_info fail");
    }
    // Call the napi_set_property API to set the property and value into the object. If it fails, throw an error directly.
    status = napi_set_property(env, args[0], args[1], args[INT_ARG_2]);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_set_property fail");
        return nullptr;
    }
    // Return the object that was set successfully.
    return args[0];
}

API declaration:

export const setProperty: (obj: Object, key: String, value: string) => Object | undefined; // napi_set_property

ArkTS code:

// napi_set_property
try {
  class Obj {
    public data: number = 0
    public message: string = ''
  }

  let obj: Obj = { data: 0, message: 'hello world' };
  let result = testNapi.setProperty(obj, 'code', 'hi');
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_property: %{public}s',
    JSON.stringify(result));
  // ...
} catch (error) {
  hilog.error(0x0000, 'testTag', 'Test Node-API napi_set_property error: %{public}s', error.message);
  // ...
}

[h2]napi_get_property

Call napi_get_property to obtain the value of the specified property in an object.

CPP code:

// napi_get_property
static napi_value GetProperty(napi_env env, napi_callback_info info)
{
    // Receive two parameters from ArkTS.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    // The first parameter passed in is the object to check, and the second parameter is the property to check. Obtain the corresponding value by calling the napi_get_property API.
    napi_value result;
    napi_status status = napi_get_property(env, args[0], args[1], &result);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_get_property fail");
        return nullptr;
    }
    return result;
}

API declaration:

export const getProperty: (obj: Object, key: string) => string | undefined; // napi_get_property

ArkTS code:

// napi_get_property
try {
  class Obj {
    public data: number = 0
    public message: string = ''
  }

  let obj: Obj = { data: 0, message: 'hello world' };
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_property: %{public}s',
    testNapi.getProperty(obj, 'message'));
  // ...
} catch (error) {
  hilog.error(0x0000, 'testTag', 'Test Node-API napi_get_property error: %{public}s', error.message);
  // ...
}

[h2]napi_has_property

Call napi_has_property 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.

CPP code:

// napi_has_property
static napi_value HasProperty(napi_env env, napi_callback_info info)
{
    // Two parameters are passed from the ArkTS side: the first is the object to check, and the second is the property to check for existence in the object.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    // Pass the parameters to the napi_has_property method. If the API call succeeds, convert the result to napi_value and return it; otherwise, throw an error.
    bool result;
    napi_status status = napi_has_property(env, args[0], args[1], &result);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_has_property fail");
        return nullptr;
    }

    // If the property exists in the object, output true, convert the result to napi_value, and return it.
    napi_value returnResult;
    napi_get_boolean(env, result, &returnResult);
    return returnResult;
}

API declaration:

export const hasProperty: (obj: Object, key: number | string) => boolean | undefined; // napi_has_property

ArkTS code:

// napi_has_property
try {
  class Obj {
    public data: number = 0
    public message: string = ''
  }

  let obj: Obj = { data: 0, message: 'hello world' };
  let resultFalse = testNapi.hasProperty(obj, 0);
  let resultTrue = testNapi.hasProperty(obj, 'data');
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_property: %{public}s',
    JSON.stringify(resultFalse));
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_property: %{public}s',
    JSON.stringify(resultTrue));
  // ...
} catch (error) {
  hilog.error(0x0000, 'testTag', 'Test Node-API napi_has_property error: %{public}s', error.message);
  // ...
}

[h2]napi_delete_property

Call napi_delete_property to delete the property specified by key from an object.

If the object is non-extensible or the property is not configurable, the property cannot be deleted.

CPP code:

// napi_delete_property
// Delete the specified property from the passed-in Object, and return a bool result indicating whether the deletion was successful.
static napi_value DeleteProperty(napi_env env, napi_callback_info info)
{
    // Receive two parameters from ArkTS.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_valuetype valueType;
    napi_typeof(env, args[0], &valueType);
    if (valueType != napi_object) {
        napi_throw_error(env, nullptr, "Expects an object as argument.");
        return nullptr;
    }
    // Delete the specified property, and store the result in result.
    bool result = false;
    napi_status status = napi_delete_property(env, args[0], args[1], &result);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_delete_property failed");
        return nullptr;
    }
    // Convert the bool result to napi_value and return it.
    napi_value ret;
    napi_get_boolean(env, result, &ret);
    return ret;
}

API declaration:

export const deleteProperty: (obj: Object, key: string) => boolean; // napi_delete_property

ArkTS code:

import testNapi from 'libentry.so';
import { hilog } from '@kit.PerformanceAnalysisKit';

export function napiDeleteProperty() {
  class Obj {
    first: number = 0;
  }

  let obj: Obj = { first: 1 };
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_delete_property first: %{public}s',
    testNapi.deleteProperty(obj, 'first'));
  // Set the new property as non-configurable
  // The Object.defineProperty method is not supported in DevEco Studio 4.1.0.400 and above versions, and needs to be used in TS (TypeScript)
  Object.defineProperty(obj, 'config', {
    configurable: false,
    value: 'value'
  })
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_delete_property config: %{public}s',
    testNapi.deleteProperty(obj, 'config'));
}

[h2]napi_has_own_property

Behaves consistently with napi_has_property. It is used to check whether a specified property exists in an object, avoiding exceptions caused by accessing a property that does not exist.

CPP code:

// napi_has_own_property
static napi_value NapiHasOwnProperty(napi_env env, napi_callback_info info)
{
    // Receive two parameters from ArkTS.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    // Check whether the first parameter is an object.
    napi_valuetype valueTypeObj;
    napi_typeof(env, args[0], &valueTypeObj);
    if (valueTypeObj != napi_object) {
        napi_throw_error(env, nullptr, "First argument must be an object.");
        return nullptr;
    }
    // Check whether the second parameter is a string.
    napi_valuetype valuetypeStr;
    napi_typeof(env, args[1], &valuetypeStr);
    if (valuetypeStr != napi_string) {
        napi_throw_error(env, nullptr, "Second argument must be a string.");
        return nullptr;
    }
    // Check whether the object has the specified property, and store the result in hasProperty.
    bool hasProperty;
    napi_status status = napi_has_own_property(env, args[0], args[1], &hasProperty);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_has_own_property failed");
        return nullptr;
    }
    // Convert the bool result to napi_value and return it.
    napi_value result;
    napi_get_boolean(env, hasProperty, &result);
    return result;
}

API declaration:

export const napiHasOwnProperty: (obj: Object, key: string) => boolean | undefined; // napi_has_own_property

ArkTS code:

import { hilog } from '@kit.PerformanceAnalysisKit';
import testNapi from 'libentry.so';

export function napiHasOwnProperty() {
  let myObj = { 'myProperty': 1 };
  let inheritedObj = { 'inheritedProperty': 2 };
  // The Object.setPrototypeOf method is not supported in DevEco Studio 4.1.0.400 and later versions, and must be used in TypeScript (TS).
  Object.setPrototypeOf(myObj, inheritedObj);
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_own_property my: %{public}s',
    testNapi.napiHasOwnProperty(myObj, 'myProperty'));
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_own_property inherited: %{public}s',
    testNapi.napiHasOwnProperty(myObj, 'inheritedProperty'));
}

Currently, the behavior of napi_has_own_property is consistent with that of napi_has_property (it traverses the prototype chain). If you need to check only own properties without traversing the prototype chain, you can use Node-API to equivalently implement the functionality of Object.prototype.hasOwnProperty.call(obj, key). The C++ example code is as follows:

// Implement the napi_has_own_property functionality equivalently (check only own properties).
static napi_value HasOwnPropertyEquivalent(napi_env env, napi_callback_info info)
{
    // Receive the two parameters passed from ArkTS.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    // Check whether the first parameter is an object.
    napi_valuetype valueTypeObj;
    napi_typeof(env, args[0], &valueTypeObj);
    if (valueTypeObj != napi_object) {
        napi_throw_error(env, nullptr, "First argument must be an object.");
        return nullptr;
    }
    // Check whether the second parameter is a string.
    valueTypeObj = napi_undefined;
    napi_typeof(env, args[1], &valueTypeObj);
    if (valueTypeObj != napi_string) {
        napi_throw_error(env, nullptr, "Second argument must be a string.");
        return nullptr;
    }
    // Obtain the global object.
    napi_value global = nullptr;
    napi_status status = napi_get_global(env, &global);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_get_global failed");
        return nullptr;
    }
    // Obtain the Object constructor.
    napi_value objectCtor = nullptr;
    status = napi_get_named_property(env, global, "Object", &objectCtor);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "get Object failed");
        return nullptr;
    }
    // Obtain Object.prototype.
    napi_value prototype = nullptr;
    status = napi_get_named_property(env, objectCtor, "prototype", &prototype);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "get Object.prototype failed");
        return nullptr;
    }
    // Obtain the Object.prototype.hasOwnProperty function.
    napi_value hasOwnProperty = nullptr;
    status = napi_get_named_property(env, prototype, "hasOwnProperty", &hasOwnProperty);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "get hasOwnProperty failed");
        return nullptr;
    }
    // Call hasOwnProperty with args[0] as this and args[1] as the argument,
    // equivalent to Object.prototype.hasOwnProperty.call(obj, key), which checks only own properties without traversing the prototype chain.
    napi_value argv[1] = { args[1] };
    napi_value callResult = nullptr;
    status = napi_call_function(env, args[0], hasOwnProperty, 1, argv, &callResult);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_call_function failed");
        return nullptr;
    }
    // Convert the result to bool and return it.
    bool hasProperty = false;
    napi_get_value_bool(env, callResult, &hasProperty);
    napi_value result;
    napi_get_boolean(env, hasProperty, &result);
    return result;
}

[h2]napi_set_named_property

Call napi_set_named_property to set a property for an ArkTS object.

CPP code:

// napi_set_named_property
static napi_value NapiSetNamedProperty(napi_env env, napi_callback_info info)
{
    // Receive one parameter from ArkTS.
    size_t argc = 1;
    napi_value str;
    const int32_t strLength = 32;
    char strKey[strLength] = "";
    napi_get_cb_info(env, info, &argc, &str, nullptr, nullptr);
    // Obtain the passed-in parameter string and store it in strKey.
    size_t keyLength;
    napi_status status = napi_get_value_string_utf8(env, str, strKey, strLength, &keyLength);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_get_value_string_utf8 failed");
        return nullptr;
    }
    // Create a new object.
    napi_value newObj;
    napi_create_object(env, &newObj);
    // Set the integer value 1234 as the property value.
    int32_t value = 1234;
    napi_value numValue;
    napi_create_int32(env, value, &numValue);
    // Associate the integer value with the specified property name.
    status = napi_set_named_property(env, newObj, strKey, numValue);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_set_named_property failed");
        return nullptr;
    }
    // Return the object newObj with the named property set.
    return newObj;
}

API declaration:

export const napiSetNamedProperty: (key: string) => Object | undefined; // napi_set_named_property

ArkTS code:

// napi_set_named_property
let obj = testNapi.napiSetNamedProperty('myProperty');
let objAsString = JSON.stringify(obj);
hilog.info(0x0000, 'testTag', 'Test Node-API napi_set_named_property: %{public}s', objAsString);

[h2]napi_get_named_property

Call napi_get_named_property to obtain the value of the specified property from an ArkTS object.

CPP code:

// napi_get_named_property
static napi_value NapiGetNamedProperty(napi_env env, napi_callback_info info)
{
    // Receive two parameters from ArkTS.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    const int32_t strLength = 32;
    char strKey[strLength] = "";
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    // Obtain the name of the property to retrieve.
    size_t keyLength;
    napi_get_value_string_utf8(env, args[1], strKey, strLength, &keyLength);
    // Obtain the value of the specified property and store it in result.
    napi_value result;
    napi_status status = napi_get_named_property(env, args[0], strKey, &result);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_get_named_property failed");
        return nullptr;
    }
    // Return result.
    return result;
}

API declaration:

export const napiGetNamedProperty: (obj: Object,
  key: string) => boolean | number | string | Object | undefined; // napi_get_named_property

ArkTS code:

// napi_get_named_property
interface NestedObj {
  nestedStr: string;
  nestedNum: number;
}

class Obj {
  public str: string = '';
  public num: number = 0;
  public bol: boolean = false;
  public nestedObj: NestedObj = { nestedStr: '', nestedNum: 0 };
}

let obj: Obj = {
  str: 'bar',
  num: 42,
  bol: true,
  nestedObj: { nestedStr: 'nestedValue', nestedNum: 123 }
};
hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s',
  testNapi.napiGetNamedProperty(obj, 'str'));
hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}d',
  testNapi.napiGetNamedProperty(obj, 'num'));
hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s',
  testNapi.napiGetNamedProperty(obj, 'bol'));
let nestedObj = testNapi.napiGetNamedProperty(obj, 'nestedObj');
let objAsString = JSON.stringify(nestedObj);
hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s', objAsString);
hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_named_property : %{public}s',
  testNapi.napiGetNamedProperty(obj, 'null'));

[h2]napi_has_named_property

Call napi_has_named_property to check whether an ArkTS object contains the specified property.

CPP code:

// napi_has_named_property
static napi_value NapiHasNamedProperty(napi_env env, napi_callback_info info)
{
    // Receive two parameters from ArkTS.
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    const int32_t strLength = 32;
    char strKey[strLength] = "";
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    // Obtain the name of the property to check.
    size_t keyLength;
    napi_get_value_string_utf8(env, args[1], strKey, strLength, &keyLength);
    // Check whether the object has the specified named property, and store the result in hasProperty.
    bool hasProperty = false;
    napi_status status = napi_has_named_property(env, args[0], strKey, &hasProperty);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "napi_has_named_property failed");
        return nullptr;
    }
    // Convert the bool result to napi_value and return it.
    napi_value result;
    napi_get_boolean(env, hasProperty, &result);
    return result;
}

API declaration:

export const napiHasNamedProperty: (obj: Object, key: string) => boolean | undefined; // napi_has_named_property

ArkTS code:

// napi_has_named_property
interface NestedObj {
  nestedStr: string;
  nestedNum: number;
}

class Obj {
  public str: string = '';
  public num: number = 0;
  public bol: boolean = false;
  public nestedObj: NestedObj = { nestedStr: '', nestedNum: 0 };
}

let obj: Obj = {
  str: 'bar',
  num: 42,
  bol: true,
  nestedObj: { nestedStr: 'nestedValue', nestedNum: 123 }
};
hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_named_property : %{public}s',
  testNapi.napiHasNamedProperty(obj, 'str'));
hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_named_property : %{public}s',
  testNapi.napiHasNamedProperty(obj, 'nestedStr'));
hilog.info(0x0000, 'testTag', 'Test Node-API napi_has_named_property : %{public}s',
  testNapi.napiHasNamedProperty(obj, 'bol'));

[h2]napi_define_properties

Call napi_define_properties to set object properties.

CPP code:

// napi_define_properties
static napi_value DefineMethodPropertiesExample(napi_env env, napi_callback_info info)
{
    // Create a property value of the int32 type.
    int32_t propValue = 26;
    napi_value returnValue = nullptr;
    napi_create_int32(env, propValue, &returnValue);
    return returnValue;
}

// Getter callback function.
static napi_value GetterCallback(napi_env env, napi_callback_info info)
{
    napi_value result;
    const char *str = u8"World!";
    size_t length = strlen(str);
    // Create the value of the property.
    napi_create_string_utf8(env, str, length, &result);
    return result;
}

// Setter callback function.
static napi_value SetterCallback(napi_env env, napi_callback_info info)
{
    // Obtain the parameters passed to the setter.
    size_t argc = 1;
    napi_value argv[1] = {nullptr};
    napi_value result;
    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
    size_t length = 0;
    napi_get_value_string_utf8(env, argv[0], nullptr, 0, &length);
    char* buf = new char[length + 1];
    std::memset(buf, 0, length + 1);
    napi_get_value_string_utf8(env, argv[0], buf, length + 1, &length);
    napi_create_string_utf8(env, buf, length, &result);
    delete[] buf;
    return result;
}

static napi_value DefineMethodProperties(napi_env env, napi_callback_info info)
{
    napi_value obj;
    napi_create_object(env, &obj);
    // Define a function defineMethodPropertiesExample on the obj object. In the function defineMethodPropertiesExample, a variable is defined and returned. This function can be called when invoking this object of obj.
    napi_property_descriptor descriptor[] = {{"defineMethodPropertiesExample", nullptr, DefineMethodPropertiesExample,
                                              nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, obj, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
    return obj;
}

static napi_value DefineStringProperties(napi_env env, napi_callback_info info)
{
    napi_value obj;
    napi_create_object(env, &obj);
    // Create a property value of the string type.
    napi_value string_value;
    napi_create_string_utf8(env, "Hello!", NAPI_AUTO_LENGTH, &string_value);
    napi_property_descriptor descriptor[] = {
        {"defineStringPropertiesExample", nullptr, nullptr, nullptr, nullptr, string_value, napi_default, nullptr}};
    napi_define_properties(env, obj, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
    return obj;
}

static napi_value CreateStringWithGetterSetter(napi_env env, napi_callback_info info)
{
    napi_value obj;
    napi_create_object(env, &obj);
    // Define the getter function.
    napi_value getterFn;
    napi_create_function(env, nullptr, 0, GetterCallback, nullptr, &getterFn);
    napi_set_named_property(env, obj, "getterCallback", getterFn);
    // Define the setter function.
    napi_value setterFn;
    napi_create_function(env, nullptr, 0, SetterCallback, nullptr, &setterFn);
    napi_set_named_property(env, obj, "setterCallback", setterFn);
    // Define a property with getter and setter.
    napi_property_descriptor desc = {"defineGetterSetter", nullptr, nullptr, GetterCallback, SetterCallback, nullptr,
                                     napi_enumerable, nullptr};
    napi_define_properties(env, obj, 1, &desc);
    return obj;
}

API declaration:

export class DefineMethodObj {
  defineMethodPropertiesExample: Function;
}

export class DefineStringObj {
  defineStringPropertiesExample: string;
}

export class DefineGetterSetterObj {
  getterCallback: Function;
  setterCallback: Function;
}

export const defineMethodProperties: () => DefineMethodObj; // napi_define_properties

export const defineStringProperties: () => DefineStringObj;

export const createStringWithGetterSetter: () => DefineGetterSetterObj;

ArkTS code:

// napi_define_properties
// Define a property of the method type.
hilog.info(0x0000, 'testTag', 'Test Node-API define_method_properties:%{public}d',
  testNapi.defineMethodProperties()
    .defineMethodPropertiesExample());
// Define a property of the string type.
hilog.info(0x0000, 'testTag', 'Test Node-API define_string_properties:%{public}s ',
  testNapi.defineStringProperties()
    .defineStringPropertiesExample);
// Getter and setter.
hilog.info(0x0000, 'testTag', 'Test Node-API get:%{public}s ',
  testNapi.createStringWithGetterSetter()
    .getterCallback());
hilog.info(0x0000, 'testTag', 'Test Node-API setter:%{public}s ',
  testNapi.createStringWithGetterSetter()
    .setterCallback('set data'));

[h2]napi_get_all_property_names

Call napi_get_all_property_names to obtain all property names in an ArkTS object.

CPP code:

// napi_get_all_property_names
static napi_value GetAllPropertyNames(napi_env env, napi_callback_info info)
{
    // Pass in one parameter.
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    // Obtain all property names of the given object.
    napi_value result;
    napi_status status = napi_get_all_property_names(env, args[0], napi_key_own_only, napi_key_writable,
                                                     napi_key_numbers_to_strings, &result);
    // If obtaining the property names fails, throw an error.
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_get_all_property_names fail");
        return nullptr;
    }

    return result;
}

API declaration:

export const getAllPropertyNames: (obj: Object) => Array<string> | undefined; // napi_get_all_property_names

ArkTS code:

// napi_get_all_property_names
try {
  class Obj {
    public data: number = 0
    public message: string = ''
  }

  let obj: Obj = { data: 0, message: 'hello world' };
  let propertyNames = testNapi.getAllPropertyNames(obj);
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_get_all_property_names: %{public}s',
    JSON.stringify(propertyNames));
  // ...
} catch (error) {
  hilog.error(0x0000, 'testTag', 'Test Node-API napi_get_all_property_names error: %{public}s',
    error.message);
  // ...
}

To 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".

// CMakeLists.txt
add_definitions( "-DLOG_DOMAIN=0xd0d0" )
add_definitions( "-DLOG_TAG=\"testTag\"" )
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so)