使用 JavaScript 值
【Working with JavaScript values】
Node-API 提供了一组用于创建所有类型 JavaScript 值的 API。这些类型中的一些在 ECMAScript 语言规范 的 章节语言类型 下有所记录。
【Node-API exposes a set of APIs to create all types of JavaScript values. Some of these types are documented under Section language types of the ECMAScript Language Specification.】
从根本上说,这些 API 用于执行以下操作之一:
【Fundamentally, these APIs are used to do one of the following:】
- 创建一个新的 JavaScript 对象
- 将原始 C 类型转换为 Node-API 值
- 将 Node-API 值转换为原始 C 类型
- 获取全局实例,包括
undefined和null
Node-API 的值由类型 napi_value 表示。任何需要 JavaScript 值的 Node-API 调用都会接收一个 napi_value。在某些情况下,API 确实会提前检查 napi_value 的类型。然而,为了获得更好的性能,调用者最好确保所使用的 napi_value 是 API 所期望的 JavaScript 类型。
【Node-API values are represented by the type napi_value.
Any Node-API call that requires a JavaScript value takes in a napi_value.
In some cases, the API does check the type of the napi_value up-front.
However, for better performance, it's better for the caller to make sure that
the napi_value in question is of the JavaScript type expected by the API.】