使用方法
¥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.