diff --git a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs index ce092d31..99bdfe54 100644 --- a/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs @@ -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, diff --git a/crates/napi/src/bindgen_runtime/js_values/buffer.rs b/crates/napi/src/bindgen_runtime/js_values/buffer.rs index 0565aaad..a3135890 100644 --- a/crates/napi/src/bindgen_runtime/js_values/buffer.rs +++ b/crates/napi/src/bindgen_runtime/js_values/buffer.rs @@ -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, diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index b239d1e7..97c552e7 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -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:: 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::