napi_get_arraybuffer_info


napi_status napi_get_arraybuffer_info(napi_env env,
                                      napi_value arraybuffer,
                                      void** data,
                                      size_t* byte_length) 
  • [in] env:调用该 API 时所处的环境。
  • [in] arraybuffer:表示正在查询的 ArrayBuffernapi_value
  • [out] dataArrayBuffer 的底层数据缓冲区。如果 byte_length 为 0,则该值可能为 NULL 或任何其他指针值。
  • [out] byte_length:底层数据缓冲区的字节长度。

如果 API 成功,则返回 napi_ok

【Returns napi_ok if the API succeeded.】

此 API 用于检索 ArrayBuffer 的底层数据缓冲区及其长度。

【This API is used to retrieve the underlying data buffer of an ArrayBuffer and its length.】

警告: 在使用此 API 时请小心。底层数据缓冲区的生命周期由 ArrayBuffer 管理,即使数据已经返回。一个可能的安全使用此 API 的方法是结合 napi_create_reference 使用,它可以保证对 ArrayBuffer 生命周期的控制。在相同的回调中使用返回的数据缓冲区也是安全的,只要没有调用可能触发垃圾回收的其他 API。

WARNING: Use caution while using this API. The lifetime of the underlying data buffer is managed by the ArrayBuffer even after it's returned. A possible safe way to use this API is in conjunction with napi_create_reference, which can be used to guarantee control over the lifetime of the ArrayBuffer. It's also safe to use the returned data buffer within the same callback as long as there are no calls to other APIs that might trigger a GC.】