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:表示正在查询的 ArrayBufferSharedArrayBuffernapi_value
  • [out] dataArrayBufferSharedArrayBuffer 的底层数据缓冲区是 0,这可能是 NULL 或其他任何指针值。
  • [out] byte_length:底层数据缓冲区的字节长度。

如果 API 成功,则返回 napi_ok

【Returns napi_ok if the API succeeded.】

此 API 用于获取 ArrayBufferSharedArrayBuffer 的底层数据缓冲区及其长度。

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

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

WARNING: Use caution while using this API. The lifetime of the underlying data buffer is managed by the ArrayBuffer or SharedArrayBuffer 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 or SharedArrayBuffer. 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.】