fix(napi): use null pointer for empty buffers

This commit is contained in:
Niklas Mischkulnig 2022-04-23 10:43:10 +02:00
parent 581e3bbb87
commit 39e55a39c9
No known key found for this signature in database
GPG key ID: C029B9EB62230BB0
3 changed files with 48 additions and 6 deletions

View file

@ -180,7 +180,14 @@ macro_rules! impl_typed_array {
unsafe {
sys::napi_create_external_arraybuffer(
env,
val.data as *mut c_void,
if length == 0 {
// Rust uses 0x1 as the data pointer for empty buffers,
// but NAPI/V8 only allows multiple buffers to have
// the same data pointer if it's 0x0.
ptr::null_mut()
} else {
val.data as *mut c_void
},
length,
Some(finalizer::<$rust_type>),
hint_ptr as *mut c_void,

View file

@ -176,7 +176,14 @@ impl ToNapiValue for Buffer {
sys::napi_create_external_buffer(
env,
len,
val.inner.as_mut_ptr() as *mut _,
if len == 0 {
// Rust uses 0x1 as the data pointer for empty buffers,
// but NAPI/V8 only allows multiple buffers to have
// the same data pointer if it's 0x0.
ptr::null_mut()
} else {
val.inner.as_mut_ptr() as *mut _
},
Some(drop_buffer),
Box::into_raw(Box::new((len, val.capacity))) as *mut _,
&mut ret,

View file

@ -264,7 +264,14 @@ impl Env {
sys::napi_create_external_buffer(
self.0,
length,
data_ptr as *mut c_void,
if length == 0 {
// Rust uses 0x1 as the data pointer for empty buffers,
// but NAPI/V8 only allows multiple buffers to have
// the same data pointer if it's 0x0.
ptr::null_mut()
} else {
data_ptr as *mut c_void
},
Some(drop_buffer),
Box::into_raw(Box::new((length, data.capacity()))) as *mut c_void,
&mut raw_value,
@ -301,7 +308,14 @@ impl Env {
sys::napi_create_external_buffer(
self.0,
length,
data as *mut c_void,
if length == 0 {
// Rust uses 0x1 as the data pointer for empty buffers,
// but NAPI/V8 only allows multiple buffers to have
// the same data pointer if it's 0x0.
ptr::null_mut()
} else {
data as *mut c_void
},
Some(
raw_finalize_with_custom_callback::<Hint, Finalize>
as unsafe extern "C" fn(
@ -386,7 +400,14 @@ impl Env {
check_status!(unsafe {
sys::napi_create_external_arraybuffer(
self.0,
data_ptr as *mut c_void,
if length == 0 {
// Rust uses 0x1 as the data pointer for empty buffers,
// but NAPI/V8 only allows multiple buffers to have
// the same data pointer if it's 0x0.
ptr::null_mut()
} else {
data_ptr as *mut c_void
},
length,
Some(drop_buffer),
Box::into_raw(Box::new((length, data.capacity()))) as *mut c_void,
@ -426,7 +447,14 @@ impl Env {
check_status!(unsafe {
sys::napi_create_external_arraybuffer(
self.0,
data as *mut c_void,
if length == 0 {
// Rust uses 0x1 as the data pointer for empty buffers,
// but NAPI/V8 only allows multiple buffers to have
// the same data pointer if it's 0x0.
ptr::null_mut()
} else {
data as *mut c_void
},
length,
Some(
raw_finalize_with_custom_callback::<Hint, Finalize>