目录

Working with Other Node-API Utilities

Introduction

Node-API also provides some useful APIs to improve development experience.

Basic Concepts

Available APIs

API Description
node_api_get_module_file_name Obtains the absolute path of the module to be loaded.
napi_strict_equals Checks whether two values are strictly equal, that is, equal in both the value and type. For example, you can use napi_strict_equals to ensure data consistency when working on data structs or algorithms of the specific type.

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 the APIs mentioned in this topic.

[h2]node_api_get_module_file_name

Use node_api_get_module_file_name to obtain the absolute path of the module to be loaded.

CPP code:

#include "napi/native_api.h"

static napi_value GetModuleFileName(napi_env env, napi_callback_info info)
{
    // Declare file, a pointer variable of the const char type, to store the absolute path of the module.
    const char *file = nullptr;
    napi_value value = nullptr;
    // Obtain the absolute path of the module and store it in the file variable.
    napi_status status = node_api_get_module_file_name(env, &file);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Failed to get module file name");
        return nullptr;
    }
    // Create a string of the napi_value type that contains the absolute path.
    napi_create_string_utf8(env, file, NAPI_AUTO_LENGTH, &value);
    return value;
}

API declaration:

// index.d.ts
export const getModuleFileName: () => string | undefined;

ArkTS code:

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

try {
    let filename = testNapi.getModuleFileName();
    hilog.info(0x0000, 'testTag', 'Test Node-API node_api_get_module_file_name:%{public}s', filename);
} catch (error) {
    hilog.error(0x0000, 'testTag', 'Test Node-API node_api_get_module_file_name error: %{public}s', error.message);
}

[h2]napi_strict_equals

Use napi_strict_equals to check whether two ArkTS values are strictly equal.

CPP code:

#include "napi/native_api.h"

static napi_value StrictEquals(napi_env env, napi_callback_info info)
{
    // Obtain 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);
    // Call napi_strict_equals to check whether two ArkTS values are strictly equal.
    bool result = true;
    napi_status status = napi_strict_equals(env, args[0], args[1], &result);
    if (status != napi_ok) {
        napi_throw_error(env, nullptr, "Node-API napi_strict_equals fail");
        return nullptr;
    }
    // Convert the result to napi_value and return napi_value.
    napi_value returnValue = nullptr;
    napi_get_boolean(env, result, &returnValue);
    return returnValue;
}

API declaration:

// index.d.ts
export const strictEquals: (lhs: string, rhs: string | number) => boolean | undefined;

ArkTS code:

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

try {
  let lhs = "123";
  let rhs = "123";
  let str = "456";
  let num = 123;
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, rhs));
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, str));
  hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, num));
} catch (error) {
  hilog.error(0x0000, 'testTag', 'Test Node-API napi_strict_equals 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)