napi_create_external


napi_status napi_create_external(napi_env env,
                                 void* data,
                                 napi_finalize finalize_cb,
                                 void* finalize_hint,
                                 napi_value* result) 
  • [in] env:调用 API 的环境。

    ¥[in] env: The environment that the API is invoked under.

  • [in] data:指向外部数据的原始指针。

    ¥[in] data: Raw pointer to the external data.

  • [in] finalize_cb:收集外部值时调用的可选回调。napi_finalize 提供了更多详细信息。

    ¥[in] finalize_cb: Optional callback to call when the external value is being collected. napi_finalize provides more details.

  • [in] finalize_hint:在收集期间传递给最终回调的可选提示。

    ¥[in] finalize_hint: Optional hint to pass to the finalize callback during collection.

  • [out] result:表示外部值的 napi_value

    ¥[out] result: A napi_value representing an external value.

如果 API 成功,则返回 napi_ok

¥Returns napi_ok if the API succeeded.

此 API 分配一个附加有外部数据的 JavaScript 值。这用于通过 JavaScript 代码传递外部数据,因此稍后可以使用 napi_get_value_external 由原生代码检索。

¥This API allocates a JavaScript value with external data attached to it. This is used to pass external data through JavaScript code, so it can be retrieved later by native code using napi_get_value_external.

API 添加了一个 napi_finalize 回调,当刚刚创建的 JavaScript 对象准备好进行垃圾回收时将调用该回调。它与 napi_wrap() 相似,不同之处在于:

¥The API adds a napi_finalize callback which will be called when the JavaScript object just created is ready for garbage collection. It is similar to napi_wrap() except that:

  • 以后无法使用 napi_unwrap() 检索原生数据,

    ¥the native data cannot be retrieved later using napi_unwrap(),

  • 以后也不能使用 napi_remove_wrap() 将其删除,并且

    ¥nor can it be removed later using napi_remove_wrap(), and

  • API 创建的对象可以与 napi_wrap() 一起使用。

    ¥the object created by the API can be used with napi_wrap().

创建的值不是对象,因此不支持附加属性。它被认为是一个独特的值类型:使用外部值调用 napi_typeof() 会产生 napi_external

¥The created value is not an object, and therefore does not support additional properties. It is considered a distinct value type: calling napi_typeof() with an external value yields napi_external.