使用方法


【Usage】

为了使用 Node-API 函数,请包含位于节点开发树 src 目录中的文件 node_api.h

【In order to use the Node-API functions, include the file node_api.h which is located in the src directory in the node development tree:】

#include <node_api.h> 

这将选择使用给定版本 Node.js 的默认 NAPI_VERSION。为了确保与特定版本的 Node-API 兼容,可以在包含头文件时显式指定版本:

【This will opt into the default NAPI_VERSION for the given release of Node.js. In order to ensure compatibility with specific versions of Node-API, the version can be specified explicitly when including the header:】

#define NAPI_VERSION 3
#include <node_api.h> 

这将 Node-API 的接口限制在指定版本(及更早版本)中可用的功能范围内。

【This restricts the Node-API surface to just the functionality that was available in the specified (and earlier) versions.】

一些 Node-API 表面是实验性的,需要明确选择加入:

【Some of the Node-API surface is experimental and requires explicit opt-in:】

#define NAPI_EXPERIMENTAL
#include <node_api.h> 

在这种情况下,整个 API 接口,包括任何实验性 API,都将对模块代码可用。

【In this case the entire API surface, including any experimental APIs, will be available to the module code.】

有时会引入影响已发布和稳定 API 的实验性功能。这些功能可以通过选择不使用来禁用:

【Occasionally, experimental features are introduced that affect already-released and stable APIs. These features can be disabled by an opt-out:】

#define NAPI_EXPERIMENTAL
#define NODE_API_EXPERIMENTAL_<FEATURE_NAME>_OPT_OUT
#include <node_api.h> 

其中 <FEATURE_NAME> 是一个影响实验性和稳定性 API 的实验性功能的名称。

【where <FEATURE_NAME> is the name of an experimental feature that affects both experimental and stable APIs.】